Stan  1.0
probability, sampling & optimization
multi_student_t.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__MULTI_STUDENT_T_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__MULTI_STUDENT_T_HPP__
3 
4 #include <cstdlib>
5 
10 #include <stan/prob/traits.hpp>
12 
13 namespace stan {
14 
15  namespace prob {
16 
23  template <bool propto,
24  typename T_y, typename T_dof, typename T_loc, typename T_scale,
25  class Policy>
26  typename boost::math::tools::promote_args<T_y,T_dof,T_loc,T_scale>::type
27  multi_student_t_log(const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
28  const T_dof& nu,
29  const Eigen::Matrix<T_loc,Eigen::Dynamic,1>& mu,
30  const
31  Eigen::Matrix<T_scale,
32  Eigen::Dynamic,Eigen::Dynamic>& Sigma,
33  const Policy&) {
34  static const char* function = "stan::prob::multi_student_t(%1%)";
35 
41  using boost::math::tools::promote_args;
42 
43  typename promote_args<T_y,T_dof,T_loc,T_scale>::type lp(0.0);
44  if (!check_size_match(function, y.size(), mu.size(), &lp, Policy()))
45  return lp;
46  if (!check_size_match(function, y.size(), Sigma.rows(), &lp, Policy()))
47  return lp;
48  if (!check_size_match(function, y.size(), Sigma.cols(), &lp, Policy()))
49  return lp;
50  if (!check_finite(function, mu, "Location parameter", &lp, Policy()))
51  return lp;
52  if (!check_not_nan(function, y, "Random variable", &lp, Policy()))
53  return lp;
54  if (!check_symmetric(function, Sigma, "Scale parameter", &lp, Policy()))
55  return lp;
56 
57  // allows infinities
58  if (!check_not_nan(function, nu,
59  "Degrees of freedom parameter", &lp,
60  Policy()))
61  return lp;
62  if (!check_positive(function, nu,
63  "Degrees of freedom parameter", &lp,
64  Policy()))
65  return lp;
66 
67  using std::isinf;
68 
69  if (isinf(nu)) // already checked nu > 0
70  return multi_normal_log(y,mu,Sigma,Policy());
71 
72  Eigen::LLT< Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic> > LLT = Sigma.llt();
73  if (LLT.info() != Eigen::Success) {
74  lp = stan::math::policies::raise_domain_error<T_scale>(function,
75  "Sigma is not positive definite (%1%)",
76  0,Policy());
77  return lp;
78  }
79  Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic> L = LLT.matrixL();
80 
81  double d = y.size();
82 
84  lp += lgamma(0.5 * (nu + d));
85  lp -= lgamma(0.5 * nu);
86  lp -= (0.5 * d) * log(nu);
87  }
88 
90  lp -= (0.5 * d) * LOG_PI;
91 
95  using Eigen::Array;
97 
98 
100  lp -= L.diagonal().array().log().sum();
101 
103 // Eigen::Matrix<T_scale,Eigen::Dynamic,Eigen::Dynamic> I(d,d);
104 // I.setIdentity();
105 
106  Eigen::Matrix<typename promote_args<T_y,T_loc>::type,
107  Eigen::Dynamic,
108  1> y_minus_mu = subtract(y,mu);
109  Eigen::Matrix<typename promote_args<T_scale,T_y,T_loc>::type,
110  Eigen::Dynamic,
111  1> half = L = mdivide_left_tri<Eigen::Lower>(L, y_minus_mu);
112  lp -= 0.5
113  * (nu + d)
114  * log(1.0 + dot_self(half) / nu);
115  }
116  return lp;
117  }
118 
119  template <bool propto,
120  typename T_y, typename T_dof, typename T_loc, typename T_scale>
121  inline
122  typename boost::math::tools::promote_args<T_y,T_dof,T_loc,T_scale>::type
123  multi_student_t_log(const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
124  const T_dof& nu,
125  const Eigen::Matrix<T_loc,Eigen::Dynamic,1>& mu,
126  const
127  Eigen::Matrix<T_scale,
128  Eigen::Dynamic,Eigen::Dynamic>& Sigma) {
129  return multi_student_t_log<propto>(y,nu,mu,Sigma,
131  }
132 
133  template <typename T_y, typename T_dof, typename T_loc, typename T_scale,
134  class Policy>
135  inline
136  typename boost::math::tools::promote_args<T_y,T_dof,T_loc,T_scale>::type
137  multi_student_t_log(const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
138  const T_dof& nu,
139  const Eigen::Matrix<T_loc,Eigen::Dynamic,1>& mu,
140  const
141  Eigen::Matrix<T_scale,
142  Eigen::Dynamic,Eigen::Dynamic>& Sigma,
143  const Policy&) {
144  return multi_student_t_log<false>(y,nu,mu,Sigma,Policy());
145  }
146 
147 
148 
149  template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
150  inline
151  typename boost::math::tools::promote_args<T_y,T_dof,T_loc,T_scale>::type
152  multi_student_t_log(const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
153  const T_dof& nu,
154  const Eigen::Matrix<T_loc,Eigen::Dynamic,1>& mu,
155  const
156  Eigen::Matrix<T_scale,
157  Eigen::Dynamic,Eigen::Dynamic>& Sigma) {
158  return multi_student_t_log<false>(y,nu,mu,Sigma,
160  }
161 
162 
163 
164  }
165 }
166 #endif
var dot_self(const Eigen::Matrix< var, R, C > &v)
Returns the dot product of a vector with itself.
Definition: matrix.hpp:702
var lgamma(const stan::agrad::var &a)
The log gamma function for variables (C99).
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
bool check_symmetric(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result, const Policy &)
Return true if the specified matrix is symmetric.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix.
Definition: matrix.hpp:1300
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
double dot_self(const Eigen::Matrix< double, R, C > &v)
Returns the dot product of the specified vector with itself.
Definition: matrix.hpp:846
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_left_tri(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &b)
Returns the solution of the system Ax=b when A is triangular.
Definition: matrix.hpp:1746
Eigen::Matrix< double, R, C > multiply(const Eigen::Matrix< double, R, C > &m, double c)
Return specified matrix multiplied by specified scalar.
Definition: matrix.hpp:1460
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
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_loc, T_scale >::type multi_student_t_log(const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const T_dof &nu, const Eigen::Matrix< T_loc, Eigen::Dynamic, 1 > &mu, const Eigen::Matrix< T_scale, Eigen::Dynamic, Eigen::Dynamic > &Sigma, const Policy &)
Return the log of the multivariate Student t distribution at the specified arguments.
boost::math::tools::promote_args< T_y, T_loc, T_covar >::type multi_normal_log(const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const Eigen::Matrix< T_loc, Eigen::Dynamic, 1 > &mu, const Eigen::Matrix< T_covar, Eigen::Dynamic, Eigen::Dynamic > &Sigma, const Policy &)
The log of the multivariate normal density for the given y, mu, and variance matrix.
Probability, optimization and sampling library.
Definition: agrad.cpp:6
int isinf(const stan::agrad::var &a)
Checks if the given number is infinite.
Definition: agrad.hpp:2374
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.