Automatic Differentiation
 
Loading...
Searching...
No Matches
multi_student_t_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_LPDF_HPP
3
17#include <cmath>
18#include <cstdlib>
19
20namespace stan {
21namespace math {
22
42template <bool propto, typename T_y, typename T_dof, typename T_loc,
43 typename T_scale>
45 const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& Sigma) {
46 using T_scale_elem = typename scalar_type<T_scale>::type;
48 using Eigen::Matrix;
49 using std::log;
50 using std::vector;
51 static constexpr const char* function = "multi_student_t";
52 check_not_nan(function, "Degrees of freedom parameter", nu);
53 check_positive(function, "Degrees of freedom parameter", nu);
54 check_finite(function, "Degrees of freedom parameter", nu);
55
56 check_consistent_sizes_mvt(function, "y", y, "mu", mu);
57
58 vector_seq_view<T_y> y_vec(y);
59 vector_seq_view<T_loc> mu_vec(mu);
60 size_t size_vec = max_size_mvt(y, mu);
61 if (size_vec == 0) {
62 return 0;
63 }
64
65 int num_dims = y_vec[0].size();
66 if (num_dims == 0) {
67 return 0;
68 }
69
70 for (size_t i = 1, size_mvt_y = size_mvt(y); i < size_mvt_y; i++) {
72 function, "Size of one of the vectors of the random variable",
73 y_vec[i].size(), "Size of another vector of the random variable",
74 y_vec[i - 1].size());
75 }
76
77 for (size_t i = 1, size_mvt_mu = size_mvt(mu); i < size_mvt_mu; i++) {
78 check_size_match(function,
79 "Size of one of the vectors "
80 "of the location variable",
81 mu_vec[i].size(),
82 "Size of another vector of "
83 "the location variable",
84 mu_vec[i - 1].size());
85 }
86
87 check_size_match(function, "Size of random variable", num_dims,
88 "size of location parameter", mu_vec[0].size());
89 check_size_match(function, "Size of random variable", num_dims,
90 "rows of scale parameter", Sigma.rows());
91 check_size_match(function, "Size of random variable", num_dims,
92 "columns of scale parameter", Sigma.cols());
93
94 for (size_t i = 0; i < size_vec; i++) {
95 check_finite(function, "Location parameter", mu_vec[i]);
96 check_not_nan(function, "Random variable", y_vec[i]);
97 }
98 const auto& Sigma_ref = to_ref(Sigma);
99 check_symmetric(function, "Scale parameter", Sigma_ref);
100
101 auto ldlt_Sigma = make_ldlt_factor(Sigma_ref);
102 check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_Sigma);
103
104 lp_type lp(0);
105
107 lp += lgamma(0.5 * (nu + num_dims)) * size_vec;
108 lp -= lgamma(0.5 * nu) * size_vec;
109 lp -= (0.5 * num_dims) * log(nu) * size_vec;
110 }
111
113 lp -= (0.5 * num_dims) * LOG_PI * size_vec;
114 }
115
116 using Eigen::Array;
117
119 lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
120 }
121
123 lp_type sum_lp_vec(0.0);
124 for (size_t i = 0; i < size_vec; i++) {
125 const auto& y_col = as_column_vector_or_scalar(y_vec[i]);
126 const auto& mu_col = as_column_vector_or_scalar(mu_vec[i]);
127 sum_lp_vec
128 += log1p(trace_inv_quad_form_ldlt(ldlt_Sigma, y_col - mu_col) / nu);
129 }
130 lp -= 0.5 * (nu + num_dims) * sum_lp_vec;
131 }
132 return lp;
133}
134
135template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
137 const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& Sigma) {
138 return multi_student_t_lpdf<false>(y, nu, mu, Sigma);
139}
140
141} // namespace math
142} // namespace stan
143#endif
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
return_type_t< T_y, T_dof, T_loc, T_scale > multi_student_t_lpdf(const T_y &y, const T_dof &nu, const T_loc &mu, const T_scale &Sigma)
The log of the multivariate student t density for the given y, mu, nu, and scale matrix.
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
size_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:18
size_t size_mvt(const ScalarT &)
Provides the size of a multivariate argument.
Definition size_mvt.hpp:24
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
value_type_t< T > log_determinant_ldlt(LDLT_factor< T > &A)
Returns log(abs(det(A))) given a LDLT_factor of A.
auto make_ldlt_factor(const T &A)
Make an LDLT_factor with matrix type plain_type_t<T>
return_type_t< T, EigMat2 > trace_inv_quad_form_ldlt(LDLT_factor< T > &A, const EigMat2 &B)
Compute the trace of an inverse quadratic form.
void check_consistent_sizes_mvt(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
static constexpr double LOG_PI
The natural logarithm of , .
Definition constants.hpp:86
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
size_t max_size_mvt(const T1 &x1, const Ts &... xs)
Calculate the size of the largest multivariate input.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_ldlt_factor(const char *function, const char *name, LDLT_factor< T > &A)
Raise domain error if the specified LDLT factor is invalid.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
std::decay_t< T > type