Stan  1.1
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 #include <stan/meta/traits.hpp>
10 #include <stan/agrad/agrad.hpp>
11 #include <stan/agrad/matrix.hpp>
12 
13 namespace stan {
14  namespace prob {
15  // InvWishart(Sigma|n,Omega) [W, S symmetric, non-neg, definite;
16  // W.dims() = S.dims();
17  // n > S.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  inv_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::inv_wishart_log(%1%)";
54 
57  using boost::math::tools::promote_args;
58 
60  typename promote_args<T_y,T_dof,T_scale>::type lp(0.0);
61  if(!check_greater_or_equal(function, nu, k-1, "Degrees of freedom parameter",
62  &lp, Policy()))
63  return lp;
64  if (!check_size_match(function,
65  W.rows(), "Rows of random variable",
66  W.cols(), "columns of random variable",
67  &lp, Policy()))
68  return lp;
69  if (!check_size_match(function,
70  S.rows(), "Rows of scale parameter",
71  S.cols(), "columns of scale parameter",
72  &lp, Policy()))
73  return lp;
74  if (!check_size_match(function,
75  W.rows(), "Rows of random variable",
76  S.rows(), "columns of scale parameter",
77  &lp, Policy()))
78  return lp;
79  // FIXME: domain checks
80 
81  Eigen::LLT< Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> > LLT_W = W.llt();
82  if (LLT_W.info() != Eigen::Success) {
83  lp = stan::math::policies::raise_domain_error<T_y>(function,
84  "W is not positive definite (%1%)",
85  0,Policy());
86  return lp;
87  }
88  Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> L = LLT_W.matrixL();
89 
93  using stan::math::lmgamma;
94 
96  lp -= lmgamma(k, 0.5 * nu);
98  lp += nu * S.llt().matrixLLT().diagonal().array().log().sum();
99  }
101  lp -= (nu + k + 1.0) * L.diagonal().array().log().sum();
102  }
105  Eigen::Matrix<T_y,Eigen::Dynamic,1> W_inv_vec = Eigen::Map<
106  const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> >(
107  &L(0), L.size(), 1);
108  Eigen::Matrix<T_scale,Eigen::Dynamic,1> S_vec = Eigen::Map<
109  const Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic> >(
110  &S(0), S.size(), 1);
111  lp -= 0.5 * dot_product(S_vec, W_inv_vec); // trace(S * W^-1)
112  }
114  lp += nu * k * NEG_LOG_TWO_OVER_TWO;
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  inv_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 inv_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  inv_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 inv_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  inv_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 inv_wishart_log<false>(W,nu,S,stan::math::default_policy());
148  }
149 
150 
151  }
152 }
153 #endif

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