Automatic Differentiation
 
Loading...
Searching...
No Matches
gp_exponential_cov.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_GP_EXPONENTIAL_COV_HPP
2#define STAN_MATH_PRIM_FUN_GP_EXPONENTIAL_COV_HPP
3
12#include <cmath>
13#include <vector>
14
15namespace stan {
16namespace math {
17
35template <typename T_x, typename T_s, typename T_l>
36inline typename Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic,
37 Eigen::Dynamic>
38gp_exponential_cov(const std::vector<T_x> &x, const T_s &sigma,
39 const T_l &length_scale) {
40 using std::exp;
41 using std::pow;
42
43 size_t x_size = stan::math::size(x);
44 Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
45 cov(x_size, x_size);
46 if (x_size == 0) {
47 return cov;
48 }
49
50 const char *function = "gp_exponential_cov";
51 size_t x_obs_size = stan::math::size(x[0]);
52 for (size_t i = 0; i < x_size; ++i) {
53 check_size_match(function, "x row", x_obs_size, "x's other row",
54 stan::math::size(x[i]));
55 }
56
57 for (size_t i = 0; i < x_size; ++i) {
58 check_not_nan(function, "x", x[i]);
59 }
60
61 check_positive_finite(function, "magnitude", sigma);
62 check_positive_finite(function, "length scale", length_scale);
63
64 T_s sigma_sq = square(sigma);
65 T_l neg_inv_l = -1.0 / length_scale;
66
67 size_t block_size = 10;
68 for (size_t jb = 0; jb < x_size; jb += block_size) {
69 for (size_t ib = jb; ib < x_size; ib += block_size) {
70 size_t j_end = std::min(x_size, jb + block_size);
71 for (size_t j = jb; j < j_end; ++j) {
72 cov(j, j) = sigma_sq;
73 size_t i_end = std::min(x_size, ib + block_size);
74 for (size_t i = std::max(ib, j + 1); i < i_end; ++i) {
75 cov.coeffRef(j, i) = cov.coeffRef(i, j)
76 = sigma_sq * exp(neg_inv_l * distance(x[i], x[j]));
77 }
78 }
79 }
80 }
81 return cov;
82}
83
100template <typename T_x, typename T_s, typename T_l>
101inline typename Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic,
102 Eigen::Dynamic>
103gp_exponential_cov(const std::vector<Eigen::Matrix<T_x, -1, 1>> &x,
104 const T_s &sigma, const std::vector<T_l> &length_scale) {
105 using std::exp;
106 using std::pow;
107
108 size_t x_size = stan::math::size(x);
109 Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
110 cov(x_size, x_size);
111 if (x_size == 0) {
112 return cov;
113 }
114
115 const char *function = "gp_exponential_cov";
116 for (size_t n = 0; n < x_size; ++n) {
117 check_not_nan(function, "x", x[n]);
118 }
119
120 check_positive_finite(function, "magnitude", sigma);
121 check_positive_finite(function, "length scale", length_scale);
122
123 size_t l_size = length_scale.size();
124 check_size_match(function, "x dimension", x[0].size(),
125 "number of length scales", l_size);
126
127 std::vector<Eigen::Matrix<return_type_t<T_x, T_l>, -1, 1>> x_new
128 = divide_columns(x, length_scale);
129
130 T_s sigma_sq = square(sigma);
131 size_t block_size = 10;
132 for (size_t jb = 0; jb < x_size; jb += block_size) {
133 for (size_t ib = jb; ib < x_size; ib += block_size) {
134 size_t j_end = std::min(x_size, jb + block_size);
135 for (size_t j = jb; j < j_end; ++j) {
136 cov(j, j) = sigma_sq;
137 size_t i_end = std::min(x_size, ib + block_size);
138 for (size_t i = std::max(ib, j + 1); i < i_end; ++i) {
139 return_type_t<T_x, T_l> dist = distance(x_new[i], x_new[j]);
140 cov.coeffRef(j, i) = cov.coeffRef(i, j) = sigma_sq * exp(-dist);
141 }
142 }
143 }
144 }
145 return cov;
146}
147
168template <typename T_x1, typename T_x2, typename T_s, typename T_l>
169inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
170 Eigen::Dynamic, Eigen::Dynamic>
171gp_exponential_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2,
172 const T_s &sigma, const T_l &length_scale) {
173 using std::exp;
174 using std::pow;
175
176 size_t x1_size = stan::math::size(x1);
177 size_t x2_size = stan::math::size(x2);
178 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
179 Eigen::Dynamic>
180 cov(x1_size, x2_size);
181 if (x1_size == 0 || x2_size == 0) {
182 return cov;
183 }
184
185 const char *function = "gp_exponential_cov";
186 size_t x1_obs_size = stan::math::size(x1[0]);
187 for (size_t i = 0; i < x1_size; ++i) {
188 check_size_match(function, "x1's row", x1_obs_size, "x1's other row",
189 stan::math::size(x1[i]));
190 }
191 for (size_t i = 0; i < x2_size; ++i) {
192 check_size_match(function, "x1's row", x1_obs_size, "x2's other row",
193 stan::math::size(x2[i]));
194 }
195
196 for (size_t n = 0; n < x1_size; ++n) {
197 check_not_nan(function, "x1", x1[n]);
198 }
199 for (size_t n = 0; n < x2_size; ++n) {
200 check_not_nan(function, "x2", x2[n]);
201 }
202
203 check_positive_finite(function, "magnitude", sigma);
204 check_positive_finite(function, "length scale", length_scale);
205
206 T_s sigma_sq = square(sigma);
207 T_l neg_inv_l = -1.0 / length_scale;
208 size_t block_size = 10;
209
210 for (size_t ib = 0; ib < x1_size; ib += block_size) {
211 for (size_t jb = 0; jb < x2_size; jb += block_size) {
212 size_t j_end = std::min(x2_size, jb + block_size);
213 for (size_t j = jb; j < j_end; ++j) {
214 size_t i_end = std::min(x1_size, ib + block_size);
215 for (size_t i = ib; i < i_end; ++i) {
216 cov(i, j) = sigma_sq * exp(neg_inv_l * distance(x1[i], x2[j]));
217 }
218 }
219 }
220 }
221 return cov;
222}
223
245template <typename T_x1, typename T_x2, typename T_s, typename T_l>
246inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
247 Eigen::Dynamic, Eigen::Dynamic>
248gp_exponential_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>> &x1,
249 const std::vector<Eigen::Matrix<T_x2, -1, 1>> &x2,
250 const T_s &sigma, const std::vector<T_l> &length_scale) {
251 using std::exp;
252 using std::pow;
253
254 size_t x1_size = stan::math::size(x1);
255 size_t x2_size = stan::math::size(x2);
256 size_t l_size = stan::math::size(length_scale);
257 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
258 Eigen::Dynamic>
259 cov(x1_size, x2_size);
260 if (x1_size == 0 || x2_size == 0) {
261 return cov;
262 }
263
264 const char *function = "gp_exponential_cov";
265 for (size_t n = 0; n < x1_size; ++n) {
266 check_not_nan(function, "x1", x1[n]);
267 }
268 for (size_t n = 0; n < x2_size; ++n) {
269 check_not_nan(function, "x2", x2[n]);
270 }
271
272 check_positive_finite(function, "magnitude", sigma);
273 check_positive_finite(function, "length scale", length_scale);
274
275 for (size_t i = 0; i < x1_size; ++i) {
276 check_size_match(function, "x1's row", stan::math::size(x1[i]),
277 "number of length scales", l_size);
278 }
279 for (size_t i = 0; i < x2_size; ++i) {
280 check_size_match(function, "x2's row", stan::math::size(x2[i]),
281 "number of length scales", l_size);
282 }
283
284 T_s sigma_sq = square(sigma);
285
286 std::vector<Eigen::Matrix<return_type_t<T_x1, T_l>, -1, 1>> x1_new
287 = divide_columns(x1, length_scale);
288 std::vector<Eigen::Matrix<return_type_t<T_x2, T_l>, -1, 1>> x2_new
289 = divide_columns(x2, length_scale);
290
291 size_t block_size = 10;
292
293 for (size_t ib = 0; ib < x1_size; ib += block_size) {
294 for (size_t jb = 0; jb < x2_size; jb += block_size) {
295 size_t j_end = std::min(x2_size, jb + block_size);
296 for (size_t j = jb; j < j_end; ++j) {
297 size_t i_end = std::min(x1_size, ib + block_size);
298 for (size_t i = ib; i < i_end; ++i) {
299 cov(i, j) = sigma_sq * exp(-distance(x1_new[i], x2_new[j]));
300 }
301 }
302 }
303 }
304 return cov;
305}
306} // namespace math
307} // namespace stan
308#endif
void divide_columns(matrix_cl< T1 > &A, const matrix_cl< T2 > &B)
Divides each column of a matrix by a vector.
matrix_cl< return_type_t< T1, T2, T3 > > gp_exponential_cov(const T1 &x, const T2 sigma, const T3 length_scale)
Matern exponential kernel on the GPU.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
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.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
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 ...