| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/scal/meta/return_type.hpp>
5 #include <stan/math/prim/scal/err/check_size_match.hpp>
6 #include <stan/math/prim/mat/fun/Eigen.hpp>
7 #include <stan/math/prim/mat/fun/squared_distance.hpp>
8 #include <stan/math/prim/scal/err/check_not_nan.hpp>
9 #include <stan/math/prim/scal/err/check_positive.hpp>
10 #include <stan/math/prim/scal/fun/square.hpp>
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
check_not_nan.hpp
stan::math::check_not_nan
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Definition: check_not_nan.hpp:63
check_size_match.hpp
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::cov_exp_quad
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.
Definition: cov_exp_quad.hpp:35
square.hpp
return_type.hpp
stan::math::squared_distance
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.
Definition: squared_distance.hpp:30
stan::math::square
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
squared_distance.hpp
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
Eigen.hpp

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