Stan Math Library  2.10.0
reverse mode automatic differentiation
multi_student_t_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_STUDENT_T_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_STUDENT_T_LOG_HPP
3 
18 #include <boost/math/special_functions/gamma.hpp>
19 #include <boost/math/special_functions/fpclassify.hpp>
20 #include <boost/random/variate_generator.hpp>
21 #include <cmath>
22 #include <cstdlib>
23 
24 namespace stan {
25  namespace math {
32  template <bool propto,
33  typename T_y, typename T_dof, typename T_loc, typename T_scale>
34  typename return_type<T_y, T_dof, T_loc, T_scale>::type
35  multi_student_t_log(const T_y& y,
36  const T_dof& nu,
37  const T_loc& mu,
38  const T_scale& Sigma) {
39  static const char* function("stan::math::multi_student_t");
40 
46  using boost::math::lgamma;
50  using stan::math::log1p;
51  using std::log;
52 
53  typedef typename scalar_type<T_scale>::type T_scale_elem;
54  typedef typename return_type<T_y, T_dof, T_loc, T_scale>::type lp_type;
55  lp_type lp(0.0);
56 
57  // allows infinities
58  check_not_nan(function, "Degrees of freedom parameter", nu);
59  check_positive(function, "Degrees of freedom parameter", nu);
60 
61  using boost::math::isinf;
62 
63  if (isinf(nu)) // already checked nu > 0
64  return multi_normal_log(y, mu, Sigma);
65 
66  using Eigen::Matrix;
67  using std::vector;
68  VectorViewMvt<const T_y> y_vec(y);
69  VectorViewMvt<const T_loc> mu_vec(mu);
70  // size of std::vector of Eigen vectors
71  size_t size_vec = max_size_mvt(y, mu);
72 
73 
74  // Check if every vector of the array has the same size
75  int size_y = y_vec[0].size();
76  int size_mu = mu_vec[0].size();
77  if (size_vec > 1) {
78  int size_y_old = size_y;
79  int size_y_new;
80  for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
81  int size_y_new = y_vec[i].size();
82  check_size_match(function,
83  "Size of one of the vectors of the random variable",
84  size_y_new,
85  "Size of another vector of the random variable",
86  size_y_old);
87  size_y_old = size_y_new;
88  }
89  int size_mu_old = size_mu;
90  int size_mu_new;
91  for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
92  int size_mu_new = mu_vec[i].size();
93  check_size_match(function,
94  "Size of one of the vectors "
95  "of the location variable",
96  size_mu_new,
97  "Size of another vector of "
98  "the location variable",
99  size_mu_old);
100  size_mu_old = size_mu_new;
101  }
102  (void) size_y_old;
103  (void) size_y_new;
104  (void) size_mu_old;
105  (void) size_mu_new;
106  }
107 
108 
109  check_size_match(function,
110  "Size of random variable", size_y,
111  "size of location parameter", size_mu);
112  check_size_match(function,
113  "Size of random variable", size_y,
114  "rows of scale parameter", Sigma.rows());
115  check_size_match(function,
116  "Size of random variable", size_y,
117  "columns of scale parameter", Sigma.cols());
118 
119  for (size_t i = 0; i < size_vec; i++) {
120  check_finite(function, "Location parameter", mu_vec[i]);
121  check_not_nan(function, "Random variable", y_vec[i]);
122  }
123  check_symmetric(function, "Scale parameter", Sigma);
124 
125 
126  LDLT_factor<T_scale_elem,
127  Eigen::Dynamic, Eigen::Dynamic> ldlt_Sigma(Sigma);
128  check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_Sigma);
129 
130  if (size_y == 0) // y_vec[0].size() == 0
131  return lp;
132 
134  lp += lgamma(0.5 * (nu + size_y)) * size_vec;
135  lp -= lgamma(0.5 * nu) * size_vec;
136  lp -= (0.5 * size_y) * log(nu) * size_vec;
137  }
138 
140  lp -= (0.5 * size_y) * LOG_PI * size_vec;
141 
142  using stan::math::multiply;
144  using stan::math::subtract;
145  using Eigen::Array;
146 
147 
149  lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
150  }
151 
153  lp_type sum_lp_vec(0.0);
154  for (size_t i = 0; i < size_vec; i++) {
155  Eigen::Matrix<typename return_type<T_y, T_loc>::type,
156  Eigen::Dynamic, 1> y_minus_mu(size_y);
157  for (int j = 0; j < size_y; j++)
158  y_minus_mu(j) = y_vec[i](j)-mu_vec[i](j);
159  sum_lp_vec += log1p(trace_inv_quad_form_ldlt(ldlt_Sigma, y_minus_mu)
160  / nu);
161  }
162  lp -= 0.5 * (nu + size_y) * sum_lp_vec;
163  }
164  return lp;
165  }
166 
167  template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
168  inline
170  multi_student_t_log(const T_y& y, const T_dof& nu, const T_loc& mu,
171  const T_scale& Sigma) {
172  return multi_student_t_log<false>(y, nu, mu, Sigma);
173  }
174 
175  }
176 }
177 #endif
int isinf(const stan::math::var &a)
Checks if the given number is infinite.
Definition: std_isinf.hpp:18
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
size_t max_size_mvt(const T1 &x1, const T2 &x2)
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
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: subtract.hpp:27
const double LOG_PI
Definition: constants.hpp:170
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:21
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:35
boost::enable_if_c<!stan::is_var< T1 >::value &&!stan::is_var< T2 >::value, typename boost::math::tools::promote_args< T1, T2 >::type >::type trace_inv_quad_form_ldlt(const stan::math::LDLT_factor< T1, R2, C2 > &A, const Eigen::Matrix< T2, R3, C3 > &B)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: return_type.hpp:27
bool isinf(const stan::math::var &v)
Checks if the given number is infinite.
Definition: boost_isinf.hpp:22
size_t size_
Definition: dot_self.hpp:18
return_type< T_y, T_dof, T_loc, T_scale >::type multi_student_t_log(const T_y &y, const T_dof &nu, const T_loc &mu, const T_scale &Sigma)
Return the log of the multivariate Student t distribution at the specified arguments.
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:20
return_type< T_y, T_loc, T_covar >::type multi_normal_log(const T_y &y, const T_loc &mu, const T_covar &Sigma)
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
fvar< T > log1p(const fvar< T > &x)
Definition: log1p.hpp:16
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is symmetric.
T log_determinant_ldlt(stan::math::LDLT_factor< T, R, C > &A)
size_t length_mvt(const Eigen::Matrix< T, R, C > &)
Definition: length_mvt.hpp:12
bool check_ldlt_factor(const char *function, const char *name, stan::math::LDLT_factor< T, R, C > &A)
Return true if the argument is a valid stan::math::LDLT_factor.

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