1 #ifndef STAN_MATH_REV_MAT_FUN_COV_EXP_QUAD_HPP 2 #define STAN_MATH_REV_MAT_FUN_COV_EXP_QUAD_HPP 13 #include <boost/math/tools/promotion.hpp> 14 #include <boost/utility/enable_if.hpp> 15 #include <boost/type_traits.hpp> 35 template <
typename T_x,
typename T_sigma,
typename T_l>
72 size_ltri_(size_ * (size_ - 1) / 2),
74 sigma_sq_d_(sigma_d_ * sigma_d_),
76 l_vari_(l.vi_), sigma_vari_(sigma.vi_),
79 double inv_half_sq_l_d = 0.5 / (l_d_ *
l_d_);
81 for (
size_t j = 0; j < size_ - 1; ++j) {
82 for (
size_t i = j + 1; i <
size_; ++i) {
85 cov_lower_[pos] =
new vari(sigma_sq_d_
87 * inv_half_sq_l_d),
false);
91 for (
size_t i = 0; i <
size_; ++i)
92 cov_diag_[i] =
new vari(sigma_sq_d_,
false);
100 vari* el_low = cov_lower_[i];
101 double prod_add = el_low->
adj_ * el_low->
val_;
102 adjl += prod_add * dist_[i];
103 adjsigma += prod_add;
105 for (
size_t i = 0; i <
size_; ++i) {
106 vari* el = cov_diag_[i];
109 l_vari_->
adj_ += adjl / (l_d_ * l_d_ *
l_d_);
126 template <
typename T_x,
typename T_l>
162 size_ltri_(size_ * (size_ - 1) / 2),
164 sigma_sq_d_(sigma_d_ * sigma_d_),
169 double inv_half_sq_l_d = 0.5 / (l_d_ *
l_d_);
171 for (
size_t j = 0; j < size_ - 1; ++j) {
172 for (
size_t i = j + 1; i <
size_; ++i) {
174 dist_[pos] = dist_sq;
175 cov_lower_[pos] =
new vari(sigma_sq_d_
177 * inv_half_sq_l_d),
false);
181 for (
size_t i = 0; i <
size_; ++i)
182 cov_diag_[i] =
new vari(sigma_sq_d_,
false);
189 vari* el_low = cov_lower_[i];
190 adjl += el_low->
adj_ * el_low->
val_ * dist_[i];
192 l_vari_->
adj_ += adjl / (l_d_ * l_d_ *
l_d_);
207 template <
typename T_x>
209 boost::enable_if_c<boost::is_same<typename scalar_type<T_x>::type,
211 Eigen::Matrix<
var, -1, -1> >::type
217 size_t x_size = x.size();
218 for (
size_t i = 0; i < x_size; ++i)
221 Eigen::Matrix<
var, -1, -1>
230 for (
size_t j = 0; j < x_size - 1; ++j) {
231 for (
size_t i = (j + 1); i < x_size; ++i) {
232 cov.coeffRef(i, j).vi_ = baseVari->
cov_lower_[pos];
233 cov.coeffRef(j, i).vi_ = cov.coeffRef(i, j).vi_;
236 cov.coeffRef(j, j).vi_ = baseVari->
cov_diag_[j];
238 cov.coeffRef(x_size - 1, x_size - 1).vi_
254 template <
typename T_x>
256 boost::enable_if_c<boost::is_same<typename scalar_type<T_x>::type,
258 Eigen::Matrix<
var, -1, -1> >::type
264 size_t x_size = x.size();
265 for (
size_t i = 0; i < x_size; ++i)
268 Eigen::Matrix<
var, -1, -1>
277 for (
size_t j = 0; j < x_size - 1; ++j) {
278 for (
size_t i = (j + 1); i < x_size; ++i) {
279 cov.coeffRef(i, j).vi_ = baseVari->
cov_lower_[pos];
280 cov.coeffRef(j, i).vi_ = cov.coeffRef(i, j).vi_;
283 cov.coeffRef(j, j).vi_ = baseVari->
cov_diag_[j];
285 cov.coeffRef(x_size - 1, x_size - 1).vi_
This is a subclass of the vari class for precomputed gradients of cov_exp_quad.
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 > cov_exp_quad(const std::vector< T_x > &x, const T_sigma &sigma, const T_l &l)
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)
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
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)
Constructor for cov_exp_quad.
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)
Constructor for cov_exp_quad.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...