1#ifndef STAN_MATH_PRIM_FUN_GP_PERIODIC_COV_HPP
2#define STAN_MATH_PRIM_FUN_GP_PERIODIC_COV_HPP
44template <
typename T_x,
typename T_sigma,
typename T_l,
typename T_p>
45inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l, T_p>,
46 Eigen::Dynamic, Eigen::Dynamic>
49 const char *fun =
"gp_periodic_cov";
53 for (
size_t n = 0; n < x.size(); ++n) {
57 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l, T_p>, Eigen::Dynamic,
59 cov(x.size(), x.size());
61 size_t x_size = x.size();
66 T_sigma sigma_sq =
square(sigma);
68 T_p pi_div_p =
pi() / p;
69 size_t block_size = 10;
71 for (
size_t jb = 0; jb < x_size; jb += block_size) {
72 for (
size_t ib = jb; ib < x_size; ib += block_size) {
73 size_t j_end = std::min(x_size, jb + block_size);
74 for (
size_t j = jb; j < j_end; ++j) {
75 cov.coeffRef(j, j) = sigma_sq;
76 size_t i_end = std::min(x_size, ib + block_size);
77 for (
size_t i = std::max(ib, j + 1); i < i_end; ++i) {
78 cov.coeffRef(j, i) = cov.coeffRef(i, j)
121template <
typename T_x1,
typename T_x2,
typename T_sigma,
typename T_l,
123inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l, T_p>,
124 Eigen::Dynamic, Eigen::Dynamic>
126 const T_sigma &sigma,
const T_l &l,
const T_p &p) {
127 const char *fun =
"gp_periodic_cov";
131 for (
size_t n = 0; n < x1.size(); ++n) {
134 for (
size_t n = 0; n < x2.size(); ++n) {
138 Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l, T_p>, Eigen::Dynamic,
140 cov(x1.size(), x2.size());
141 if (x1.size() == 0 || x2.size() == 0) {
145 T_sigma sigma_sq =
square(sigma);
147 T_p pi_div_p =
pi() / p;
148 size_t block_size = 10;
150 for (
size_t ib = 0; ib < x1.size(); ib += block_size) {
151 for (
size_t jb = 0; jb < x2.size(); jb += block_size) {
152 size_t j_end = std::min(x2.size(), jb + block_size);
153 for (
size_t j = jb; j < j_end; ++j) {
154 size_t i_end = std::min(x1.size(), ib + block_size);
155 for (
size_t i = ib; i < i_end; ++i) {
fvar< T > sin(const fvar< T > &x)
fvar< T > inv_square(const fvar< T > &x)
auto distance(const T_a &a, const T_b &b)
Returns the distance between the specified vectors.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
Eigen::Matrix< return_type_t< T_x, T_sigma, T_l, T_p >, Eigen::Dynamic, Eigen::Dynamic > gp_periodic_cov(const std::vector< T_x > &x, const T_sigma &sigma, const T_l &l, const T_p &p)
Returns a periodic covariance matrix using the input .
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
static constexpr double pi()
Return the value of pi.
fvar< T > square(const fvar< T > &x)
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...