Automatic Differentiation
 
Loading...
Searching...
No Matches
multi_student_t_cholesky_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_CHOLESKY_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_CHOLESKY_LPDF_HPP
3
25#include <cmath>
26#include <cstdlib>
27#include <iostream>
28
29namespace stan {
30namespace math {
52template <
53 bool propto, typename T_y, typename T_dof, typename T_loc, typename T_covar,
54 require_any_not_vector_vt<is_stan_scalar, T_y, T_dof, T_loc>* = nullptr,
56 T_y, T_loc, T_covar>* = nullptr>
58 const T_y& y, const T_dof& nu, const T_loc& mu, const T_covar& L) {
59 static constexpr const char* function = "multi_student_t_cholesky";
60 using T_covar_elem = typename scalar_type<T_covar>::type;
62 using T_partials_return = partials_return_t<T_y, T_dof, T_loc, T_covar>;
63 using matrix_partials_t
64 = Eigen::Matrix<T_partials_return, Eigen::Dynamic, Eigen::Dynamic>;
65 using vector_partials_t = Eigen::Matrix<T_partials_return, Eigen::Dynamic, 1>;
66 using row_vector_partials_t
67 = Eigen::Matrix<T_partials_return, 1, Eigen::Dynamic>;
68 using T_y_ref = ref_type_t<T_y>;
69 using T_nu_ref = ref_type_t<T_dof>;
70 using T_mu_ref = ref_type_t<T_loc>;
71 using T_L_ref = ref_type_t<T_covar>;
72
73 check_consistent_sizes_mvt(function, "y", y, "mu", mu);
74 size_t num_y = size_mvt(y);
75 size_t num_mu = size_mvt(mu);
76
77 if (num_y == 0 || num_mu == 0) {
78 return 0;
79 }
80
81 T_y_ref y_ref = y;
82 T_nu_ref nu_ref = nu;
83 T_mu_ref mu_ref = mu;
84 T_L_ref L_ref = L;
85 vector_seq_view<T_y_ref> y_vec(y_ref);
86 vector_seq_view<T_mu_ref> mu_vec(mu_ref);
87 const size_t size_vec = max_size_mvt(y_ref, mu_ref);
88
89 check_not_nan(function, "Degrees of freedom parameter", nu_ref);
90 check_positive(function, "Degrees of freedom parameter", nu_ref);
91 check_finite(function, "Degrees of freedom parameter", nu_ref);
92 check_cholesky_factor(function, "scale parameter", L_ref);
93
94 const int size_y = y_vec[0].size();
95 const int size_mu = mu_vec[0].size();
96 const int num_dims = L.rows();
97
98 if (unlikely(num_dims == 0)) {
99 return T_return(0);
100 }
101
102 for (size_t i = 1, size_mvt_y = num_y; i < size_mvt_y; i++) {
104 function, "Size of one of the vectors of the random variable",
105 y_vec[i].size(), "Size of another vector of the random variable",
106 y_vec[i - 1].size());
107 }
108
109 for (size_t i = 1, size_mvt_mu = num_mu; i < size_mvt_mu; i++) {
110 check_size_match(function,
111 "Size of one of the vectors "
112 "of the location variable",
113 mu_vec[i].size(),
114 "Size of another vector of "
115 "the location variable",
116 mu_vec[i - 1].size());
117 }
118
119 check_size_match(function, "Size of random variable", size_mu,
120 "rows of scale parameter", L.rows());
121 check_size_match(function, "Size of random variable", size_y,
122 "size of location parameter", size_mu);
123 check_size_match(function, "Size of random variable", size_y,
124 "rows of scale parameter", L.rows());
125 check_size_match(function, "Size of random variable", size_y,
126 "columns of scale parameter", L.cols());
127
128 for (size_t i = 0; i < size_vec; i++) {
129 check_finite(function, "Location parameter", mu_vec[i]);
130 check_not_nan(function, "Random variable", y_vec[i]);
131 }
132
133 auto ops_partials = make_partials_propagator(y_ref, nu_ref, mu_ref, L_ref);
134
135 T_partials_return lp(0);
136
138 lp += -0.5 * num_dims * LOG_PI * size_vec;
139 }
140
142 T_partials_return nu_val = value_of(nu_ref);
143 T_partials_return inv_nu = inv(nu_val);
144 T_partials_return nu_plus_dims = nu_val + num_dims;
145 matrix_partials_t L_val = value_of(L_ref);
146 matrix_partials_t L_deriv;
147 const auto& half_nu
148 = to_ref_if<include_summand<propto, T_dof>::value>(0.5 * nu_val);
149 const auto& digamma_vals = to_ref_if<!is_constant<T_dof>::value>(
150 digamma(half_nu + 0.5 * num_dims) - digamma(half_nu));
151
153 lp += lgamma(0.5 * nu_plus_dims) * size_vec;
154 lp += -lgamma(0.5 * nu_val) * size_vec;
155 lp += -(0.5 * num_dims) * log(nu_val) * size_vec;
156 }
157
159 lp += -sum(log(L_val.diagonal())) * size_vec;
160 }
161
162 T_partials_return sum_lp_vec(0.0);
163 row_vector_partials_t half(size_y);
164 vector_partials_t y_val_minus_mu_val(size_y);
165 vector_partials_t scaled_diff(size_y);
166
167 for (size_t i = 0; i < size_vec; i++) {
168 decltype(auto) y_val = as_value_column_vector_or_scalar(y_vec[i]);
169 decltype(auto) mu_val = as_value_column_vector_or_scalar(mu_vec[i]);
170 y_val_minus_mu_val = eval(y_val - mu_val);
171
172 half = mdivide_left_tri<Eigen::Lower>(L_val, y_val_minus_mu_val)
173 .transpose();
174
175 scaled_diff = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose();
176
177 T_partials_return dot_half = dot_self(half);
178
180 T_partials_return G = dot_product(scaled_diff, y_val_minus_mu_val);
181 partials<1>(ops_partials)[i] += 0.5
182 * (digamma_vals - log1p(G * inv_nu)
183 + (G - num_dims) / (G + nu_val));
184 }
185
186 scaled_diff *= nu_plus_dims / (dot_half + nu_val);
187
189 partials_vec<0>(ops_partials)[i] += -scaled_diff;
190 }
191
193 partials_vec<2>(ops_partials)[i] += scaled_diff;
194 }
195
197 if (i == 0) {
198 L_deriv
199 = (scaled_diff * half).template triangularView<Eigen::Lower>();
200 } else {
201 L_deriv
202 += (scaled_diff * half).template triangularView<Eigen::Lower>();
203 }
204 }
205
206 sum_lp_vec += log1p(dot_half * inv_nu);
207 }
208
210 L_deriv.diagonal().array() -= size_vec / L_val.diagonal().array();
211 partials<3>(ops_partials) += L_deriv;
212 }
213 lp += -0.5 * nu_plus_dims * sum_lp_vec;
214 }
215 return ops_partials.build(lp);
216}
217
239template <bool propto, typename T_y, typename T_dof, typename T_loc,
240 typename T_covar,
243 T_y, T_dof, T_loc, T_covar>* = nullptr>
245 const T_y& y, const T_dof& nu, const T_loc& mu, const T_covar& L) {
246 static const char* function = "multi_student_t_cholesky";
247 using T_covar_elem = typename scalar_type<T_covar>::type;
248 using Eigen::Matrix;
250 using T_partials_return = partials_return_t<T_y, T_dof, T_loc, T_covar>;
251 using matrix_partials_t
252 = Eigen::Matrix<T_partials_return, Eigen::Dynamic, Eigen::Dynamic>;
253 using vector_partials_t = Eigen::Matrix<T_partials_return, Eigen::Dynamic, 1>;
254 using row_vector_partials_t
255 = Eigen::Matrix<T_partials_return, 1, Eigen::Dynamic>;
256 using T_y_ref = ref_type_t<T_y>;
257 using T_nu_ref = ref_type_t<T_dof>;
258 using T_mu_ref = ref_type_t<T_loc>;
259 using T_L_ref = ref_type_t<T_covar>;
260
261 T_y_ref y_ref = y;
262 T_nu_ref nu_ref = nu;
263 T_mu_ref mu_ref = mu;
264 T_L_ref L_ref = L;
265 vector_partials_t y_val = as_value_column_vector_or_scalar(y_ref);
266 vector_partials_t mu_val = as_value_column_vector_or_scalar(mu_ref);
267
268 const int size_y = y_ref.size();
269 const int size_mu = mu_ref.size();
270
271 if (unlikely(size_y == 0)) {
272 return T_return(0);
273 }
274
275 check_not_nan(function, "Degrees of freedom parameter", nu_ref);
276 check_positive(function, "Degrees of freedom parameter", nu_ref);
277 check_finite(function, "Degrees of freedom parameter", nu_ref);
278
279 check_size_match(function, "Size of random variable", size_mu,
280 "rows of scale parameter", L.rows());
281 check_size_match(function, "Size of random variable", size_y,
282 "size of location parameter", size_mu);
283 check_size_match(function, "Size of random variable", size_y,
284 "rows of scale parameter", L.rows());
285 check_size_match(function, "Size of random variable", size_y,
286 "columns of scale parameter", L.cols());
287
288 check_finite(function, "Location parameter", mu_val);
289 check_not_nan(function, "Random variable", y_val);
290
291 check_cholesky_factor(function, "scale parameter", L_ref);
292
293 T_partials_return lp(0);
294
295 auto ops_partials = make_partials_propagator(y_ref, nu_ref, mu_ref, L_ref);
296
297 if (include_summand<propto>::value) {
298 lp += -0.5 * size_y * LOG_PI;
299 }
300
301 if (include_summand<propto, T_y, T_dof, T_loc, T_covar_elem>::value) {
302 T_partials_return nu_val = value_of(nu_ref);
303 T_partials_return inv_nu = inv(nu_val);
304 T_partials_return nu_plus_dims = nu_val + size_y;
305 vector_partials_t y_val_minus_mu_val = eval(y_val - mu_val);
306
307 matrix_partials_t L_val = value_of(L_ref);
308 row_vector_partials_t half
309 = mdivide_left_tri<Eigen::Lower>(L_val, y_val_minus_mu_val).transpose();
310 vector_partials_t scaled_diff
311 = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose();
312 T_partials_return dot_half = dot_self(half);
313
314 if (!is_constant_all<T_dof>::value) {
315 T_partials_return half_nu = 0.5 * nu_val;
316 T_partials_return digamma_vals
317 = digamma(half_nu + 0.5 * size_y) - digamma(half_nu);
318 T_partials_return G = dot_product(scaled_diff, y_val_minus_mu_val);
319
320 partials<1>(ops_partials)
321 += 0.5
322 * (digamma_vals - log1p(G * inv_nu) + (G - size_y) / (G + nu_val));
323 }
324
325 if (include_summand<propto, T_dof>::value) {
326 lp += lgamma(0.5 * (nu_val + size_y));
327 lp += -lgamma(0.5 * nu_val);
328 lp += -0.5 * size_y * log(nu_val);
329 }
330
331 if (include_summand<propto, T_covar_elem>::value) {
332 lp += -sum(log(L_val.diagonal()));
333 }
334
335 if (!is_constant_all<T_y, T_loc, T_covar_elem>::value) {
336 T_partials_return scale_val = nu_plus_dims / (dot_half + nu_val);
337
338 if (!is_constant_all<T_y>::value) {
339 partials<0>(ops_partials) += -scaled_diff * scale_val;
340 }
341 if (!is_constant_all<T_loc>::value) {
342 partials<2>(ops_partials) += scaled_diff * scale_val;
343 }
344 if (!is_constant_all<T_covar_elem>::value) {
345 matrix_partials_t L_deriv
346 = (scaled_diff * half).template triangularView<Eigen::Lower>();
347 L_deriv.diagonal().array() -= 1 / L_val.diagonal().array();
348 edge<3>(ops_partials).partials_ += L_deriv;
349 }
350 }
351
352 lp += -0.5 * nu_plus_dims * log1p(dot_half * inv_nu);
353 }
354
355 return ops_partials.build(lp);
356}
357
358template <typename T_y, typename T_dof, typename T_loc, typename T_covar>
360 const T_y& y, const T_dof& nu, const T_loc& mu, const T_covar& L) {
361 return multi_student_t_cholesky_lpdf<false>(y, nu, mu, L);
362}
363
364} // namespace math
365} // namespace stan
366#endif
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
#define unlikely(x)
return_type_t< T_y, T_dof, T_loc, T_covar > multi_student_t_cholesky_lpdf(const T_y &y, const T_dof &nu, const T_loc &mu, const T_covar &L)
The log of the multivariate student t density for the given y, mu, nu, and a Cholesky factor L of the...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_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.
require_all_t< container_type_check_base< is_vector, value_type_t, TypeCheck, Check >... > require_all_vector_vt
Require all of the types satisfy is_vector.
void check_consistent_sizes_mvt(const char *)
Trivial no input case, this function is a no-op.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
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.
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_cholesky_factor(const char *function, const char *name, const Mat &y)
Throw an exception if the specified matrix is not a valid Cholesky factor.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
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.
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
auto as_value_column_vector_or_scalar(T &&a)
Extract values from input argument and transform to a column vector.
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:12
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
std::decay_t< T > type