1 #ifndef STAN_MATH_REV_MAT_FUN_COV_EXP_QUAD_HPP 2 #define STAN_MATH_REV_MAT_FUN_COV_EXP_QUAD_HPP 11 #include <type_traits> 21 template <
typename T_x,
typename T_sigma,
typename T_l>
42 size_ltri_(size_ * (size_ - 1) / 2),
45 sigma_sq_d_(sigma_d_ * sigma_d_),
49 sigma_vari_(sigma.vi_),
54 double inv_half_sq_l_d = 0.5 / (l_d_ *
l_d_);
56 for (
size_t j = 0; j < size_ - 1; ++j) {
57 for (
size_t i = j + 1; i <
size_; ++i) {
60 cov_lower_[pos] =
new vari(
61 sigma_sq_d_ *
std::exp(-dist_sq * inv_half_sq_l_d),
false);
65 for (
size_t i = 0; i <
size_; ++i)
66 cov_diag_[i] =
new vari(sigma_sq_d_,
false);
74 vari* el_low = cov_lower_[i];
75 double prod_add = el_low->
adj_ * el_low->
val_;
76 adjl += prod_add * dist_[i];
79 for (
size_t i = 0; i <
size_; ++i) {
80 vari* el = cov_diag_[i];
83 l_vari_->
adj_ += adjl / (l_d_ * l_d_ *
l_d_);
91 template <
typename T_x,
typename T_l>
110 size_ltri_(size_ * (size_ - 1) / 2),
113 sigma_sq_d_(sigma_d_ * sigma_d_),
121 double inv_half_sq_l_d = 0.5 / (l_d_ *
l_d_);
123 for (
size_t j = 0; j < size_ - 1; ++j) {
124 for (
size_t i = j + 1; i <
size_; ++i) {
126 dist_[pos] = dist_sq;
127 cov_lower_[pos] =
new vari(
128 sigma_sq_d_ *
std::exp(-dist_sq * inv_half_sq_l_d),
false);
132 for (
size_t i = 0; i <
size_; ++i)
133 cov_diag_[i] =
new vari(sigma_sq_d_,
false);
140 vari* el_low = cov_lower_[i];
141 adjl += el_low->
adj_ * el_low->
val_ * dist_[i];
143 l_vari_->
adj_ += adjl / (l_d_ * l_d_ *
l_d_);
150 template <
typename T_x>
151 inline typename std::enable_if<
152 std::is_same<typename scalar_type<T_x>::type,
double>::value,
153 Eigen::Matrix<
var, -1, -1> >::type
154 cov_exp_quad(
const std::vector<T_x>& x,
const var& sigma,
const var& l) {
161 template <
typename T_x>
162 inline typename std::enable_if<
163 std::is_same<typename scalar_type<T_x>::type,
double>::value,
164 Eigen::Matrix<
var, -1, -1> >::type
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 &length_scale)
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Eigen::Matrix< typename stan::return_type< T_x, T_sigma, T_l >::type, Eigen::Dynamic, Eigen::Dynamic > gp_exp_quad_cov(const std::vector< T_x > &x, const T_sigma &sigma, const T_l &length_scale)
Returns a squared exponential kernel.
The variable implementation base class.
const double val_
The value of this variable.
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)
vari(double x)
Construct a variable implementation from a value.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
cov_exp_quad_vari(const std::vector< T_x > &x, const T_sigma &sigma, const T_l &l)
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
cov_exp_quad_vari(const std::vector< T_x > &x, double sigma, const T_l &l)
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
This struct always provides access to the autodiff stack using the singleton pattern.