Stan  1.0
probability, sampling & optimization
wishart.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__WISHART_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__WISHART_HPP__
3 
8 #include <stan/agrad/matrix.hpp>
9 #include <stan/prob/traits.hpp>
10 #include <boost/concept_check.hpp>
11 
12 namespace stan {
13 
14  namespace prob {
15 
16  // Wishart(Sigma|n,Omega) [Sigma, Omega symmetric, non-neg, definite;
17  // Sigma.dims() = Omega.dims();
18  // n > Sigma.rows() - 1]
46  template <bool propto,
47  typename T_y, typename T_dof, typename T_scale,
48  class Policy>
49  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
50  wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
51  const T_dof& nu,
52  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S,
53  const Policy&) {
54  static const char* function = "stan::prob::wishart_log(%1%)";
55 
58  using boost::math::tools::promote_args;
59 
61  typename promote_args<T_y,T_dof,T_scale>::type lp(0.0);
62  if (!check_greater_or_equal(function, nu, k - 1,
63  "Degrees of freedom parameter", &lp, Policy()))
64  return lp;
65  if (!check_size_match(function, W.rows(), W.cols(), &lp, Policy()))
66  return lp;
67  if (!check_size_match(function, S.rows(), S.cols(), &lp, Policy()))
68  return lp;
69  if (!check_size_match(function, W.rows(), S.rows(), &lp, Policy()))
70  return lp;
71  // FIXME: domain checks
72 
73  Eigen::LLT< Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> > LLT_W = W.llt();
74  if (LLT_W.info() != Eigen::Success) {
75  lp = stan::math::policies::raise_domain_error<T_y>(function,
76  "W is not positive definite (%1%)",
77  0,Policy());
78  return lp;
79  }
80  Eigen::LLT< Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic> > LLT_S = S.llt();
81  if (LLT_S.info() != Eigen::Success) {
82  lp = stan::math::policies::raise_domain_error<T_scale>(function,
83  "S is not positive definite (%1%)",
84  0,Policy());
85  return lp;
86  }
87 
88  Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> L_W = LLT_W.matrixL();
89  Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic> L_S = LLT_S.matrixL();
90 
94  using stan::math::lmgamma;
96  lp += nu * k * NEG_LOG_TWO_OVER_TWO;
97 
99  lp -= lmgamma(k, 0.5 * nu);
100 
102  lp -= nu * L_S.diagonal().array().log().sum();
103 
105  L_S = crossprod(mdivide_left_tri_low(L_S));
106  Eigen::Matrix<T_scale,Eigen::Dynamic,1> S_inv_vec = Eigen::Map<
107  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic> >(
108  &L_S(0), L_S.size(), 1);
109  Eigen::Matrix<T_y,Eigen::Dynamic,1> W_vec = Eigen::Map<
110  const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> >(
111  &W(0), W.size(), 1);
112  lp -= 0.5 * dot_product(S_inv_vec, W_vec); // trace(S^-1 * W)
113  }
114 
115  if (include_summand<propto,T_y,T_dof>::value && nu != (k + 1))
116  lp += (nu - k - 1.0) * L_W.diagonal().array().log().sum();
117  return lp;
118  }
119 
120  template <bool propto,
121  typename T_y, typename T_dof, typename T_scale>
122  inline
123  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
124  wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
125  const T_dof& nu,
126  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S) {
127  return wishart_log<propto>(W,nu,S,stan::math::default_policy());
128  }
129 
130 
131  template <typename T_y, typename T_dof, typename T_scale,
132  class Policy>
133  inline
134  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
135  wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
136  const T_dof& nu,
137  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S,
138  const Policy&) {
139  return wishart_log<false>(W,nu,S,Policy());
140  }
141 
142 
143  template <typename T_y, typename T_dof, typename T_scale>
144  inline
145  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
146  wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
147  const T_dof& nu,
148  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S) {
149  return wishart_log<false>(W,nu,S,stan::math::default_policy());
150  }
151 
152 
153  }
154 }
155 #endif
internal::traits< Derived >::Index size_type
var dot_product(const Eigen::Matrix< var, R1, C1 > &v1, const Eigen::Matrix< var, R2, C2 > &v2)
Returns the dot product.
Definition: matrix.hpp:716
matrix_v crossprod(const matrix_v &M)
Returns the result of pre-multiplying a matrix by its own transpose.
Definition: matrix.hpp:1171
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_left_tri_low(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &b)
Definition: matrix.hpp:1708
boost::math::tools::promote_args< T >::type lmgamma(unsigned int k, T x)
Return the natural logarithm of the multivariate gamma function with the speciifed dimensions and arg...
bool check_greater_or_equal(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result, const Policy &)
matrix_d crossprod(const matrix_d &M)
Returns the result of pre-multiplying a matrix by its own transpose.
Definition: matrix.hpp:1565
double dot_product(const Eigen::Matrix< double, R1, C1 > &v1, const Eigen::Matrix< double, R2, C2 > &v2)
Returns the dot product of the specified vectors.
Definition: matrix.hpp:875
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
bool check_size_match(const char *function, T_size1 i, T_size2 j, T_result *result, const Policy &)
boost::math::tools::promote_args< T_y, T_dof, T_scale >::type 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, const Policy &)
The log of the Wishart density for the given W, degrees of freedom, and scale matrix.
Definition: wishart.hpp:50
Probability, optimization and sampling library.
Definition: agrad.cpp:6
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33

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