Automatic Differentiation
 
Loading...
Searching...
No Matches
multi_gp_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_MULTI_GP_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_MULTI_GP_LPDF_HPP
3
10
11namespace stan {
12namespace math {
13
33template <bool propto, typename T_y, typename T_covar, typename T_w,
34 require_all_matrix_t<T_y, T_covar>* = nullptr,
35 require_col_vector_t<T_w>* = nullptr>
37 const T_covar& Sigma,
38 const T_w& w) {
40 static constexpr const char* function = "multi_gp_lpdf";
41 check_size_match(function, "Size of random variable (rows y)", y.rows(),
42 "Size of kernel scales (w)", w.size());
43 check_size_match(function, "Size of random variable", y.cols(),
44 "rows of covariance parameter", Sigma.rows());
45 check_positive(function, "Kernel rows", Sigma.rows());
46
47 const auto& Sigma_ref = to_ref(Sigma);
48 const auto& w_ref = to_ref(w);
49 const auto& y_t_ref = to_ref(y.transpose());
50
51 check_finite(function, "Kernel", Sigma_ref);
52 check_symmetric(function, "Kernel", Sigma_ref);
53 check_positive_finite(function, "Kernel scales", w_ref);
54 check_finite(function, "Random variable", y_t_ref);
55
56 auto ldlt_Sigma = make_ldlt_factor(Sigma_ref);
57 check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
58
59 T_lp lp(0.0);
60
61 if (y.rows() == 0) {
62 return lp;
63 }
64
66 lp += NEG_LOG_SQRT_TWO_PI * y.size();
67 }
68
70 lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * y.rows();
71 }
72
74 lp += (0.5 * y.cols()) * sum(log(w_ref));
75 }
76
78 lp -= 0.5 * trace_gen_inv_quad_form_ldlt(w_ref, ldlt_Sigma, y_t_ref);
79 }
80
81 return lp;
82}
83
84template <typename T_y, typename T_covar, typename T_w>
86 const T_covar& Sigma,
87 const T_w& w) {
88 return multi_gp_lpdf<false>(y, Sigma, w);
89}
90
91} // namespace math
92} // namespace stan
93#endif
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
return_type_t< T_y, T_covar, T_w > multi_gp_lpdf(const T_y &y, const T_covar &Sigma, const T_w &w)
The log of a multivariate Gaussian Process for the given y, Sigma, and w.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
value_type_t< T > log_determinant_ldlt(LDLT_factor< T > &A)
Returns log(abs(det(A))) given a LDLT_factor of A.
auto make_ldlt_factor(const T &A)
Make an LDLT_factor with matrix type plain_type_t<T>
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
const double NEG_LOG_SQRT_TWO_PI
The value of minus the natural logarithm of the square root of , .
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
return_type_t< EigMat1, T2, EigMat3 > trace_gen_inv_quad_form_ldlt(const EigMat1 &D, LDLT_factor< T2 > &A, const EigMat3 &B)
Compute the trace of an inverse quadratic form.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_ldlt_factor(const char *function, const char *name, LDLT_factor< T > &A)
Raise domain error if the specified LDLT factor is invalid.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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.
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 ...
Definition fvar.hpp:9
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...