1 #ifndef STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_LPDF_HPP
2 #define STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_LPDF_HPP
3
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/as_column_vector_or_scalar.hpp>
7 #include <stan/math/prim/fun/constants.hpp>
8 #include <stan/math/prim/fun/is_inf.hpp>
9 #include <stan/math/prim/fun/log.hpp>
10 #include <stan/math/prim/fun/log1p.hpp>
11 #include <stan/math/prim/fun/lgamma.hpp>
12 #include <stan/math/prim/fun/max_size_mvt.hpp>
13 #include <stan/math/prim/fun/size_mvt.hpp>
14 #include <stan/math/prim/fun/to_ref.hpp>
15 #include <stan/math/prim/fun/vector_seq_view.hpp>
16 #include <stan/math/prim/prob/multi_normal_log.hpp>
17 #include <cmath>
18 #include <cstdlib>
19
20 namespace stan {
21 namespace math {
22
23 /** \ingroup multivar_dists
24 * Return the log of the multivariate Student t distribution
25 * at the specified arguments.
26 *
27 * @tparam propto Carry out calculations up to a proportion
28 */
29 template <bool propto, typename T_y, typename T_dof, typename T_loc,
30 typename T_scale>
multi_student_t_lpdf(const T_y & y,const T_dof & nu,const T_loc & mu,const T_scale & Sigma)31 return_type_t<T_y, T_dof, T_loc, T_scale> multi_student_t_lpdf(
32 const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& Sigma) {
33 using T_scale_elem = typename scalar_type<T_scale>::type;
34 using lp_type = return_type_t<T_y, T_dof, T_loc, T_scale>;
35 using Eigen::Matrix;
36 using std::log;
37 using std::vector;
38 static const char* function = "multi_student_t";
39 check_not_nan(function, "Degrees of freedom parameter", nu);
40 check_positive(function, "Degrees of freedom parameter", nu);
41
42 if (is_inf(nu)) {
43 return multi_normal_log(y, mu, Sigma);
44 }
45
46 size_t number_of_y = size_mvt(y);
47 size_t number_of_mu = size_mvt(mu);
48 if (number_of_y == 0 || number_of_mu == 0) {
49 return 0;
50 }
51 check_consistent_sizes_mvt(function, "y", y, "mu", mu);
52
53 vector_seq_view<T_y> y_vec(y);
54 vector_seq_view<T_loc> mu_vec(mu);
55 size_t size_vec = max_size_mvt(y, mu);
56
57 int size_y = y_vec[0].size();
58 int size_mu = mu_vec[0].size();
59 if (size_vec > 1) {
60 int size_y_old = size_y;
61 int size_y_new;
62 for (size_t i = 1, size_mvt_y = size_mvt(y); i < size_mvt_y; i++) {
63 int size_y_new = y_vec[i].size();
64 check_size_match(
65 function, "Size of one of the vectors of the random variable",
66 size_y_new, "Size of another vector of the random variable",
67 size_y_old);
68 size_y_old = size_y_new;
69 }
70 int size_mu_old = size_mu;
71 int size_mu_new;
72 for (size_t i = 1, size_mvt_mu = size_mvt(mu); i < size_mvt_mu; i++) {
73 int size_mu_new = mu_vec[i].size();
74 check_size_match(function,
75 "Size of one of the vectors "
76 "of the location variable",
77 size_mu_new,
78 "Size of another vector of "
79 "the location variable",
80 size_mu_old);
81 size_mu_old = size_mu_new;
82 }
83 (void)size_y_old;
84 (void)size_y_new;
85 (void)size_mu_old;
86 (void)size_mu_new;
87 }
88
89 check_size_match(function, "Size of random variable", size_y,
90 "size of location parameter", size_mu);
91 check_size_match(function, "Size of random variable", size_y,
92 "rows of scale parameter", Sigma.rows());
93 check_size_match(function, "Size of random variable", size_y,
94 "columns of scale parameter", Sigma.cols());
95
96 for (size_t i = 0; i < size_vec; i++) {
97 check_finite(function, "Location parameter", mu_vec[i]);
98 check_not_nan(function, "Random variable", y_vec[i]);
99 }
100 const auto& Sigma_ref = to_ref(Sigma);
101 check_symmetric(function, "Scale parameter", Sigma_ref);
102
103 auto ldlt_Sigma = make_ldlt_factor(Sigma_ref);
104 check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_Sigma);
105
106 if (size_y == 0) {
107 return 0;
108 }
109
110 lp_type lp(0);
111
112 if (include_summand<propto, T_dof>::value) {
113 lp += lgamma(0.5 * (nu + size_y)) * size_vec;
114 lp -= lgamma(0.5 * nu) * size_vec;
115 lp -= (0.5 * size_y) * log(nu) * size_vec;
116 }
117
118 if (include_summand<propto>::value) {
119 lp -= (0.5 * size_y) * LOG_PI * size_vec;
120 }
121
122 using Eigen::Array;
123
124 if (include_summand<propto, T_scale_elem>::value) {
125 lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
126 }
127
128 if (include_summand<propto, T_y, T_dof, T_loc, T_scale_elem>::value) {
129 lp_type sum_lp_vec(0.0);
130 for (size_t i = 0; i < size_vec; i++) {
131 const auto& y_col = as_column_vector_or_scalar(y_vec[i]);
132 const auto& mu_col = as_column_vector_or_scalar(mu_vec[i]);
133 sum_lp_vec
134 += log1p(trace_inv_quad_form_ldlt(ldlt_Sigma, y_col - mu_col) / nu);
135 }
136 lp -= 0.5 * (nu + size_y) * sum_lp_vec;
137 }
138 return lp;
139 }
140
141 template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
multi_student_t_lpdf(const T_y & y,const T_dof & nu,const T_loc & mu,const T_scale & Sigma)142 inline return_type_t<T_y, T_dof, T_loc, T_scale> multi_student_t_lpdf(
143 const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& Sigma) {
144 return multi_student_t_lpdf<false>(y, nu, mu, Sigma);
145 }
146
147 } // namespace math
148 } // namespace stan
149 #endif
150