Stan Math Library  2.15.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 
12 #include <vector>
13 #include <cmath>
14 
15 namespace stan {
16  namespace math {
17 
33  template<typename T_x, typename T_sigma, typename T_l>
34  inline typename
35  Eigen::Matrix<typename stan::return_type<T_x, T_sigma, T_l>::type,
36  Eigen::Dynamic, Eigen::Dynamic>
37  cov_exp_quad(const std::vector<T_x>& x,
38  const T_sigma& sigma,
39  const T_l& l) {
40  using std::exp;
41  check_positive("cov_exp_quad", "marginal variance", sigma);
42  check_positive("cov_exp_quad", "length-scale", l);
43  for (size_t n = 0; n < x.size(); ++n)
44  check_not_nan("cov_exp_quad", "x", x[n]);
45 
46  Eigen::Matrix<typename stan::return_type<T_x, T_sigma, T_l>::type,
47  Eigen::Dynamic, Eigen::Dynamic>
48  cov(x.size(), x.size());
49 
50  int x_size = x.size();
51  if (x_size == 0)
52  return cov;
53 
54  T_sigma sigma_sq = square(sigma);
55  T_l neg_half_inv_l_sq = - 0.5 / square(l);
56 
57  for (int j = 0; j < (x_size - 1); ++j) {
58  cov(j, j) = sigma_sq;
59  for (int i = j + 1; i < x_size; ++i) {
60  cov(i, j) = sigma_sq * exp(squared_distance(x[i], x[j])
61  * neg_half_inv_l_sq);
62  cov(j, i) = cov(i, j);
63  }
64  }
65  cov(x_size - 1, x_size - 1) = sigma_sq;
66  return cov;
67  }
68 
85  template<typename T_x1, typename T_x2, typename T_sigma, typename T_l>
86  inline typename
87  Eigen::Matrix<typename stan::return_type<T_x1, T_x2, T_sigma, T_l>::type,
88  Eigen::Dynamic, Eigen::Dynamic>
89  cov_exp_quad(const std::vector<T_x1>& x1,
90  const std::vector<T_x2>& x2,
91  const T_sigma& sigma,
92  const T_l& l) {
93  using std::exp;
94  check_positive("cov_exp_quad", "marginal variance", sigma);
95  check_positive("cov_exp_quad", "length-scale", l);
96  for (size_t n = 0; n < x1.size(); ++n)
97  check_not_nan("cov_exp_quad", "x1", x1[n]);
98  for (size_t n = 0; n < x2.size(); ++n)
99  check_not_nan("cov_exp_quad", "x2", x2[n]);
100 
101  Eigen::Matrix<typename stan::return_type<T_x1, T_x2, T_sigma, T_l>::type,
102  Eigen::Dynamic, Eigen::Dynamic>
103  cov(x1.size(), x2.size());
104  if (x1.size() == 0 || x2.size() == 0)
105  return cov;
106 
107  T_sigma sigma_sq = square(sigma);
108  T_l neg_half_inv_l_sq = - 0.5 / square(l);
109 
110  for (size_t i = 0; i < x1.size(); ++i) {
111  for (size_t j = 0; j < x2.size(); ++j) {
112  cov(i, j) = sigma_sq * exp(squared_distance(x1[i], x2[j])
113  * neg_half_inv_l_sq);
114  }
115  }
116  return cov;
117  }
118 
119  }
120 }
121 #endif
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, const T_sigma &sigma, const T_l &l)
Returns a squared exponential kernel.
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:14
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.
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
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.

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