Automatic Differentiation
 
Loading...
Searching...
No Matches
multi_gp_cholesky_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_MULTI_GP_CHOLESKY_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_MULTI_GP_CHOLESKY_LPDF_HPP
3
12
13namespace stan {
14namespace math {
15
38template <bool propto, typename T_y, typename T_covar, typename T_w,
39 require_all_eigen_matrix_dynamic_t<T_y, T_covar>* = nullptr,
40 require_eigen_col_vector_t<T_w>* = nullptr>
42 const T_covar& L,
43 const T_w& w) {
45 static constexpr const char* function = "multi_gp_cholesky_lpdf";
46 check_size_match(function, "Size of random variable (rows y)", y.rows(),
47 "Size of kernel scales (w)", w.size());
48 check_size_match(function, "Size of random variable", y.cols(),
49 "rows of covariance parameter", L.rows());
50
51 const auto& y_ref = to_ref(y);
52 check_finite(function, "Random variable", y_ref);
53 const auto& L_ref = to_ref(L);
54 check_cholesky_factor(function, "Cholesky decomposition of kernel matrix",
55 L_ref);
56 const auto& w_ref = to_ref(w);
57 check_positive_finite(function, "Kernel scales", w_ref);
58
59 if (y.rows() == 0) {
60 return 0;
61 }
62
63 T_lp lp(0);
65 lp += NEG_LOG_SQRT_TWO_PI * y.size();
66 }
67
69 lp -= sum(log(L_ref.diagonal())) * y.rows();
70 }
71
73 lp += 0.5 * y.cols() * sum(log(w_ref));
74 }
75
77 T_lp sum_lp_vec(0);
78 for (int i = 0; i < y.rows(); i++) {
79 sum_lp_vec
80 += w_ref.coeff(i)
81 * dot_self(mdivide_left_tri_low(L_ref, y_ref.row(i).transpose()));
82 }
83 lp -= 0.5 * sum_lp_vec;
84 }
85
86 return lp;
87}
88
89template <typename T_y, typename T_covar, typename T_w>
91 const T_covar& L,
92 const T_w& w) {
93 return multi_gp_cholesky_lpdf<false>(y, L, w);
94}
95
96} // namespace math
97} // namespace stan
98#endif
return_type_t< T_y, T_covar, T_w > multi_gp_cholesky_lpdf(const T_y &y, const T_covar &L, const T_w &w)
The log of a multivariate Gaussian Process for the given y, w, and a Cholesky factor L of the kernel ...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
const double NEG_LOG_SQRT_TWO_PI
The value of minus the natural logarithm of the square root of , .
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
void check_cholesky_factor(const char *function, const char *name, const Mat &y)
Throw an exception if the specified matrix is not a valid Cholesky factor.
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
Eigen::Matrix< value_type_t< T1 >, T1::RowsAtCompileTime, T2::ColsAtCompileTime > mdivide_left_tri_low(const T1 &A, const T2 &b)
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...