Loading [MathJax]/extensions/TeX/AMSsymbols.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
gp_exp_quad_cov.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_GP_EXP_QUAD_COV_HPP
2#define STAN_MATH_PRIM_FUN_GP_EXP_QUAD_COV_HPP
3
11#include <cmath>
12#include <type_traits>
13#include <vector>
14
15namespace stan {
16namespace math {
17
18namespace internal {
19
33template <typename T_x, typename T_x_alloc, typename T_sigma, typename T_l>
34inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
35 Eigen::Dynamic>
36gp_exp_quad_cov(const std::vector<T_x, T_x_alloc> &x, const T_sigma &sigma_sq,
37 const T_l &neg_half_inv_l_sq) {
38 using std::exp;
39 const size_t x_size = x.size();
40 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
41 Eigen::Dynamic>
42 cov(x_size, x_size);
43 cov.diagonal().array() = sigma_sq;
44 size_t block_size = 10;
45 for (size_t jb = 0; jb < x.size(); jb += block_size) {
46 for (size_t ib = jb; ib < x.size(); ib += block_size) {
47 size_t j_end = std::min(x_size, jb + block_size);
48 for (size_t j = jb; j < j_end; ++j) {
49 size_t i_end = std::min(x_size, ib + block_size);
50 for (size_t i = std::max(ib, j + 1); i < i_end; ++i) {
51 cov.coeffRef(i, j)
52 = sigma_sq
53 * exp(squared_distance(x[i], x[j]) * neg_half_inv_l_sq);
54 }
55 }
56 }
57 }
58 cov.template triangularView<Eigen::Upper>() = cov.transpose();
59 return cov;
60}
61
80template <typename T_x1, typename T_x1_alloc, typename T_x2, typename T_sigma,
81 typename T_l>
82inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>,
83 Eigen::Dynamic, Eigen::Dynamic>
84gp_exp_quad_cov(const std::vector<T_x1, T_x1_alloc> &x1,
85 const std::vector<T_x2> &x2, const T_sigma &sigma_sq,
86 const T_l &neg_half_inv_l_sq) {
87 using std::exp;
88 Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>, Eigen::Dynamic,
89 Eigen::Dynamic>
90 cov(x1.size(), x2.size());
91 size_t block_size = 10;
92
93 for (size_t ib = 0; ib < x1.size(); ib += block_size) {
94 for (size_t jb = 0; jb < x2.size(); jb += block_size) {
95 size_t j_end = std::min(x2.size(), jb + block_size);
96 for (size_t j = jb; j < j_end; ++j) {
97 size_t i_end = std::min(x1.size(), ib + block_size);
98 for (size_t i = ib; i < i_end; ++i) {
99 cov.coeffRef(i, j)
100 = sigma_sq
101 * exp(squared_distance(x1[i], x2[j]) * neg_half_inv_l_sq);
102 }
103 }
104 }
105 }
106 return cov;
107}
108} // namespace internal
109
125template <typename T_x, typename T_x_alloc, typename T_sigma, typename T_l>
126inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
127 Eigen::Dynamic>
128gp_exp_quad_cov(const std::vector<T_x, T_x_alloc> &x, const T_sigma &sigma,
129 const T_l &length_scale) {
130 check_positive("gp_exp_quad_cov", "magnitude", sigma);
131 check_positive("gp_exp_quad_cov", "length scale", length_scale);
132
133 const size_t x_size = x.size();
134 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
135 Eigen::Dynamic>
136 cov(x_size, x_size);
137
138 if (x_size == 0) {
139 return cov;
140 }
141
142 for (size_t n = 0; n < x_size; ++n) {
143 check_not_nan("gp_exp_quad_cov", "x", x[n]);
144 }
145
146 cov = internal::gp_exp_quad_cov(x, square(sigma),
147 -0.5 / square(length_scale));
148 return cov;
149}
150
165template <typename T_x, typename T_x_alloc, typename T_sigma, typename T_l,
166 typename T_l_alloc>
167inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
168 Eigen::Dynamic>
169gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x, -1, 1>, T_x_alloc> &x,
170 const T_sigma &sigma,
171 const std::vector<T_l, T_l_alloc> &length_scale) {
172 check_positive_finite("gp_exp_quad_cov", "magnitude", sigma);
173 check_positive_finite("gp_exp_quad_cov", "length scale", length_scale);
174
175 size_t x_size = x.size();
176 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
177 Eigen::Dynamic>
178 cov(x_size, x_size);
179 if (x_size == 0) {
180 return cov;
181 }
182
183 check_size_match("gp_exp_quad_cov", "x dimension", x[0].size(),
184 "number of length scales", length_scale.size());
185 cov = internal::gp_exp_quad_cov(divide_columns(x, length_scale),
186 square(sigma), -0.5);
187 return cov;
188}
189
210template <typename T_x1, typename T_x1_alloc, typename T_x2,
211 typename T_x2_alloc, typename T_sigma, typename T_l>
212inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>,
213 Eigen::Dynamic, Eigen::Dynamic>
214gp_exp_quad_cov(const std::vector<T_x1, T_x1_alloc> &x1,
215 const std::vector<T_x2, T_x2_alloc> &x2, const T_sigma &sigma,
216 const T_l &length_scale) {
217 const char *function_name = "gp_exp_quad_cov";
218 check_positive(function_name, "magnitude", sigma);
219 check_positive(function_name, "length scale", length_scale);
220
221 const size_t x1_size = x1.size();
222 const size_t x2_size = x2.size();
223 Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>, Eigen::Dynamic,
224 Eigen::Dynamic>
225 cov(x1_size, x2_size);
226 if (x1_size == 0 || x2_size == 0) {
227 return cov;
228 }
229
230 for (size_t i = 0; i < x1_size; ++i) {
231 check_not_nan(function_name, "x1", x1[i]);
232 }
233 for (size_t i = 0; i < x2_size; ++i) {
234 check_not_nan(function_name, "x2", x2[i]);
235 }
236
237 cov = internal::gp_exp_quad_cov(x1, x2, square(sigma),
238 -0.5 / square(length_scale));
239 return cov;
240}
241
261template <typename T_x1, typename T_x1_alloc, typename T_x2,
262 typename T_x2_alloc, typename T_s, typename T_l, typename T_l_alloc>
263inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
264 Eigen::Dynamic, Eigen::Dynamic>
265gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>, T_x1_alloc> &x1,
266 const std::vector<Eigen::Matrix<T_x2, -1, 1>, T_x2_alloc> &x2,
267 const T_s &sigma,
268 const std::vector<T_l, T_l_alloc> &length_scale) {
269 size_t x1_size = x1.size();
270 size_t x2_size = x2.size();
271 size_t l_size = length_scale.size();
272
273 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
274 Eigen::Dynamic>
275 cov(x1_size, x2_size);
276
277 if (x1_size == 0 || x2_size == 0) {
278 return cov;
279 }
280
281 const char *function_name = "gp_exp_quad_cov";
282 for (size_t i = 0; i < x1_size; ++i) {
283 check_not_nan(function_name, "x1", x1[i]);
284 }
285 for (size_t i = 0; i < x2_size; ++i) {
286 check_not_nan(function_name, "x2", x2[i]);
287 }
288 check_positive_finite(function_name, "magnitude", sigma);
289 check_positive_finite(function_name, "length scale", length_scale);
290 check_size_match(function_name, "x dimension", x1[0].size(),
291 "number of length scales", l_size);
292 check_size_match(function_name, "x dimension", x2[0].size(),
293 "number of length scales", l_size);
294 cov = internal::gp_exp_quad_cov(divide_columns(x1, length_scale),
295 divide_columns(x2, length_scale),
296 square(sigma), -0.5);
297 return cov;
298}
299
300} // namespace math
301} // namespace stan
302#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_exp_quad_cov(const matrix_cl< T1 > &x, const T2 sigma, const T3 length_scale)
Squared exponential kernel on the GPU.
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
Eigen::Matrix< return_type_t< T_x, T_sigma, T_l >, Eigen::Dynamic, Eigen::Dynamic > gp_exp_quad_cov(const std::vector< T_x, T_x_alloc > &x, const T_sigma &sigma_sq, const T_l &neg_half_inv_l_sq)
Returns a squared exponential kernel.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
auto squared_distance(const T_a &a, const T_b &b)
Returns the squared distance.
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 ...