| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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
4 #include <boost/math/special_functions/gamma.hpp>
5 #include <boost/math/special_functions/fpclassify.hpp>
6 #include <boost/random/variate_generator.hpp>
7 #include <stan/math/prim/mat/err/check_ldlt_factor.hpp>
8 #include <stan/math/prim/scal/err/check_size_match.hpp>
9 #include <stan/math/prim/mat/err/check_symmetric.hpp>
10 #include <stan/math/prim/scal/err/check_finite.hpp>
11 #include <stan/math/prim/scal/err/check_not_nan.hpp>
12 #include <stan/math/prim/scal/err/check_positive.hpp>
13 #include <stan/math/prim/mat/fun/multiply.hpp>
14 #include <stan/math/prim/mat/fun/dot_product.hpp>
15 #include <stan/math/prim/mat/fun/subtract.hpp>
16 #include <stan/math/prim/scal/fun/log1p.hpp>
17 #include <stan/math/prim/scal/fun/constants.hpp>
18 #include <stan/math/prim/mat/prob/multi_normal_log.hpp>
19 #include <stan/math/prim/scal/meta/include_summand.hpp>
20 #include <stan/math/prim/scal/meta/VectorViewMvt.hpp>
21 #include <cmath>
22 #include <cstdlib>
23
24 namespace stan {
25
26 namespace math {
27 using Eigen::Dynamic;
34 template <bool propto,
35 typename T_y, typename T_dof, typename T_loc, typename T_scale>
36 typename return_type<T_y, T_dof, T_loc, T_scale>::type
37 multi_student_t_log(const T_y& y,
38 const T_dof& nu,
39 const T_loc& mu,
40 const T_scale& Sigma) {
41 static const char* function("stan::math::multi_student_t");
42
43 using stan::math::check_size_match;
44 using stan::math::check_finite;
45 using stan::math::check_not_nan;
46 using stan::math::check_symmetric;
47 using stan::math::check_positive;
48 using boost::math::lgamma;
49 using stan::math::log_determinant_ldlt;
50 using stan::math::LDLT_factor;
51 using stan::math::check_ldlt_factor;
52 using stan::math::log1p;
53 using std::log;
54
55 typedef typename scalar_type<T_scale>::type T_scale_elem;
56 typedef typename return_type<T_y, T_dof, T_loc, T_scale>::type lp_type;
57 lp_type lp(0.0);
58
59 // allows infinities
60 check_not_nan(function, "Degrees of freedom parameter", nu);
61 check_positive(function, "Degrees of freedom parameter", nu);
62
63 using boost::math::isinf;
64
65 if (isinf(nu)) // already checked nu > 0
66 return multi_normal_log(y, mu, Sigma);
67
68 using Eigen::Matrix;
69 using std::vector;
70 VectorViewMvt<const T_y> y_vec(y);
71 VectorViewMvt<const T_loc> mu_vec(mu);
72 // size of std::vector of Eigen vectors
73 size_t size_vec = max_size_mvt(y, mu);
74
75
76 // Check if every vector of the array has the same size
77 int size_y = y_vec[0].size();
78 int size_mu = mu_vec[0].size();
79 if (size_vec > 1) {
80 int size_y_old = size_y;
81 int size_y_new;
82 for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
83 int size_y_new = y_vec[i].size();
84 check_size_match(function,
85 "Size of one of the vectors of the random variable",
86 size_y_new,
87 "Size of another vector of the random variable",
88 size_y_old);
89 size_y_old = size_y_new;
90 }
91 int size_mu_old = size_mu;
92 int size_mu_new;
93 for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
94 int size_mu_new = mu_vec[i].size();
95 check_size_match(function,
96 "Size of one of the vectors "
97 "of the location variable",
98 size_mu_new,
99 "Size of another vector of "
100 "the location variable",
101 size_mu_old);
102 size_mu_old = size_mu_new;
103 }
104 (void) size_y_old;
105 (void) size_y_new;
106 (void) size_mu_old;
107 (void) size_mu_new;
108 }
109
110
111 check_size_match(function,
112 "Size of random variable", size_y,
113 "size of location parameter", size_mu);
114 check_size_match(function,
115 "Size of random variable", size_y,
116 "rows of scale parameter", Sigma.rows());
117 check_size_match(function,
118 "Size of random variable", size_y,
119 "columns of scale parameter", Sigma.cols());
120
121 for (size_t i = 0; i < size_vec; i++) {
122 check_finite(function, "Location parameter", mu_vec[i]);
123 check_not_nan(function, "Random variable", y_vec[i]);
124 }
125 check_symmetric(function, "Scale parameter", Sigma);
126
127
128 LDLT_factor<T_scale_elem, Dynamic, Dynamic> ldlt_Sigma(Sigma);
129 check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_Sigma);
130
131 if (size_y == 0) // y_vec[0].size() == 0
132 return lp;
133
134 if (include_summand<propto, T_dof>::value) {
135 lp += lgamma(0.5 * (nu + size_y)) * size_vec;
136 lp -= lgamma(0.5 * nu) * size_vec;
137 lp -= (0.5 * size_y) * log(nu) * size_vec;
138 }
139
140 if (include_summand<propto>::value)
141 lp -= (0.5 * size_y) * LOG_PI * size_vec;
142
143 using stan::math::multiply;
144 using stan::math::dot_product;
145 using stan::math::subtract;
146 using Eigen::Array;
147
148
149 if (include_summand<propto, T_scale_elem>::value) {
150 lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
151 }
152
153 if (include_summand<propto, T_y, T_dof, T_loc, T_scale_elem>::value) {
154 lp_type sum_lp_vec(0.0);
155 for (size_t i = 0; i < size_vec; i++) {
156 Eigen::Matrix<typename return_type<T_y, T_loc>::type, Dynamic, 1>
157 y_minus_mu(size_y);
158 for (int j = 0; j < size_y; j++)
159 y_minus_mu(j) = y_vec[i](j)-mu_vec[i](j);
160 sum_lp_vec += log1p(trace_inv_quad_form_ldlt(ldlt_Sigma, y_minus_mu)
161 / nu);
162 }
163 lp -= 0.5 * (nu + size_y) * sum_lp_vec;
164 }
165 return lp;
166 }
167
168 template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
169 inline
170 typename return_type<T_y, T_dof, T_loc, T_scale>::type
171 multi_student_t_log(const T_y& y, const T_dof& nu, const T_loc& mu,
172 const T_scale& Sigma) {
173 return multi_student_t_log<false>(y, nu, mu, Sigma);
174 }
175
176 }
177 }
178 #endif
check_not_nan.hpp
multiply.hpp
std::isinf
int isinf(const stan::math::var &a)
Checks if the given number is infinite.
Definition: std_isinf.hpp:18
stan::math::lgamma
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
stan::max_size_mvt
size_t max_size_mvt(const T1 &x1, const T2 &x2)
Definition: max_size_mvt.hpp:10
stan::math::check_not_nan
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Definition: check_not_nan.hpp:63
check_size_match.hpp
check_positive.hpp
stan::math::subtract
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
stan::math::LOG_PI
const double LOG_PI
Definition: constants.hpp:170
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
VectorViewMvt.hpp
stan::math::multiply
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:20
stan::math::check_symmetric
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Dynamic, Dynamic > &y)
Return true if the specified matrix is symmetric.
Definition: check_symmetric.hpp:38
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:38
stan::math::trace_inv_quad_form_ldlt
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)
Definition: trace_inv_quad_form_ldlt.hpp:27
check_symmetric.hpp
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:36
stan::return_type::type
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
include_summand.hpp
check_ldlt_factor.hpp
boost::math::isinf
bool isinf(const stan::math::var &v)
Checks if the given number is infinite.
Definition: boost_isinf.hpp:22
stan::math::LDLT_factor
Definition: LDLT_factor.hpp:18
multi_normal_log.hpp
size_
size_t size_
Definition: dot_self.hpp:18
stan::math::multi_student_t_log
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.
Definition: multi_student_t_log.hpp:37
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
stan::math::check_size_match
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.
Definition: check_size_match.hpp:30
stan::length_mvt
size_t length_mvt(const T &)
Definition: length_mvt.hpp:12
stan::math::dot_product
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
dot_product.hpp
stan::math::multi_normal_log
return_type< T_y, T_loc, T_covar >::type multi_normal_log(const T_y &y, const T_loc &mu, const T_covar &Sigma)
Definition: multi_normal_log.hpp:28
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:62
constants.hpp
check_finite.hpp
stan::VectorViewMvt
Definition: VectorViewMvt.hpp:16
stan::math::log1p
fvar< T > log1p(const fvar< T > &x)
Definition: log1p.hpp:16
log1p.hpp
stan::math::log_determinant_ldlt
T log_determinant_ldlt(stan::math::LDLT_factor< T, R, C > &A)
Definition: log_determinant_ldlt.hpp:12
subtract.hpp
stan::math::check_ldlt_factor
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.
Definition: check_ldlt_factor.hpp:34

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