1 #ifndef STAN_MATH_PRIM_MAT_FUN_GP_MATERN52_COV_HPP 2 #define STAN_MATH_PRIM_MAT_FUN_GP_MATERN52_COV_HPP 41 template <
typename T_x,
typename T_s,
typename T_l>
42 inline typename Eigen::Matrix<typename return_type<T_x, T_s, T_l>::type,
43 Eigen::Dynamic, Eigen::Dynamic>
45 const T_l &length_scale) {
48 size_t x_size = x.size();
49 Eigen::Matrix<typename return_type<T_x, T_s, T_l>::type, Eigen::Dynamic,
56 for (
size_t n = 0; n < x_size; ++n)
62 T_s sigma_sq =
square(sigma);
63 T_l root_5_inv_l =
sqrt(5.0) / length_scale;
64 T_l inv_l_sq_5_3 = 5.0 / (3.0 *
square(length_scale));
65 T_l neg_root_5_inv_l = -
sqrt(5.0) / length_scale;
67 for (
size_t i = 0; i < x_size; ++i) {
69 for (
size_t j = i + 1; j < x_size; ++j) {
74 * (1.0 + root_5_inv_l * dist + inv_l_sq_5_3 * sq_distance)
75 *
exp(neg_root_5_inv_l * dist);
76 cov(j, i) = cov(i, j);
105 template <
typename T_x,
typename T_s,
typename T_l>
106 inline typename Eigen::Matrix<typename return_type<T_x, T_s, T_l>::type,
107 Eigen::Dynamic, Eigen::Dynamic>
109 const T_s &sigma,
const std::vector<T_l> &length_scale) {
112 size_t x_size = x.size();
113 Eigen::Matrix<typename return_type<T_x, T_s, T_l>::type, Eigen::Dynamic,
119 size_t l_size = length_scale.size();
120 for (
size_t n = 0; n < x_size; ++n)
126 "number of length scales", l_size);
128 T_s sigma_sq =
square(sigma);
129 double root_5 =
sqrt(5.0);
130 double five_thirds = 5.0 / 3.0;
131 double neg_root_5 = -root_5;
133 std::vector<Eigen::Matrix<typename return_type<T_x, T_l>::type, -1, 1>> x_new
136 for (
size_t i = 0; i < x_size; ++i) {
137 cov(i, i) = sigma_sq;
138 for (
size_t j = i + 1; j < x_size; ++j) {
142 cov(i, j) = sigma_sq * (1.0 + root_5 * dist + five_thirds * sq_distance)
143 *
exp(neg_root_5 * dist);
144 cov(j, i) = cov(i, j);
172 template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
173 inline typename Eigen::Matrix<typename return_type<T_x1, T_x2, T_s, T_l>::type,
174 Eigen::Dynamic, Eigen::Dynamic>
176 const T_s &sigma,
const T_l &length_scale) {
179 size_t x1_size = x1.size();
180 size_t x2_size = x2.size();
181 Eigen::Matrix<typename return_type<T_x1, T_x2, T_s, T_l>::type,
182 Eigen::Dynamic, Eigen::Dynamic>
183 cov(x1_size, x2_size);
185 if (x1_size == 0 || x2_size == 0)
188 for (
size_t n = 0; n < x1_size; ++n)
190 for (
size_t n = 0; n < x2_size; ++n)
196 T_s sigma_sq =
square(sigma);
197 T_l root_5_inv_l =
sqrt(5.0) / length_scale;
198 T_l inv_l_sq_5_3 = 5.0 / (3.0 *
square(length_scale));
199 T_l neg_root_5_inv_l = -
sqrt(5.0) / length_scale;
201 for (
size_t i = 0; i < x1_size; ++i) {
202 for (
size_t j = 0; j < x2_size; ++j) {
207 * (1.0 + root_5_inv_l * dist + inv_l_sq_5_3 * sq_distance)
208 *
exp(neg_root_5_inv_l * dist);
240 template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
241 inline typename Eigen::Matrix<
245 const std::vector<Eigen::Matrix<T_x2, Eigen::Dynamic, 1>> &x2,
246 const T_s &sigma,
const std::vector<T_l> &length_scale) {
249 size_t x1_size = x1.size();
250 size_t x2_size = x2.size();
251 Eigen::Matrix<typename return_type<T_x1, T_x2, T_s, T_l>::type,
252 Eigen::Dynamic, Eigen::Dynamic>
253 cov(x1_size, x2_size);
255 if (x1_size == 0 || x2_size == 0)
258 size_t l_size = length_scale.size();
260 for (
size_t n = 0; n < x1_size; ++n)
262 for (
size_t n = 0; n < x2_size; ++n)
268 "number of length scales", l_size);
270 "number of length scales", l_size);
272 T_s sigma_sq =
square(sigma);
273 double root_5 =
sqrt(5.0);
274 double five_thirds = 5.0 / 3.0;
275 double neg_root_5 = -root_5;
277 std::vector<Eigen::Matrix<typename return_type<T_x1, T_l>::type, -1, 1>>
279 std::vector<Eigen::Matrix<typename return_type<T_x2, T_l>::type, -1, 1>>
282 for (
size_t i = 0; i < x1_size; ++i) {
283 for (
size_t j = 0; j < x2_size; ++j) {
287 cov(i, j) = sigma_sq * (1.0 + root_5 * dist + five_thirds * sq_distance)
288 *
exp(neg_root_5 * dist);
fvar< T > sqrt(const fvar< T > &x)
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.
fvar< T > square(const fvar< T > &x)
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
std::vector< Eigen::Matrix< typename return_type< T_x, T_v, double >::type, Eigen::Dynamic, 1 > > divide_columns(const std::vector< Eigen::Matrix< T_x, Eigen::Dynamic, 1 >> &x, const std::vector< T_v > &vec)
Takes Stan data type vector[n] x[D] and divides column vector in x element-wise by the values in vec...
fvar< T > squared_distance(const Eigen::Matrix< fvar< T >, R, C > &v1, const Eigen::Matrix< double, R, C > &v2)
Returns the squared distance between the specified vectors of the same dimensions.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
fvar< T > exp(const fvar< T > &x)
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
Eigen::Matrix< typename return_type< T_x, T_s, T_l >::type, Eigen::Dynamic, Eigen::Dynamic > gp_matern52_cov(const std::vector< T_x > &x, const T_s &sigma, const T_l &length_scale)
Returns a Matern 5/2 covariance matrix with one input vector.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.