Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
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/prob/traits.hpp>
9 #include <boost/concept_check.hpp>
10 
11 namespace stan {
12 
13  namespace prob {
14 
15  // Wishart(Sigma|n,Omega) [Sigma, Omega symmetric, non-neg, definite;
16  // Sigma.dims() = Omega.dims();
17  // n > Sigma.rows() - 1]
45  template <bool propto,
46  typename T_y, typename T_dof, typename T_scale,
47  class Policy>
48  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
49  wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
50  const T_dof& nu,
51  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S,
52  const Policy&) {
53  static const char* function = "stan::prob::wishart_log(%1%)";
54 
57  using boost::math::tools::promote_args;
58  using Eigen::Array;
59 
61  typename promote_args<T_y,T_dof,T_scale>::type lp;
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 
93  using stan::math::lmgamma;
94  lp = 0.0;
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  Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic> I(k,k);
106  I.setIdentity();
107  L_S = mdivide_left_tri_low(L_S, I);
108  L_S = L_S.transpose() * L_S.template triangularView<Eigen::Lower>();
109  lp -= 0.5 * elt_multiply(L_S, W).array().sum(); // trace(S^-1 * W)
110  }
111 
112  if (include_summand<propto,T_y,T_dof>::value && nu != (k + 1))
113  lp += (nu - k - 1.0) * L_W.diagonal().array().log().sum();
114 
115  return lp;
116  }
117 
118  template <bool propto,
119  typename T_y, typename T_dof, typename T_scale>
120  inline
121  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
122  wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
123  const T_dof& nu,
124  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S) {
125  return wishart_log<propto>(W,nu,S,stan::math::default_policy());
126  }
127 
128 
129  template <typename T_y, typename T_dof, typename T_scale,
130  class Policy>
131  inline
132  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
133  wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
134  const T_dof& nu,
135  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S,
136  const Policy&) {
137  return wishart_log<false>(W,nu,S,Policy());
138  }
139 
140 
141  template <typename T_y, typename T_dof, typename T_scale>
142  inline
143  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
144  wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
145  const T_dof& nu,
146  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S) {
147  return wishart_log<false>(W,nu,S,stan::math::default_policy());
148  }
149 
150 
151  }
152 }
153 #endif

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