1 #ifndef STAN_MATH_REV_MAT_FUN_GP_EXP_QUAD_COV_HPP 2 #define STAN_MATH_REV_MAT_FUN_GP_EXP_QUAD_COV_HPP 14 #include <type_traits> 32 template <
typename T_x,
typename T_sigma,
typename T_l>
65 const T_l &length_scale)
68 size_ltri_(size_ * (size_ - 1) / 2),
71 sigma_sq_d_(sigma_d_ * sigma_d_),
74 l_vari_(length_scale.vi_),
75 sigma_vari_(sigma.vi_),
80 double inv_half_sq_l_d = 0.5 / (l_d_ *
l_d_);
82 for (
size_t j = 0; j < size_ - 1; ++j) {
83 for (
size_t i = j + 1; i <
size_; ++i) {
86 cov_lower_[pos] =
new vari(
87 sigma_sq_d_ *
std::exp(-dist_sq * 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>
158 const T_l &length_scale)
161 size_ltri_(size_ * (size_ - 1) / 2),
164 sigma_sq_d_(sigma_d_ * sigma_d_),
167 l_vari_(length_scale.vi_),
172 double inv_half_sq_l_d = 0.5 / (l_d_ *
l_d_);
174 for (
size_t j = 0; j < size_ - 1; ++j) {
175 for (
size_t i = j + 1; i <
size_; ++i) {
177 dist_[pos] = dist_sq;
178 cov_lower_[pos] =
new vari(
179 sigma_sq_d_ *
std::exp(-dist_sq * inv_half_sq_l_d),
false);
183 for (
size_t i = 0; i <
size_; ++i)
184 cov_diag_[i] =
new vari(sigma_sq_d_,
false);
191 vari *el_low = cov_lower_[i];
192 adjl += el_low->
adj_ * el_low->
val_ * dist_[i];
194 l_vari_->
adj_ += adjl / (l_d_ * l_d_ *
l_d_);
209 template <
typename T_x>
210 inline typename std::enable_if<
211 std::is_same<typename scalar_type<T_x>::type,
double>::value,
212 Eigen::Matrix<
var, -1, -1>>::type
214 const var &length_scale) {
217 size_t x_size = x.size();
218 for (
size_t i = 0; i < x_size; ++i)
221 Eigen::Matrix<
var, -1, -1> cov(x_size, x_size);
229 for (
size_t j = 0; j < x_size - 1; ++j) {
230 for (
size_t i = (j + 1); i < x_size; ++i) {
231 cov.coeffRef(i, j).vi_ = baseVari->
cov_lower_[pos];
232 cov.coeffRef(j, i).vi_ = cov.coeffRef(i, j).vi_;
235 cov.coeffRef(j, j).vi_ = baseVari->
cov_diag_[j];
237 cov.coeffRef(x_size - 1, x_size - 1).vi_ = baseVari->
cov_diag_[x_size - 1];
252 template <
typename T_x>
253 inline typename std::enable_if<
254 std::is_same<typename scalar_type<T_x>::type,
double>::value,
255 Eigen::Matrix<
var, -1, -1>>::type
257 const var &length_scale) {
260 size_t x_size = x.size();
261 for (
size_t i = 0; i < x_size; ++i)
264 Eigen::Matrix<
var, -1, -1> cov(x_size, x_size);
272 for (
size_t j = 0; j < x_size - 1; ++j) {
273 for (
size_t i = (j + 1); i < x_size; ++i) {
274 cov.coeffRef(i, j).vi_ = baseVari->
cov_lower_[pos];
275 cov.coeffRef(j, i).vi_ = cov.coeffRef(i, j).vi_;
278 cov.coeffRef(j, j).vi_ = baseVari->
cov_diag_[j];
280 cov.coeffRef(x_size - 1, x_size - 1).vi_ = baseVari->
cov_diag_[x_size - 1];
gp_exp_quad_cov_vari(const std::vector< T_x > &x, double sigma, const T_l &length_scale)
Constructor for gp_exp_quad_cov.
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.
gp_exp_quad_cov_vari(const std::vector< T_x > &x, const T_sigma &sigma, const T_l &length_scale)
Constructor for gp_exp_quad_cov.
const double val_
The value of this variable.
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
This is a subclass of the vari class for precomputed gradients of gp_exp_quad_cov.
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.
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
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...
This struct always provides access to the autodiff stack using the singleton pattern.