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

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

matrix_normal_prec_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MATRIX_NORMAL_PREC_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MATRIX_NORMAL_PREC_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
11 #include <stan/math/prim/mat/fun/log.hpp>
12 #include <stan/math/prim/mat/fun/log_determinant.hpp>
13 #include <stan/math/prim/mat/fun/log_determinant_ldlt.hpp>
14 #include <stan/math/prim/mat/fun/subtract.hpp>
15 #include <stan/math/prim/mat/fun/trace_quad_form.hpp>
16 #include <stan/math/prim/mat/fun/trace_gen_quad_form.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 {
40 template <bool propto,
41 typename T_y, typename T_Mu, typename T_Sigma, typename T_D>
42 typename boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type
43 matrix_normal_prec_log(const Eigen::Matrix
44 <T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
45 const Eigen::Matrix
46 <T_Mu, Eigen::Dynamic, Eigen::Dynamic>& Mu,
47 const Eigen::Matrix
48 <T_Sigma, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
49 const Eigen::Matrix
50 <T_D, Eigen::Dynamic, Eigen::Dynamic>& D) {
51 static const char* function("stan::math::matrix_normal_prec_log");
52 typename
53 boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type lp(0.0);
54
55 using stan::math::check_not_nan;
56 using stan::math::check_symmetric;
57 using stan::math::check_size_match;
58 using stan::math::check_positive;
59 using stan::math::check_finite;
60 using stan::math::trace_gen_quad_form;
61 using stan::math::log_determinant_ldlt;
62 using stan::math::subtract;
63 using stan::math::LDLT_factor;
64 using stan::math::check_ldlt_factor;
65
66 check_positive(function, "Sigma rows", Sigma.rows());
67 check_finite(function, "Sigma", Sigma);
68 check_symmetric(function, "Sigma", Sigma);
69
70 LDLT_factor<T_Sigma, Eigen::Dynamic, Eigen::Dynamic> ldlt_Sigma(Sigma);
71 check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
72 check_positive(function, "D rows", D.rows());
73 check_finite(function, "D", D);
74 check_symmetric(function, "Sigma", D);
75
76 LDLT_factor<T_D, Eigen::Dynamic, Eigen::Dynamic> ldlt_D(D);
77 check_ldlt_factor(function, "LDLT_Factor of D", ldlt_D);
78 check_size_match(function,
79 "Rows of random variable", y.rows(),
80 "Rows of location parameter", Mu.rows());
81 check_size_match(function,
82 "Columns of random variable", y.cols(),
83 "Columns of location parameter", Mu.cols());
84 check_size_match(function,
85 "Rows of random variable", y.rows(),
86 "Rows of Sigma", Sigma.rows());
87 check_size_match(function,
88 "Columns of random variable", y.cols(),
89 "Rows of D", D.rows());
90 check_finite(function, "Location parameter", Mu);
91 check_finite(function, "Random variable", y);
92
93 if (include_summand<propto>::value)
94 lp += NEG_LOG_SQRT_TWO_PI * y.cols() * y.rows();
95
96 if (include_summand<propto, T_Sigma>::value) {
97 lp += log_determinant_ldlt(ldlt_Sigma) * (0.5 * y.rows());
98 }
99
100 if (include_summand<propto, T_D>::value) {
101 lp += log_determinant_ldlt(ldlt_D) * (0.5 * y.cols());
102 }
103
104 if (include_summand<propto, T_y, T_Mu, T_Sigma, T_D>::value) {
105 lp -= 0.5 * trace_gen_quad_form(D, Sigma, subtract(y, Mu));
106 }
107 return lp;
108 }
109
110 template <typename T_y, typename T_Mu, typename T_Sigma, typename T_D>
111 typename boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type
112 matrix_normal_prec_log(const Eigen::Matrix
113 <T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
114 const Eigen::Matrix
115 <T_Mu, Eigen::Dynamic, Eigen::Dynamic>& Mu,
116 const Eigen::Matrix
117 <T_Sigma, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
118 const Eigen::Matrix
119 <T_D, Eigen::Dynamic, Eigen::Dynamic>& D) {
120 return matrix_normal_prec_log<false>(y, Mu, Sigma, D);
121 }
122 }
123 }
124
125 #endif
check_not_nan.hpp
stan::math::matrix_normal_prec_log
boost::math::tools::promote_args< T_y, T_Mu, T_Sigma, T_D >::type matrix_normal_prec_log(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const Eigen::Matrix< T_Mu, Eigen::Dynamic, Eigen::Dynamic > &Mu, const Eigen::Matrix< T_Sigma, Eigen::Dynamic, Eigen::Dynamic > &Sigma, const Eigen::Matrix< T_D, Eigen::Dynamic, Eigen::Dynamic > &D)
The log of the matrix normal density for the given y, mu, Sigma and D where Sigma and D are given as ...
Definition: matrix_normal_prec_log.hpp:43
log_determinant.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.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::subtract
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix...
Definition: subtract.hpp:27
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
stan::math::trace_gen_quad_form
fvar< T > trace_gen_quad_form(const Eigen::Matrix< fvar< T >, RD, CD > &D, const Eigen::Matrix< fvar< T >, RA, CA > &A, const Eigen::Matrix< fvar< T >, RB, CB > &B)
Definition: trace_gen_quad_form.hpp:15
include_summand.hpp
check_ldlt_factor.hpp
trace_gen_quad_form.hpp
stan::math::LDLT_factor
Definition: LDLT_factor.hpp:18
trace_quad_form.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::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
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
subtract.hpp
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.