Stan Math Library  2.11.0
reverse mode automatic differentiation
cov_exp_quad.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_COV_EXP_QUAD_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_COV_EXP_QUAD_HPP
3 
11 #include <vector>
12 
13 namespace stan {
14  namespace math {
15 
31  template<typename T_x, typename T_sigma, typename T_l>
32  inline typename
33  Eigen::Matrix<typename stan::return_type<T_x, T_sigma, T_l>::type,
34  Eigen::Dynamic, Eigen::Dynamic>
35  cov_exp_quad(const std::vector<T_x>& x,
36  T_sigma& sigma,
37  T_l& l) {
38  using std::exp;
39  stan::math::check_positive("cov_exp_quad", "sigma", sigma);
40  stan::math::check_positive("cov_exp_quad", "l", l);
41  for (size_t n = 0; n < x.size(); n++)
42  stan::math::check_not_nan("cov_exp_quad", "x", x[n]);
43 
44  Eigen::Matrix<typename stan::return_type<T_x, T_sigma, T_l>::type,
45  Eigen::Dynamic, Eigen::Dynamic>
46  cov(x.size(), x.size());
47 
48  if (x.size() == 0)
49  return cov;
50 
51  T_sigma sigma_sq = square(sigma);
52  T_l neg_half_inv_l_sq = - 0.5 / square(l);
53 
54  for (size_t i = 0; i < x.size(); ++i) {
55  cov(i, i) = sigma_sq;
56  for (size_t j = i + 1; j < x.size(); ++j) {
57  cov(i, j) = sigma_sq * exp(squared_distance(x[i], x[j])
58  * neg_half_inv_l_sq);
59  cov(j, i) = cov(i, j);
60  }
61  }
62  return cov;
63  }
64 
81  template<typename T_x1, typename T_x2, typename T_sigma, typename T_l>
82  inline typename
83  Eigen::Matrix<typename stan::return_type<T_x1, T_x2, T_sigma, T_l>::type,
84  Eigen::Dynamic, Eigen::Dynamic>
85  cov_exp_quad(const std::vector<T_x1>& x1,
86  const std::vector<T_x2>& x2,
87  T_sigma& sigma,
88  T_l& l) {
89  using std::exp;
90  stan::math::check_positive("cov_exp_quad", "sigma", sigma);
91  stan::math::check_positive("cov_exp_quad", "l", l);
92  for (size_t n = 0; n < x1.size(); n++)
93  stan::math::check_not_nan("cov_exp_quad", "x1", x1[n]);
94  for (size_t n = 0; n < x2.size(); n++)
95  stan::math::check_not_nan("cov_exp_quad", "x2", x2[n]);
96 
97  Eigen::Matrix<typename stan::return_type<T_x1, T_x2, T_sigma, T_l>::type,
98  Eigen::Dynamic, Eigen::Dynamic>
99  cov(x1.size(), x2.size());
100  if (x1.size() == 0 || x2.size() == 0)
101  return cov;
102 
103  T_sigma sigma_sq = square(sigma);
104  T_l neg_half_inv_l_sq = - 0.5 / square(l);
105 
106  for (size_t i = 0; i < x1.size(); ++i) {
107  for (size_t j = 0; j < x2.size(); ++j) {
108  cov(i, j) = sigma_sq * exp(squared_distance(x1[i], x2[j])
109  * neg_half_inv_l_sq);
110  }
111  }
112  return cov;
113  }
114  }
115 
116 }
117 #endif
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Eigen::Matrix< typename stan::return_type< T_x, T_sigma, T_l >::type, Eigen::Dynamic, Eigen::Dynamic > cov_exp_quad(const std::vector< T_x > &x, T_sigma &sigma, T_l &l)
Returns a squared exponential kernel.
stan::math::fvar< T > squared_distance(const Eigen::Matrix< stan::math::fvar< T >, R, C > &v1, const Eigen::Matrix< double, R, C > &v2)
Returns the squared distance between the specified vectors of the same dimensions.
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.

     [ Stan Home Page ] © 2011–2016, Stan Development Team.