Automatic Differentiation
 
Loading...
Searching...
No Matches
gp_periodic_cov.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_GP_PERIODIC_COV_HPP
2#define STAN_MATH_PRIM_FUN_GP_PERIODIC_COV_HPP
3
13#include <cmath>
14#include <vector>
15
16namespace stan {
17namespace math {
18
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>
47gp_periodic_cov(const std::vector<T_x> &x, const T_sigma &sigma, const T_l &l,
48 const T_p &p) {
49 const char *fun = "gp_periodic_cov";
50 check_positive(fun, "signal standard deviation", sigma);
51 check_positive(fun, "length-scale", l);
52 check_positive(fun, "period", p);
53 for (size_t n = 0; n < x.size(); ++n) {
54 check_not_nan(fun, "element of x", x[n]);
55 }
56
57 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l, T_p>, Eigen::Dynamic,
58 Eigen::Dynamic>
59 cov(x.size(), x.size());
60
61 size_t x_size = x.size();
62 if (x_size == 0) {
63 return cov;
64 }
65
66 T_sigma sigma_sq = square(sigma);
67 T_l neg_two_inv_l_sq = -2.0 * inv_square(l);
68 T_p pi_div_p = pi() / p;
69 size_t block_size = 10;
70
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)
79 = sigma_sq
80 * exp(square(sin(pi_div_p * distance(x[i], x[j])))
81 * neg_two_inv_l_sq);
82 }
83 }
84 }
85 }
86 return cov;
87}
88
121template <typename T_x1, typename T_x2, typename T_sigma, typename T_l,
122 typename T_p>
123inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l, T_p>,
124 Eigen::Dynamic, Eigen::Dynamic>
125gp_periodic_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2,
126 const T_sigma &sigma, const T_l &l, const T_p &p) {
127 const char *fun = "gp_periodic_cov";
128 check_positive(fun, "signal standard deviation", sigma);
129 check_positive(fun, "length-scale", l);
130 check_positive(fun, "period", p);
131 for (size_t n = 0; n < x1.size(); ++n) {
132 check_not_nan(fun, "element of x1", x1[n]);
133 }
134 for (size_t n = 0; n < x2.size(); ++n) {
135 check_not_nan(fun, "element of x2", x2[n]);
136 }
137
138 Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l, T_p>, Eigen::Dynamic,
139 Eigen::Dynamic>
140 cov(x1.size(), x2.size());
141 if (x1.size() == 0 || x2.size() == 0) {
142 return cov;
143 }
144
145 T_sigma sigma_sq = square(sigma);
146 T_l neg_two_inv_l_sq = -2.0 * inv_square(l);
147 T_p pi_div_p = pi() / p;
148 size_t block_size = 10;
149
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) {
156 cov.coeffRef(i, j)
157 = sigma_sq
158 * exp(square(sin(pi_div_p * distance(x1[i], x2[j])))
159 * neg_two_inv_l_sq);
160 }
161 }
162 }
163 }
164 return cov;
165}
166} // namespace math
167} // namespace stan
168
169#endif
fvar< T > sin(const fvar< T > &x)
Definition sin.hpp:16
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.
Definition distance.hpp:33
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.
Definition constants.hpp:36
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...