Stan Math Library: stan/math/prim/mat/prob/multi_gp_log.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

multi_gp_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_GP_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_GP_LOG_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
12 #include <stan/math/prim/mat/fun/log.hpp>
13 #include <stan/math/prim/mat/fun/log_determinant_ldlt.hpp>
14 #include <stan/math/prim/mat/fun/multiply.hpp>
15 #include <stan/math/prim/mat/fun/sum.hpp>
16 #include <stan/math/prim/mat/fun/trace_gen_inv_quad_form_ldlt.hpp>
17 #include <stan/math/prim/scal/fun/constants.hpp>
18 #include <stan/math/prim/scal/meta/include_summand.hpp>
19
20 namespace stan {
21 namespace math {
22 // MultiGP(y|Sigma, w) [y.rows() = w.size(), y.cols() = Sigma.rows();
23 // Sigma symmetric, non-negative, definite]
42 template <bool propto,
43 typename T_y, typename T_covar, typename T_w>
44 typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
45 multi_gp_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
46 const Eigen::Matrix
47 <T_covar, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
48 const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
49 static const char* function("stan::math::multi_gp_log");
50 typedef typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
51 T_lp;
52 T_lp lp(0.0);
53
54 using stan::math::sum;
55 using stan::math::log;
56 using stan::math::LDLT_factor;
57 using stan::math::log_determinant_ldlt;
58 using stan::math::trace_gen_inv_quad_form_ldlt;
59
60 using stan::math::check_size_match;
61 using stan::math::check_positive_finite;
62 using stan::math::check_positive;
63 using stan::math::check_finite;
64 using stan::math::check_symmetric;
65 using stan::math::check_ldlt_factor;
66 using stan::math::check_not_nan;
67
68 check_positive(function, "Kernel rows", Sigma.rows());
69 check_finite(function, "Kernel", Sigma);
70 check_symmetric(function, "Kernel", Sigma);
71
72 LDLT_factor<T_covar, Eigen::Dynamic, Eigen::Dynamic> ldlt_Sigma(Sigma);
73 check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
74
75 check_size_match(function,
76 "Size of random variable (rows y)", y.rows(),
77 "Size of kernel scales (w)", w.size());
78 check_size_match(function,
79 "Size of random variable", y.cols(),
80 "rows of covariance parameter", Sigma.rows());
81 check_positive_finite(function, "Kernel scales", w);
82 check_finite(function, "Random variable", y);
83
84 if (y.rows() == 0)
85 return lp;
86
87 if (include_summand<propto>::value) {
88 lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols();
89 }
90
91 if (include_summand<propto, T_covar>::value) {
92 lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * y.rows();
93 }
94
95 if (include_summand<propto, T_w>::value) {
96 lp += (0.5 * y.cols()) * sum(log(w));
97 }
98
99 if (include_summand<propto, T_y, T_w, T_covar>::value) {
100 Eigen::Matrix<T_w, Eigen::Dynamic, Eigen::Dynamic>
101 w_mat(w.asDiagonal());
102 Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic> yT(y.transpose());
103 lp -= 0.5 * trace_gen_inv_quad_form_ldlt(w_mat, ldlt_Sigma, yT);
104 }
105
106 return lp;
107 }
108
109 template <typename T_y, typename T_covar, typename T_w>
110 inline
111 typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
112 multi_gp_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
113 const Eigen::Matrix<T_covar, Eigen::Dynamic, Eigen::Dynamic>&
114 Sigma,
115 const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
116 return multi_gp_log<false>(y, Sigma, w);
117 }
118 }
119 }
120
121 #endif
check_not_nan.hpp
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
multiply.hpp
stan::math::check_not_nan
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Definition: check_not_nan.hpp:63
check_size_match.hpp
check_positive_finite.hpp
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::trace_gen_inv_quad_form_ldlt
boost::enable_if_c<!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 stan::math::LDLT_factor< T2, R2, C2 > &A, const Eigen::Matrix< T3, R3, C3 > &B)
Definition: trace_gen_inv_quad_form_ldlt.hpp:30
check_symmetric.hpp
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:36
log.hpp
include_summand.hpp
check_ldlt_factor.hpp
stan::math::LDLT_factor
Definition: LDLT_factor.hpp:18
trace_gen_inv_quad_form_ldlt.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
stan::math::check_size_match
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
Definition: check_size_match.hpp:30
stan::math::multi_gp_log
boost::math::tools::promote_args< T_y, T_covar, T_w >::type multi_gp_log(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.
Definition: multi_gp_log.hpp:45
stan::math::NEG_LOG_SQRT_TWO_PI
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:184
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:62
constants.hpp
check_finite.hpp
sum.hpp
stan::math::check_symmetric
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is symmetric.
Definition: check_symmetric.hpp:37
stan::math::log_determinant_ldlt
T log_determinant_ldlt(stan::math::LDLT_factor< T, R, C > &A)
Definition: log_determinant_ldlt.hpp:12
stan::math::check_positive_finite
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
Definition: check_positive_finite.hpp:28
stan::math::check_ldlt_factor
bool check_ldlt_factor(const char *function, const char *name, stan::math::LDLT_factor< T, R, C > &A)
Return true if the argument is a valid stan::math::LDLT_factor.
Definition: check_ldlt_factor.hpp:34
log_determinant_ldlt.hpp

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