1#ifndef STAN_MATH_PRIM_FUN_GP_EXP_QUAD_COV_HPP
2#define STAN_MATH_PRIM_FUN_GP_EXP_QUAD_COV_HPP
33template <
typename T_x,
typename T_sigma,
typename T_l>
34inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
37 const T_l &neg_half_inv_l_sq) {
39 const size_t x_size = x.size();
40 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
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) {
58 cov.template triangularView<Eigen::Upper>() = cov.transpose();
80template <
typename T_x1,
typename T_x2,
typename T_sigma,
typename T_l>
81inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>,
82 Eigen::Dynamic, Eigen::Dynamic>
84 const T_sigma &sigma_sq,
const T_l &neg_half_inv_l_sq) {
86 Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>, Eigen::Dynamic,
88 cov(x1.size(), x2.size());
89 size_t block_size = 10;
91 for (
size_t ib = 0; ib < x1.size(); ib += block_size) {
92 for (
size_t jb = 0; jb < x2.size(); jb += block_size) {
93 size_t j_end = std::min(x2.size(), jb + block_size);
94 for (
size_t j = jb; j < j_end; ++j) {
95 size_t i_end = std::min(x1.size(), ib + block_size);
96 for (
size_t i = ib; i < i_end; ++i) {
123template <
typename T_x,
typename T_sigma,
typename T_l>
124inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
127 const T_l &length_scale) {
131 const size_t x_size = x.size();
132 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
140 for (
size_t n = 0; n < x_size; ++n) {
145 -0.5 /
square(length_scale));
163template <
typename T_x,
typename T_sigma,
typename T_l>
164inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
167 const T_sigma &sigma,
const std::vector<T_l> &length_scale) {
171 size_t x_size = x.size();
172 Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
180 "number of length scales", length_scale.size());
206template <
typename T_x1,
typename T_x2,
typename T_sigma,
typename T_l>
207inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>,
208 Eigen::Dynamic, Eigen::Dynamic>
210 const T_sigma &sigma,
const T_l &length_scale) {
211 const char *function_name =
"gp_exp_quad_cov";
215 const size_t x1_size = x1.size();
216 const size_t x2_size = x2.size();
217 Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>, Eigen::Dynamic,
219 cov(x1_size, x2_size);
220 if (x1_size == 0 || x2_size == 0) {
224 for (
size_t i = 0; i < x1_size; ++i) {
227 for (
size_t i = 0; i < x2_size; ++i) {
232 -0.5 /
square(length_scale));
255template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
256inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
257 Eigen::Dynamic, Eigen::Dynamic>
259 const std::vector<Eigen::Matrix<T_x2, -1, 1>> &x2,
260 const T_s &sigma,
const std::vector<T_l> &length_scale) {
261 size_t x1_size = x1.size();
262 size_t x2_size = x2.size();
263 size_t l_size = length_scale.size();
265 Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>, Eigen::Dynamic,
267 cov(x1_size, x2_size);
269 if (x1_size == 0 || x2_size == 0) {
273 const char *function_name =
"gp_exp_quad_cov";
274 for (
size_t i = 0; i < x1_size; ++i) {
277 for (
size_t i = 0; i < x2_size; ++i) {
283 "number of length scales", l_size);
285 "number of length scales", l_size);
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>>.
Eigen::Matrix< return_type_t< T_x, T_sigma, T_l >, Eigen::Dynamic, Eigen::Dynamic > gp_exp_quad_cov(const std::vector< T_x > &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)
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...