1#ifndef STAN_MATH_PRIM_FUN_GP_EXPONENTIAL_COV_HPP
2#define STAN_MATH_PRIM_FUN_GP_EXPONENTIAL_COV_HPP
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,
39 const T_l &length_scale) {
44 Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
50 const char *function =
"gp_exponential_cov";
52 for (
size_t i = 0; i < x_size; ++i) {
57 for (
size_t i = 0; i < x_size; ++i) {
64 T_s sigma_sq =
square(sigma);
65 T_l neg_inv_l = -1.0 / length_scale;
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) {
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)
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,
104 const T_s &sigma,
const std::vector<T_l> &length_scale) {
109 Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
115 const char *function =
"gp_exponential_cov";
116 for (
size_t n = 0; n < x_size; ++n) {
123 size_t l_size = length_scale.size();
125 "number of length scales", l_size);
127 std::vector<Eigen::Matrix<return_type_t<T_x, T_l>, -1, 1>> x_new
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) {
140 cov.coeffRef(j, i) = cov.coeffRef(i, j) = sigma_sq *
exp(-dist);
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>
172 const T_s &sigma,
const T_l &length_scale) {
178 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
180 cov(x1_size, x2_size);
181 if (x1_size == 0 || x2_size == 0) {
185 const char *function =
"gp_exponential_cov";
187 for (
size_t i = 0; i < x1_size; ++i) {
191 for (
size_t i = 0; i < x2_size; ++i) {
196 for (
size_t n = 0; n < x1_size; ++n) {
199 for (
size_t n = 0; n < x2_size; ++n) {
206 T_s sigma_sq =
square(sigma);
207 T_l neg_inv_l = -1.0 / length_scale;
208 size_t block_size = 10;
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]));
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>
249 const std::vector<Eigen::Matrix<T_x2, -1, 1>> &x2,
250 const T_s &sigma,
const std::vector<T_l> &length_scale) {
257 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
259 cov(x1_size, x2_size);
260 if (x1_size == 0 || x2_size == 0) {
264 const char *function =
"gp_exponential_cov";
265 for (
size_t n = 0; n < x1_size; ++n) {
268 for (
size_t n = 0; n < x2_size; ++n) {
275 for (
size_t i = 0; i < x1_size; ++i) {
277 "number of length scales", l_size);
279 for (
size_t i = 0; i < x2_size; ++i) {
281 "number of length scales", l_size);
284 T_s sigma_sq =
square(sigma);
286 std::vector<Eigen::Matrix<return_type_t<T_x1, T_l>, -1, 1>> x1_new
288 std::vector<Eigen::Matrix<return_type_t<T_x2, T_l>, -1, 1>> x2_new
291 size_t block_size = 10;
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]));
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>>.
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 ...