Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
inv_wishart.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__INV_WISHART_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__INV_WISHART_HPP__
3 
8 #include <stan/prob/traits.hpp>
9 
10 namespace stan {
11  namespace prob {
12  // InvWishart(Sigma|n,Omega) [W, S symmetric, non-neg, definite;
13  // W.dims() = S.dims();
14  // n > S.rows() - 1]
42  template <bool propto,
43  typename T_y, typename T_dof, typename T_scale,
44  class Policy>
45  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
46  inv_wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
47  const T_dof& nu,
48  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S,
49  const Policy&) {
50  static const char* function = "stan::prob::inv_wishart_log(%1%)";
51 
54  using boost::math::tools::promote_args;
55  using Eigen::Array;
56 
58  typename promote_args<T_y,T_dof,T_scale>::type lp(0.0);
59  if(!check_greater_or_equal(function, nu, k-1, "Degrees of freedom parameter",
60  &lp, Policy()))
61  return lp;
62  if (!check_size_match(function, W.rows(), W.cols(), &lp, Policy()))
63  return lp;
64  if (!check_size_match(function, S.rows(), S.cols(), &lp, Policy()))
65  return lp;
66  if (!check_size_match(function, W.rows(), S.rows(), &lp, Policy()))
67  return lp;
68  // FIXME: domain checks
69 
70  Eigen::LLT< Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> > LLT_W = W.llt();
71  if (LLT_W.info() != Eigen::Success) {
72  lp = stan::math::policies::raise_domain_error<T_y>(function,
73  "W is not positive definite (%1%)",
74  0,Policy());
75  return lp;
76  }
77  Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> L = LLT_W.matrixL();
78 
82  using stan::math::lmgamma;
83 
85  lp -= lmgamma(k, 0.5 * nu);
87  lp += nu * S.llt().matrixLLT().diagonal().array().log().sum();
88  }
90  lp -= (nu + k + 1.0) * L.diagonal().array().log().sum();
91  }
94  lp -= 0.5 * elt_multiply(S, L).array().sum(); // trace(S * W^-1)
95  }
97  lp += nu * k * NEG_LOG_TWO_OVER_TWO;
98  return lp;
99  }
100 
101  template <bool propto,
102  typename T_y, typename T_dof, typename T_scale>
103  inline
104  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
105  inv_wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
106  const T_dof& nu,
107  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S) {
108  return inv_wishart_log<propto>(W,nu,S,stan::math::default_policy());
109  }
110 
111 
112  template <typename T_y, typename T_dof, typename T_scale,
113  class Policy>
114  inline
115  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
116  inv_wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
117  const T_dof& nu,
118  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S,
119  const Policy&) {
120  return inv_wishart_log<false>(W,nu,S,Policy());
121  }
122 
123 
124  template <typename T_y, typename T_dof, typename T_scale>
125  inline
126  typename boost::math::tools::promote_args<T_y,T_dof,T_scale>::type
127  inv_wishart_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& W,
128  const T_dof& nu,
129  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic>& S) {
130  return inv_wishart_log<false>(W,nu,S,stan::math::default_policy());
131  }
132 
133 
134  }
135 }
136 #endif

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