Stan Math Library  2.20.0
reverse mode automatic differentiation
multi_gp_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_GP_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_GP_LPDF_HPP
3 
15 
16 namespace stan {
17 namespace math {
37 template <bool propto, typename T_y, typename T_covar, typename T_w>
38 typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
40  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
41  const Eigen::Matrix<T_covar, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
42  const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
43  static const char* function = "multi_gp_lpdf";
44  typedef
45  typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type T_lp;
46  T_lp lp(0.0);
47 
48  check_positive(function, "Kernel rows", Sigma.rows());
49  check_finite(function, "Kernel", Sigma);
50  check_symmetric(function, "Kernel", Sigma);
51 
53  check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
54 
55  check_size_match(function, "Size of random variable (rows y)", y.rows(),
56  "Size of kernel scales (w)", w.size());
57  check_size_match(function, "Size of random variable", y.cols(),
58  "rows of covariance parameter", Sigma.rows());
59  check_positive_finite(function, "Kernel scales", w);
60  check_finite(function, "Random variable", y);
61 
62  if (y.rows() == 0)
63  return lp;
64 
66  lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols();
67  }
68 
70  lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * y.rows();
71  }
72 
74  lp += (0.5 * y.cols()) * sum(log(w));
75  }
76 
78  Eigen::Matrix<T_w, Eigen::Dynamic, Eigen::Dynamic> w_mat(w.asDiagonal());
79  Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic> yT(y.transpose());
80  lp -= 0.5 * trace_gen_inv_quad_form_ldlt(w_mat, ldlt_Sigma, yT);
81  }
82 
83  return lp;
84 }
85 
86 template <typename T_y, typename T_covar, typename T_w>
87 inline typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
89  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
90  const Eigen::Matrix<T_covar, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
91  const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
92  return multi_gp_lpdf<false>(y, Sigma, w);
93 }
94 
95 } // namespace math
96 } // namespace stan
97 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< T_y, T_covar, T_w >::type multi_gp_lpdf(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const Eigen::Matrix< T_covar, Eigen::Dynamic, Eigen::Dynamic > &Sigma, const Eigen::Matrix< T_w, Eigen::Dynamic, 1 > &w)
The log of a multivariate Gaussian Process for the given y, Sigma, and w.
void check_ldlt_factor(const char *function, const char *name, LDLT_factor< T, R, C > &A)
Raise domain error if the specified LDLT factor is invalid.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
std::enable_if< !stan::is_var< T1 >::value &&!stan::is_var< T2 >::value &&!stan::is_var< T3 >::value, typename boost::math::tools::promote_args< T1, T2, T3 >::type >::type trace_gen_inv_quad_form_ldlt(const Eigen::Matrix< T1, R1, C1 > &D, const LDLT_factor< T2, R2, C2 > &A, const Eigen::Matrix< T3, R3, C3 > &B)
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.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
LDLT_factor is a thin wrapper on Eigen::LDLT to allow for reusing factorizations and efficient autodi...
Definition: LDLT_factor.hpp:63
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:156
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_symmetric(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is symmetric.
T log_determinant_ldlt(LDLT_factor< T, R, C > &A)

     [ Stan Home Page ] © 2011–2018, Stan Development Team.