1 #ifndef STAN_MATH_PRIM_MAT_FUN_GP_EXPONENTIAL_COV_HPP 2 #define STAN_MATH_PRIM_MAT_FUN_GP_EXPONENTIAL_COV_HPP 34 template <
typename T_x,
typename T_s,
typename T_l>
35 inline typename Eigen::Matrix<typename stan::return_type<T_x, T_s, T_l>::type,
36 Eigen::Dynamic, Eigen::Dynamic>
38 const T_l &length_scale) {
43 Eigen::Matrix<typename stan::return_type<T_x, T_s, T_l>::type, Eigen::Dynamic,
49 const char *
function =
"gp_exponential_cov";
50 size_t x_obs_size =
size_of(x[0]);
51 for (
size_t i = 0; i < x_size; ++i)
55 for (
size_t i = 0; i < x_size; ++i)
61 T_s sigma_sq =
square(sigma);
62 T_l neg_inv_l = -1.0 / length_scale;
64 for (
size_t i = 0; i < x_size; ++i) {
66 for (
size_t j = i + 1; j < x_size; ++j) {
67 cov(i, j) = sigma_sq *
exp(neg_inv_l *
distance(x[i], x[j]));
68 cov(j, i) = cov(i, j);
90 template <
typename T_x,
typename T_s,
typename T_l>
91 inline typename Eigen::Matrix<typename stan::return_type<T_x, T_s, T_l>::type,
92 Eigen::Dynamic, Eigen::Dynamic>
94 const T_s &sigma,
const std::vector<T_l> &length_scale) {
99 Eigen::Matrix<typename stan::return_type<T_x, T_s, T_l>::type, Eigen::Dynamic,
105 const char *
function =
"gp_exponential_cov";
106 for (
size_t n = 0; n < x_size; ++n)
112 size_t l_size = length_scale.size();
114 "number of length scales", l_size);
116 std::vector<Eigen::Matrix<typename return_type<T_x, T_l>::type, -1, 1>> x_new
119 T_s sigma_sq =
square(sigma);
120 for (
size_t i = 0; i < x_size; ++i) {
121 cov(i, i) = sigma_sq;
122 for (
size_t j = i + 1; j < x_size; ++j) {
124 cov(i, j) = sigma_sq *
exp(-dist);
125 cov(j, i) = cov(i, j);
151 template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
152 inline typename Eigen::Matrix<
156 const T_s &sigma,
const T_l &length_scale) {
162 Eigen::Matrix<typename stan::return_type<T_x1, T_x2, T_s, T_l>::type,
163 Eigen::Dynamic, Eigen::Dynamic>
164 cov(x1_size, x2_size);
165 if (x1_size == 0 || x2_size == 0)
168 const char *
function =
"gp_exponential_cov";
169 size_t x1_obs_size =
size_of(x1[0]);
170 for (
size_t i = 0; i < x1_size; ++i)
173 for (
size_t i = 0; i < x2_size; ++i)
177 for (
size_t n = 0; n < x1_size; ++n)
179 for (
size_t n = 0; n < x2_size; ++n)
185 T_s sigma_sq =
square(sigma);
186 T_l neg_inv_l = -1.0 / length_scale;
187 for (
size_t i = 0; i < x1_size; ++i) {
188 for (
size_t j = 0; j < x2_size; ++j) {
189 cov(i, j) = sigma_sq *
exp(neg_inv_l *
distance(x1[i], x2[j]));
216 template <
typename T_x1,
typename T_x2,
typename T_s,
typename T_l>
217 inline typename Eigen::Matrix<typename return_type<T_x1, T_x2, T_s, T_l>::type,
218 Eigen::Dynamic, Eigen::Dynamic>
220 const std::vector<Eigen::Matrix<T_x2, -1, 1>> &x2,
221 const T_s &sigma,
const std::vector<T_l> &length_scale) {
227 size_t l_size =
size_of(length_scale);
228 Eigen::Matrix<typename stan::return_type<T_x1, T_x2, T_s, T_l>::type,
229 Eigen::Dynamic, Eigen::Dynamic>
230 cov(x1_size, x2_size);
231 if (x1_size == 0 || x2_size == 0)
234 const char *
function =
"gp_exponential_cov";
235 for (
size_t n = 0; n < x1_size; ++n)
237 for (
size_t n = 0; n < x2_size; ++n)
243 for (
size_t i = 0; i < x1_size; ++i)
245 "number of length scales", l_size);
246 for (
size_t i = 0; i < x2_size; ++i)
248 "number of length scales", l_size);
250 T_s sigma_sq =
square(sigma);
253 std::vector<Eigen::Matrix<typename return_type<T_x1, T_l>::type, -1, 1>>
255 std::vector<Eigen::Matrix<typename return_type<T_x2, T_l>::type, -1, 1>>
258 for (
size_t i = 0; i < x1_size; ++i) {
259 for (
size_t j = 0; j < x2_size; ++j) {
260 cov(i, j) = sigma_sq *
exp(-
distance(x1_new[i], x2_new[j]));
Eigen::Matrix< typename stan::return_type< T_x, T_s, T_l >::type, Eigen::Dynamic, Eigen::Dynamic > gp_exponential_cov(const std::vector< T_x > &x, const T_s &sigma, const T_l &length_scale)
Returns a Matern exponential covariance Matrix.
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...
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
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)