| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

inv_wishart_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_INV_WISHART_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_INV_WISHART_LOG_HPP
3
4 #include <stan/math/prim/mat/err/check_ldlt_factor.hpp>
5 #include <stan/math/prim/scal/err/check_greater.hpp>
6 #include <stan/math/prim/scal/err/check_size_match.hpp>
7 #include <stan/math/prim/mat/err/check_square.hpp>
8 #include <stan/math/prim/mat/meta/index_type.hpp>
9 #include <stan/math/prim/mat/fun/log_determinant_ldlt.hpp>
10 #include <stan/math/prim/mat/fun/mdivide_left_ldlt.hpp>
11
12 #include <stan/math/prim/scal/fun/constants.hpp>
13 #include <stan/math/prim/scal/meta/include_summand.hpp>
14 #include <stan/math/prim/scal/fun/lmgamma.hpp>
15 #include <stan/math/prim/mat/fun/trace.hpp>
16
17 namespace stan {
18 namespace math {
19 // InvWishart(Sigma|n, Omega) [W, S symmetric, non-neg, definite;
20 // W.dims() = S.dims();
21 // n > S.rows() - 1]
49 template <bool propto,
50 typename T_y, typename T_dof, typename T_scale>
51 typename boost::math::tools::promote_args<T_y, T_dof, T_scale>::type
52 inv_wishart_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& W,
53 const T_dof& nu,
54 const Eigen::Matrix
55 <T_scale, Eigen::Dynamic, Eigen::Dynamic>& S) {
56 static const char* function("stan::math::inv_wishart_log");
57
58 using boost::math::tools::promote_args;
59 using Eigen::Dynamic;
60 using Eigen::Matrix;
61 using stan::math::check_greater;
62 using stan::math::check_size_match;
63 using stan::math::check_square;
64 using stan::math::index_type;
65
66 typename index_type<Matrix<T_scale, Dynamic, Dynamic> >::type k
67 = S.rows();
68 typename promote_args<T_y, T_dof, T_scale>::type lp(0.0);
69
70 check_greater(function, "Degrees of freedom parameter", nu, k-1);
71 check_square(function, "random variable", W);
72 check_square(function, "scale parameter", S);
73 check_size_match(function,
74 "Rows of random variable", W.rows(),
75 "columns of scale parameter", S.rows());
76
77 // FIXME: domain checks
78
79 using stan::math::lmgamma;
80 using stan::math::log_determinant_ldlt;
81 using stan::math::mdivide_left_ldlt;
82 using stan::math::trace;
83 using stan::math::LDLT_factor;
84 using stan::math::check_ldlt_factor;
85
86 LDLT_factor<T_y, Eigen::Dynamic, Eigen::Dynamic> ldlt_W(W);
87 check_ldlt_factor(function, "LDLT_Factor of random variable", ldlt_W);
88 LDLT_factor<T_scale, Eigen::Dynamic, Eigen::Dynamic> ldlt_S(S);
89 check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_S);
90
91 if (include_summand<propto, T_dof>::value)
92 lp -= lmgamma(k, 0.5 * nu);
93 if (include_summand<propto, T_dof, T_scale>::value) {
94 lp += 0.5 * nu * log_determinant_ldlt(ldlt_S);
95 }
96 if (include_summand<propto, T_y, T_dof, T_scale>::value) {
97 lp -= 0.5 * (nu + k + 1.0) * log_determinant_ldlt(ldlt_W);
98 }
99 if (include_summand<propto, T_y, T_scale>::value) {
100 // L = crossprod(mdivide_left_tri_low(L));
101 // Eigen::Matrix<T_y, Eigen::Dynamic, 1> W_inv_vec = Eigen::Map<
102 // const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic> >(
103 // &L(0), L.size(), 1);
104 // Eigen::Matrix<T_scale, Eigen::Dynamic, 1> S_vec = Eigen::Map<
105 // const Eigen::Matrix<T_scale, Eigen::Dynamic, Eigen::Dynamic> >(
106 // &S(0), S.size(), 1);
107 // lp -= 0.5 * dot_product(S_vec, W_inv_vec); // trace(S * W^-1)
108 Eigen::Matrix<typename promote_args<T_y, T_scale>::type,
109 Eigen::Dynamic, Eigen::Dynamic>
110 Winv_S(mdivide_left_ldlt
111 (ldlt_W,
112 static_cast<Eigen::Matrix
113 <T_scale, Eigen::Dynamic, Eigen::Dynamic> >
114 (S.template selfadjointView<Eigen::Lower>())));
115 lp -= 0.5*trace(Winv_S);
116 }
117 if (include_summand<propto, T_dof, T_scale>::value)
118 lp += nu * k * NEG_LOG_TWO_OVER_TWO;
119 return lp;
120 }
121
122 template <typename T_y, typename T_dof, typename T_scale>
123 inline
124 typename boost::math::tools::promote_args<T_y, T_dof, T_scale>::type
125 inv_wishart_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& W,
126 const T_dof& nu,
127 const Eigen::Matrix
128 <T_scale, Eigen::Dynamic, Eigen::Dynamic>& S) {
129 return inv_wishart_log<false>(W, nu, S);
130 }
131
132 }
133 }
134 #endif
check_size_match.hpp
stan
Definition: log_sum_exp.hpp:8
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
trace.hpp
include_summand.hpp
check_greater.hpp
check_ldlt_factor.hpp
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
stan::math::LDLT_factor
Definition: LDLT_factor.hpp:18
index_type.hpp
stan::math::mdivide_left_ldlt
Eigen::Matrix< fvar< T2 >, R1, C2 > mdivide_left_ldlt(const stan::math::LDLT_factor< double, R1, C1 > &A, const Eigen::Matrix< fvar< T2 >, R2, C2 > &b)
Returns the solution of the system Ax=b given an LDLT_factor of A.
Definition: mdivide_left_ldlt.hpp:25
stan::math::inv_wishart_log
boost::math::tools::promote_args< T_y, T_dof, T_scale >::type inv_wishart_log(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &W, const T_dof &nu, const Eigen::Matrix< T_scale, Eigen::Dynamic, Eigen::Dynamic > &S)
The log of the Inverse-Wishart density for the given W, degrees of freedom, and scale matrix...
Definition: inv_wishart_log.hpp:52
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
constants.hpp
lmgamma.hpp
stan::math::trace
T trace(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the trace of the specified matrix.
Definition: trace.hpp:20
stan::math::NEG_LOG_TWO_OVER_TWO
const double NEG_LOG_TWO_OVER_TWO
Definition: constants.hpp:191
mdivide_left_ldlt.hpp
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::lmgamma
fvar< typename stan::return_type< T, int >::type > lmgamma(int x1, const fvar< T > &x2)
Definition: lmgamma.hpp:16
stan::math::check_square
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
Definition: check_square.hpp:28
stan::math::check_greater
bool check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is strictly greater than low.
Definition: check_greater.hpp:84
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
check_square.hpp

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