1#ifndef STAN_MATH_PRIM_FUN_GP_MATERN32_COV_HPP
2#define STAN_MATH_PRIM_FUN_GP_MATERN32_COV_HPP
36template <
typename T_x,
typename T_s,
typename T_l>
37inline typename Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic,
40 const T_l &length_scale) {
45 Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
52 const char *function =
"gp_matern32_cov";
54 for (
size_t i = 0; i < x_size; ++i) {
59 for (
size_t n = 0; n < x_size; ++n) {
66 T_s sigma_sq =
square(sigma);
67 T_l root_3_inv_l = std::sqrt(3.0) / length_scale;
69 size_t block_size = 10;
70 for (
size_t jb = 0; jb < x_size; jb += block_size) {
71 for (
size_t ib = jb; ib < x_size; ib += block_size) {
72 size_t j_end = std::min(x_size, jb + block_size);
73 for (
size_t j = jb; j < j_end; ++j) {
74 cov.coeffRef(j, j) = sigma_sq;
75 size_t i_end = std::min(x_size, ib + block_size);
76 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 * (1.0 + root_3_inv_l * dist)
80 *
exp(-root_3_inv_l * dist);
106template <
typename T_x,
typename T_s,
typename T_l>
107inline typename Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic,
110 const T_s &sigma,
const std::vector<T_l> &length_scale) {
114 Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
120 const char *function =
"gp_matern32_cov";
121 for (
size_t n = 0; n < x_size; ++n) {
128 size_t l_size = length_scale.size();
129 for (
size_t n = 0; n < l_size; ++n) {
134 "number of length scales", l_size);
136 T_s sigma_sq =
square(sigma);
137 double root_3 = std::sqrt(3.0);
139 std::vector<Eigen::Matrix<return_type_t<T_x, T_l>, -1, 1>> x_new
142 size_t block_size = 10;
143 for (
size_t jb = 0; jb < x_size; jb += block_size) {
144 for (
size_t ib = jb; ib < x_size; ib += block_size) {
145 size_t j_end = std::min(x_size, jb + block_size);
146 for (
size_t j = jb; j < j_end; ++j) {
147 size_t i_end = std::min(x_size, ib + block_size);
148 for (
size_t i = std::max(ib, j); i < i_end; ++i) {
150 cov.coeffRef(j, i) = cov.coeffRef(i, j)
151 = sigma_sq * (1.0 + root_3 * dist) *
exp(-root_3 * dist);
183template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
184inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
185 Eigen::Dynamic, Eigen::Dynamic>
187 const T_s &sigma,
const T_l &length_scale) {
192 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
194 cov(x1_size, x2_size);
196 if (x1_size == 0 || x2_size == 0) {
200 const char *function =
"gp_matern32_cov";
202 for (
size_t i = 0; i < x1_size; ++i) {
206 for (
size_t i = 0; i < x2_size; ++i) {
211 for (
size_t n = 0; n < x1_size; ++n) {
214 for (
size_t n = 0; n < x2_size; ++n) {
221 T_s sigma_sq =
square(sigma);
222 T_l root_3_inv_l_sq = std::sqrt(3.0) / length_scale;
224 size_t block_size = 10;
225 for (
size_t ib = 0; ib < x1.size(); ib += block_size) {
226 for (
size_t jb = 0; jb < x2.size(); jb += block_size) {
227 size_t j_end = std::min(x2_size, jb + block_size);
228 for (
size_t j = jb; j < j_end; ++j) {
229 size_t i_end = std::min(x1_size, ib + block_size);
230 for (
size_t i = ib; i < i_end; ++i) {
232 cov(i, j) = sigma_sq * (1.0 + root_3_inv_l_sq * dist)
233 *
exp(-root_3_inv_l_sq * dist);
265template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
266inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
267 Eigen::Dynamic, Eigen::Dynamic>
269 const std::vector<Eigen::Matrix<T_x2, -1, 1>> &x2,
270 const T_s &sigma,
const std::vector<T_l> &length_scale) {
275 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
277 cov(x1_size, x2_size);
279 if (x1_size == 0 || x2_size == 0) {
283 const char *function =
"gp_matern_32_cov";
284 for (
size_t n = 0; n < x1_size; ++n) {
287 for (
size_t n = 0; n < x2_size; ++n) {
294 size_t l_size = length_scale.size();
295 for (
size_t n = 0; n < l_size; ++n) {
299 for (
size_t i = 0; i < x1_size; ++i) {
301 "number of length scales", l_size);
303 for (
size_t i = 0; i < x2_size; ++i) {
305 "number of length scales", l_size);
308 T_s sigma_sq =
square(sigma);
309 double root_3 = std::sqrt(3.0);
311 std::vector<Eigen::Matrix<return_type_t<T_x1, T_l>, -1, 1>> x1_new
313 std::vector<Eigen::Matrix<return_type_t<T_x2, T_l>, -1, 1>> x2_new
316 size_t block_size = 10;
318 for (
size_t ib = 0; ib < x1.size(); ib += block_size) {
319 for (
size_t jb = 0; jb < x2.size(); jb += block_size) {
320 size_t j_end = std::min(x2_size, jb + block_size);
321 for (
size_t j = jb; j < j_end; ++j) {
322 size_t i_end = std::min(x1_size, ib + block_size);
323 for (
size_t i = ib; i < i_end; ++i) {
325 cov(i, j) = sigma_sq * (1.0 + root_3 * dist) *
exp(-root_3 * 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_matern32_cov(const T1 &x, const T2 sigma, const T3 length_scale)
Matern 3/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>>.
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.
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 ...