1#ifndef STAN_MATH_PRIM_FUN_GP_MATERN52_COV_HPP
2#define STAN_MATH_PRIM_FUN_GP_MATERN52_COV_HPP
38template <
typename T_x,
typename T_s,
typename T_l>
39inline typename Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic,
42 const T_l &length_scale) {
46 size_t x_size = x.size();
47 Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
54 for (
size_t n = 0; n < x_size; ++n) {
61 T_s sigma_sq =
square(sigma);
62 T_l root_5_inv_l =
sqrt(5.0) / length_scale;
63 T_l inv_l_sq_5_3 = 5.0 / (3.0 *
square(length_scale));
65 size_t block_size = 10;
66 for (
size_t jb = 0; jb < x_size; jb += block_size) {
67 for (
size_t ib = jb; ib < x_size; ib += block_size) {
68 size_t j_end = std::min(x_size, jb + block_size);
69 for (
size_t j = jb; j < j_end; ++j) {
70 cov.coeffRef(j, j) = sigma_sq;
71 size_t i_end = std::min(x_size, ib + block_size);
72 for (
size_t i = std::max(ib, j + 1); i < i_end; ++i) {
75 cov.coeffRef(j, i) = cov.coeffRef(i, j)
77 * (1.0 + root_5_inv_l * dist + inv_l_sq_5_3 * sq_distance)
78 *
exp(-root_5_inv_l * dist);
109template <
typename T_x,
typename T_s,
typename T_l>
110inline typename Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic,
113 const T_s &sigma,
const std::vector<T_l> &length_scale) {
116 size_t x_size = x.size();
117 Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
123 size_t l_size = length_scale.size();
124 for (
size_t n = 0; n < x_size; ++n) {
131 "number of length scales", l_size);
133 T_s sigma_sq =
square(sigma);
134 double root_5 =
sqrt(5.0);
135 double five_thirds = 5.0 / 3.0;
137 std::vector<Eigen::Matrix<return_type_t<T_x, T_l>, -1, 1>> x_new
139 size_t block_size = 10;
141 for (
size_t jb = 0; jb < x_size; jb += block_size) {
142 for (
size_t ib = jb; ib < x_size; ib += block_size) {
143 size_t j_end = std::min(x_size, jb + block_size);
144 for (
size_t j = jb; j < j_end; ++j) {
145 cov.coeffRef(j, j) = sigma_sq;
146 size_t i_end = std::min(x_size, ib + block_size);
147 for (
size_t i = std::max(ib, j + 1); i < i_end; ++i) {
151 cov.coeffRef(j, i) = cov.coeffRef(i, j)
152 = sigma_sq * (1.0 + root_5 * dist + five_thirds * sq_distance)
153 *
exp(-root_5 * dist);
185template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
186inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
187 Eigen::Dynamic, Eigen::Dynamic>
189 const T_s &sigma,
const T_l &length_scale) {
192 size_t x1_size = x1.size();
193 size_t x2_size = x2.size();
194 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
196 cov(x1_size, x2_size);
198 if (x1_size == 0 || x2_size == 0) {
202 for (
size_t n = 0; n < x1_size; ++n) {
205 for (
size_t n = 0; n < x2_size; ++n) {
212 T_s sigma_sq =
square(sigma);
213 T_l root_5_inv_l =
sqrt(5.0) / length_scale;
214 T_l inv_l_sq_5_3 = 5.0 / (3.0 *
square(length_scale));
216 size_t block_size = 10;
218 for (
size_t ib = 0; ib < x1_size; ib += block_size) {
219 for (
size_t jb = 0; jb < x2_size; jb += block_size) {
220 size_t j_end = std::min(x2_size, jb + block_size);
221 for (
size_t j = jb; j < j_end; ++j) {
222 size_t i_end = std::min(x1_size, ib + block_size);
223 for (
size_t i = ib; i < i_end; ++i) {
229 * (1.0 + root_5_inv_l * dist + inv_l_sq_5_3 * sq_distance)
230 *
exp(-root_5_inv_l * dist);
267template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
268inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
269 Eigen::Dynamic, Eigen::Dynamic>
271 const std::vector<Eigen::Matrix<T_x2, Eigen::Dynamic, 1>> &x2,
272 const T_s &sigma,
const std::vector<T_l> &length_scale) {
275 size_t x1_size = x1.size();
276 size_t x2_size = x2.size();
277 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
279 cov(x1_size, x2_size);
281 if (x1_size == 0 || x2_size == 0) {
285 size_t l_size = length_scale.size();
287 for (
size_t n = 0; n < x1_size; ++n) {
290 for (
size_t n = 0; n < x2_size; ++n) {
297 "number of length scales", l_size);
299 "number of length scales", l_size);
301 T_s sigma_sq =
square(sigma);
302 double root_5 =
sqrt(5.0);
303 double five_thirds = 5.0 / 3.0;
305 std::vector<Eigen::Matrix<return_type_t<T_x1, T_l>, -1, 1>> x1_new
307 std::vector<Eigen::Matrix<return_type_t<T_x2, T_l>, -1, 1>> x2_new
309 size_t block_size = 10;
311 for (
size_t ib = 0; ib < x1_size; ib += block_size) {
312 for (
size_t jb = 0; jb < x2_size; jb += block_size) {
313 size_t j_end = std::min(x2_size, jb + block_size);
314 for (
size_t j = jb; j < j_end; ++j) {
315 size_t i_end = std::min(x1_size, ib + block_size);
316 for (
size_t i = ib; i < i_end; ++i) {
321 = sigma_sq * (1.0 + root_5 * dist + five_thirds * sq_distance)
322 *
exp(-root_5 * dist);
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_matern52_cov(const T1 &x, const T2 sigma, const T3 length_scale)
Matern 5/2 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>>.
fvar< T > sqrt(const fvar< T > &x)
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
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)
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...