1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_GP_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_GP_LPDF_HPP
3 
4 #include <stan/math/prim/mat/err/check_ldlt_factor.hpp>
5 #include <stan/math/prim/scal/err/check_size_match.hpp>
6 #include <stan/math/prim/mat/err/check_symmetric.hpp>
7 #include <stan/math/prim/scal/err/check_finite.hpp>
8 #include <stan/math/prim/scal/err/check_not_nan.hpp>
9 #include <stan/math/prim/scal/err/check_positive.hpp>
10 #include <stan/math/prim/scal/err/check_positive_finite.hpp>
11 #include <stan/math/prim/mat/fun/log.hpp>
12 #include <stan/math/prim/mat/fun/log_determinant_ldlt.hpp>
13 #include <stan/math/prim/mat/fun/multiply.hpp>
14 #include <stan/math/prim/mat/fun/sum.hpp>
15 #include <stan/math/prim/mat/fun/trace_gen_inv_quad_form_ldlt.hpp>
16 #include <stan/math/prim/scal/fun/constants.hpp>
17 #include <stan/math/prim/scal/meta/include_summand.hpp>
18 
19 namespace stan {
20 namespace math {
21 /**
22  * The log of a multivariate Gaussian Process for the given y, Sigma, and
23  * w.  y is a dxN matrix, where each column is a different observation and each
24  * row is a different output dimension.  The Gaussian Process is assumed to
25  * have a scaled kernel matrix with a different scale for each output dimension.
26  * This distribution is equivalent to:
27  *    for (i in 1:d) row(y, i) ~ multi_normal(0, (1/w[i])*Sigma).
28  *
29  * @param y A dxN matrix
30  * @param Sigma The NxN kernel matrix
31  * @param w A d-dimensional vector of positve inverse scale parameters for each
32  * output.
33  * @return The log of the multivariate GP density.
34  * @throw std::domain_error if Sigma is not square, not symmetric,
35  * or not semi-positive definite.
36  * @tparam T_y Type of scalar.
37  * @tparam T_covar Type of kernel.
38  * @tparam T_w Type of weight.
39  */
40 template <bool propto, typename T_y, typename T_covar, typename T_w>
41 typename 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)42 multi_gp_lpdf(
43     const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
44     const Eigen::Matrix<T_covar, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
45     const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
46   static const char* function = "multi_gp_lpdf";
47   typedef
48       typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type T_lp;
49   T_lp lp(0.0);
50 
51   check_positive(function, "Kernel rows", Sigma.rows());
52   check_finite(function, "Kernel", Sigma);
53   check_symmetric(function, "Kernel", Sigma);
54 
55   LDLT_factor<T_covar, Eigen::Dynamic, Eigen::Dynamic> ldlt_Sigma(Sigma);
56   check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
57 
58   check_size_match(function, "Size of random variable (rows y)", y.rows(),
59                    "Size of kernel scales (w)", w.size());
60   check_size_match(function, "Size of random variable", y.cols(),
61                    "rows of covariance parameter", Sigma.rows());
62   check_positive_finite(function, "Kernel scales", w);
63   check_finite(function, "Random variable", y);
64 
65   if (y.rows() == 0)
66     return lp;
67 
68   if (include_summand<propto>::value) {
69     lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols();
70   }
71 
72   if (include_summand<propto, T_covar>::value) {
73     lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * y.rows();
74   }
75 
76   if (include_summand<propto, T_w>::value) {
77     lp += (0.5 * y.cols()) * sum(log(w));
78   }
79 
80   if (include_summand<propto, T_y, T_w, T_covar>::value) {
81     Eigen::Matrix<T_w, Eigen::Dynamic, Eigen::Dynamic> w_mat(w.asDiagonal());
82     Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic> yT(y.transpose());
83     lp -= 0.5 * trace_gen_inv_quad_form_ldlt(w_mat, ldlt_Sigma, yT);
84   }
85 
86   return lp;
87 }
88 
89 template <typename T_y, typename T_covar, typename T_w>
90 inline typename 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)91 multi_gp_lpdf(
92     const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
93     const Eigen::Matrix<T_covar, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
94     const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
95   return multi_gp_lpdf<false>(y, Sigma, w);
96 }
97 
98 }  // namespace math
99 }  // namespace stan
100 #endif
101