1 #ifndef STAN_MATH_PRIM_MAT_FUN_GP_MATERN32_COV_HPP 2 #define STAN_MATH_PRIM_MAT_FUN_GP_MATERN32_COV_HPP 39 template <
typename T_x,
typename T_s,
typename T_l>
40 inline typename Eigen::Matrix<typename return_type<T_x, T_s, T_l>::type,
41 Eigen::Dynamic, Eigen::Dynamic>
43 const T_l &length_scale) {
48 Eigen::Matrix<typename return_type<T_x, T_s, T_l>::type, Eigen::Dynamic,
55 const char *
function =
"gp_matern32_cov";
56 size_t x_obs_size =
size_of(x[0]);
57 for (
size_t i = 0; i < x_size; ++i)
61 for (
size_t n = 0; n < x_size; ++n)
67 T_s sigma_sq =
square(sigma);
68 T_l root_3_inv_l =
sqrt(3.0) / length_scale;
69 T_l neg_root_3_inv_l = -1.0 *
sqrt(3.0) / length_scale;
71 for (
size_t i = 0; i < x_size; ++i) {
73 for (
size_t j = i + 1; j < x_size; ++j) {
75 cov(i, j) = sigma_sq * (1.0 + root_3_inv_l * dist)
76 *
exp(neg_root_3_inv_l * dist);
77 cov(j, i) = cov(i, j);
101 template <
typename T_x,
typename T_s,
typename T_l>
102 inline typename Eigen::Matrix<typename return_type<T_x, T_s, T_l>::type,
103 Eigen::Dynamic, Eigen::Dynamic>
105 const T_s &sigma,
const std::vector<T_l> &length_scale) {
109 Eigen::Matrix<typename return_type<T_x, T_s, T_l>::type, Eigen::Dynamic,
115 const char *
function =
"gp_matern32_cov";
116 size_t l_size = length_scale.size();
117 for (
size_t n = 0; n < x_size; ++n)
123 for (
size_t n = 0; n < x_size; ++n)
127 "number of length scales", l_size);
129 T_s sigma_sq =
square(sigma);
130 double root_3 =
sqrt(3.0);
131 double neg_root_3 = -1.0 *
sqrt(3.0);
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 for (
size_t j = i; j < x_size; ++j) {
139 cov(i, j) = sigma_sq * (1.0 + root_3 * dist) *
exp(neg_root_3 * dist);
140 cov(j, i) = cov(i, j);
170 template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
171 inline typename Eigen::Matrix<typename return_type<T_x1, T_x2, T_s, T_l>::type,
172 Eigen::Dynamic, Eigen::Dynamic>
174 const T_s &sigma,
const T_l &length_scale) {
179 Eigen::Matrix<typename return_type<T_x1, T_x2, T_s, T_l>::type,
180 Eigen::Dynamic, Eigen::Dynamic>
181 cov(x1_size, x2_size);
183 if (x1_size == 0 || x2_size == 0)
186 const char *
function =
"gp_matern32_cov";
187 size_t x1_obs_size =
size_of(x1[0]);
188 for (
size_t i = 0; i < x1_size; ++i)
191 for (
size_t i = 0; i < x2_size; ++i)
195 for (
size_t n = 0; n < x1_size; ++n)
197 for (
size_t n = 0; n < x2_size; ++n)
203 T_s sigma_sq =
square(sigma);
204 T_l root_3_inv_l_sq =
sqrt(3.0) / length_scale;
205 T_l neg_root_3_inv_l_sq = -1.0 *
sqrt(3.0) / length_scale;
207 for (
size_t i = 0; i < x1_size; ++i) {
208 for (
size_t j = 0; j < x2_size; ++j) {
210 cov(i, j) = sigma_sq * (1.0 + root_3_inv_l_sq * dist)
211 *
exp(neg_root_3_inv_l_sq * dist);
241 template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
242 inline typename Eigen::Matrix<typename return_type<T_x1, T_x2, T_s, T_l>::type,
243 Eigen::Dynamic, Eigen::Dynamic>
245 const std::vector<Eigen::Matrix<T_x2, -1, 1>> &x2,
246 const T_s &sigma,
const std::vector<T_l> &length_scale) {
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 const char *
function =
"gp_matern_32_cov";
259 for (
size_t n = 0; n < x1_size; ++n)
261 for (
size_t n = 0; n < x2_size; ++n)
267 size_t l_size = length_scale.size();
268 for (
size_t n = 0; n < l_size; ++n)
271 for (
size_t i = 0; i < x1_size; ++i)
273 "number of length scales", l_size);
274 for (
size_t i = 0; i < x2_size; ++i)
276 "number of length scales", l_size);
278 T_s sigma_sq =
square(sigma);
279 double root_3 =
sqrt(3.0);
280 double neg_root_3 = -1.0 *
sqrt(3.0);
282 std::vector<Eigen::Matrix<typename return_type<T_x1, T_l>::type, -1, 1>>
284 std::vector<Eigen::Matrix<typename return_type<T_x2, T_l>::type, -1, 1>>
287 for (
size_t i = 0; i < x1_size; ++i) {
288 for (
size_t j = 0; j < x2_size; ++j) {
291 cov(i, j) = sigma_sq * (1.0 + root_3 * dist) *
exp(neg_root_3 * 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...
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.
size_t size_of(const T &x)
Returns the size of the provided vector or the constant 1 if the input argument is not a vector...
Eigen::Matrix< typename return_type< T_x, T_s, T_l >::type, Eigen::Dynamic, Eigen::Dynamic > gp_matern32_cov(const std::vector< T_x > &x, const T_s &sigma, const T_l &length_scale)
Returns a Matern 3/2 covariance matrix.
boost::math::tools::promote_args< T1, T2 >::type distance(const Eigen::Matrix< T1, R1, C1 > &v1, const Eigen::Matrix< T2, R2, C2 > &v2)
Returns the distance between the specified vectors.
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)