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>
51 const char *fun =
"gp_periodic_cov";
55 for (
size_t n = 0; n < x.size(); ++n) {
59 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l, T_p>, Eigen::Dynamic,
61 cov(x.size(), x.size());
63 size_t x_size = x.size();
68 T_sigma sigma_sq =
square(sigma);
70 T_p pi_div_p =
pi() / p;
71 size_t block_size = 10;
73 for (
size_t jb = 0; jb < x_size; jb += block_size) {
74 for (
size_t ib = jb; ib < x_size; ib += block_size) {
75 size_t j_end = std::min(x_size, jb + block_size);
76 for (
size_t j = jb; j < j_end; ++j) {
77 cov.coeffRef(j, j) = sigma_sq;
78 size_t i_end = std::min(x_size, ib + block_size);
79 for (
size_t i = std::max(ib, j + 1); i < i_end; ++i) {
80 cov.coeffRef(j, i) = cov.coeffRef(i, j)
123template <
typename T_x1,
typename T_x2,
typename T_sigma,
typename T_l,
125inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l, T_p>,
126 Eigen::Dynamic, Eigen::Dynamic>
128 const T_sigma &sigma,
const T_l &l,
const T_p &p) {
131 const char *fun =
"gp_periodic_cov";
135 for (
size_t n = 0; n < x1.size(); ++n) {
138 for (
size_t n = 0; n < x2.size(); ++n) {
142 Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l, T_p>, Eigen::Dynamic,
144 cov(x1.size(), x2.size());
145 if (x1.size() == 0 || x2.size() == 0) {
149 T_sigma sigma_sq =
square(sigma);
151 T_p pi_div_p =
pi() / p;
152 size_t block_size = 10;
154 for (
size_t ib = 0; ib < x1.size(); ib += block_size) {
155 for (
size_t jb = 0; jb < x2.size(); jb += block_size) {
156 size_t j_end = std::min(x2.size(), jb + block_size);
157 for (
size_t j = jb; j < j_end; ++j) {
158 size_t i_end = std::min(x1.size(), ib + block_size);
159 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 ...