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
137 if constexpr (include_summand<propto>::value) {
138 lp += -0.5 * num_dims * LOG_PI * size_vec;
139 }
140
141 if constexpr (include_summand<propto, T_y, T_dof, T_loc,
142 T_covar_elem>::value) {
143 T_partials_return nu_val = value_of(nu_ref);
144 T_partials_return inv_nu = inv(nu_val);
145 T_partials_return nu_plus_dims = nu_val + num_dims;
146 matrix_partials_t L_val = value_of(L_ref);
147 matrix_partials_t L_deriv;
148 const auto& half_nu
149 = to_ref_if<include_summand<propto, T_dof>::value>(0.5 * nu_val);
150 const auto& digamma_vals = to_ref_if<is_autodiff_v<T_dof>>(
151 digamma(half_nu + 0.5 * num_dims) - digamma(half_nu));
152
154 lp += lgamma(0.5 * nu_plus_dims) * size_vec;
155 lp += -lgamma(0.5 * nu_val) * size_vec;
156 lp += -(0.5 * num_dims) * log(nu_val) * size_vec;
157 }
158
160 lp += -sum(log(L_val.diagonal())) * size_vec;
161 }
162
163 T_partials_return sum_lp_vec(0.0);
164 row_vector_partials_t half(size_y);
165 vector_partials_t y_val_minus_mu_val(size_y);
166 vector_partials_t scaled_diff(size_y);
167
168 for (size_t i = 0; i < size_vec; i++) {
169 decltype(auto) y_val = as_value_column_vector_or_scalar(y_vec[i]);
170 decltype(auto) mu_val = as_value_column_vector_or_scalar(mu_vec[i]);
171 y_val_minus_mu_val = eval(y_val - mu_val);
172
173 half = mdivide_left_tri<Eigen::Lower>(L_val, y_val_minus_mu_val)
174 .transpose();
175
176 scaled_diff = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose();
177
178 T_partials_return dot_half = dot_self(half);
179
180 if constexpr (is_autodiff_v<T_dof>) {
181 T_partials_return G = dot_product(scaled_diff, y_val_minus_mu_val);
182 partials<1>(ops_partials)[i] += 0.5
183 * (digamma_vals - log1p(G * inv_nu)
184 + (G - num_dims) / (G + nu_val));
185 }
186
187 scaled_diff *= nu_plus_dims / (dot_half + nu_val);
188
189 if constexpr (is_autodiff_v<T_y>) {
190 partials_vec<0>(ops_partials)[i] += -scaled_diff;
191 }
192
193 if constexpr (is_autodiff_v<T_loc>) {
194 partials_vec<2>(ops_partials)[i] += scaled_diff;
195 }
196
197 if constexpr (is_autodiff_v<T_covar_elem>) {
198 if (i == 0) {
199 L_deriv
200 = (scaled_diff * half).template triangularView<Eigen::Lower>();
201 } else {
202 L_deriv
203 += (scaled_diff * half).template triangularView<Eigen::Lower>();
204 }
205 }
206
207 sum_lp_vec += log1p(dot_half * inv_nu);
208 }
209
210 if constexpr (is_autodiff_v<T_covar_elem>) {
211 L_deriv.diagonal().array() -= size_vec / L_val.diagonal().array();
212 partials<3>(ops_partials) += L_deriv;
213 }
214 lp += -0.5 * nu_plus_dims * sum_lp_vec;
215 }
216 return ops_partials.build(lp);
217}
218
240template <bool propto, typename T_y, typename T_dof, typename T_loc,
241 typename T_covar,
244 T_y, T_dof, T_loc, T_covar>* = nullptr>
246 const T_y& y, const T_dof& nu, const T_loc& mu, const T_covar& L) {
247 static const char* function = "multi_student_t_cholesky";
248 using T_covar_elem = typename scalar_type<T_covar>::type;
249 using Eigen::Matrix;
251 using T_partials_return = partials_return_t<T_y, T_dof, T_loc, T_covar>;
252 using matrix_partials_t
253 = Eigen::Matrix<T_partials_return, Eigen::Dynamic, Eigen::Dynamic>;
254 using vector_partials_t = Eigen::Matrix<T_partials_return, Eigen::Dynamic, 1>;
255 using row_vector_partials_t
256 = Eigen::Matrix<T_partials_return, 1, Eigen::Dynamic>;
257 using T_y_ref = ref_type_t<T_y>;
258 using T_nu_ref = ref_type_t<T_dof>;
259 using T_mu_ref = ref_type_t<T_loc>;
260 using T_L_ref = ref_type_t<T_covar>;
261
262 T_y_ref y_ref = y;
263 T_nu_ref nu_ref = nu;
264 T_mu_ref mu_ref = mu;
265 T_L_ref L_ref = L;
266 vector_partials_t y_val = as_value_column_vector_or_scalar(y_ref);
267 vector_partials_t mu_val = as_value_column_vector_or_scalar(mu_ref);
268
269 const int size_y = y_ref.size();
270 const int size_mu = mu_ref.size();
271
272 if (unlikely(size_y == 0)) {
273 return T_return(0);
274 }
275
276 check_not_nan(function, "Degrees of freedom parameter", nu_ref);
277 check_positive(function, "Degrees of freedom parameter", nu_ref);
278 check_finite(function, "Degrees of freedom parameter", nu_ref);
279
280 check_size_match(function, "Size of random variable", size_mu,
281 "rows of scale parameter", L.rows());
282 check_size_match(function, "Size of random variable", size_y,
283 "size of location parameter", size_mu);
284 check_size_match(function, "Size of random variable", size_y,
285 "rows of scale parameter", L.rows());
286 check_size_match(function, "Size of random variable", size_y,
287 "columns of scale parameter", L.cols());
288
289 check_finite(function, "Location parameter", mu_val);
290 check_not_nan(function, "Random variable", y_val);
291
292 check_cholesky_factor(function, "scale parameter", L_ref);
293
294 T_partials_return lp(0);
295
296 auto ops_partials = make_partials_propagator(y_ref, nu_ref, mu_ref, L_ref);
297
298 if constexpr (include_summand<propto>::value) {
299 lp += -0.5 * size_y * LOG_PI;
300 }
301
302 if constexpr (include_summand<propto, T_y, T_dof, T_loc,
303 T_covar_elem>::value) {
304 T_partials_return nu_val = value_of(nu_ref);
305 T_partials_return inv_nu = inv(nu_val);
306 T_partials_return nu_plus_dims = nu_val + size_y;
307 vector_partials_t y_val_minus_mu_val = eval(y_val - mu_val);
308
309 matrix_partials_t L_val = value_of(L_ref);
310 row_vector_partials_t half
311 = mdivide_left_tri<Eigen::Lower>(L_val, y_val_minus_mu_val).transpose();
312 vector_partials_t scaled_diff
313 = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose();
314 T_partials_return dot_half = dot_self(half);
315
316 if constexpr (is_autodiff_v<T_dof>) {
317 T_partials_return half_nu = 0.5 * nu_val;
318 T_partials_return digamma_vals
319 = digamma(half_nu + 0.5 * size_y) - digamma(half_nu);
320 T_partials_return G = dot_product(scaled_diff, y_val_minus_mu_val);
321
322 partials<1>(ops_partials)
323 += 0.5
324 * (digamma_vals - log1p(G * inv_nu) + (G - size_y) / (G + nu_val));
325 }
326
327 if constexpr (include_summand<propto, T_dof>::value) {
328 lp += lgamma(0.5 * (nu_val + size_y));
329 lp += -lgamma(0.5 * nu_val);
330 lp += -0.5 * size_y * log(nu_val);
331 }
332
333 if constexpr (include_summand<propto, T_covar_elem>::value) {
334 lp += -sum(log(L_val.diagonal()));
335 }
336
337 if constexpr (is_any_autodiff_v<T_y, T_loc, T_covar_elem>) {
338 T_partials_return scale_val = nu_plus_dims / (dot_half + nu_val);
339
340 if constexpr (is_autodiff_v<T_y>) {
341 partials<0>(ops_partials) += -scaled_diff * scale_val;
342 }
343 if constexpr (is_autodiff_v<T_loc>) {
344 partials<2>(ops_partials) += scaled_diff * scale_val;
345 }
346 if constexpr (is_autodiff_v<T_covar_elem>) {
347 matrix_partials_t L_deriv
348 = (scaled_diff * half).template triangularView<Eigen::Lower>();
349 L_deriv.diagonal().array() -= 1 / L_val.diagonal().array();
350 edge<3>(ops_partials).partials_ += L_deriv;
351 }
352 }
353
354 lp += -0.5 * nu_plus_dims * log1p(dot_half * inv_nu);
355 }
356
357 return ops_partials.build(lp);
358}
359
360template <typename T_y, typename T_dof, typename T_loc, typename T_covar>
362 const T_y& y, const T_dof& nu, const T_loc& mu, const T_covar& L) {
363 return multi_student_t_cholesky_lpdf<false>(y, nu, mu, L);
364}
365
366} // namespace math
367} // namespace stan
368#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.
int64_t size_mvt(const ScalarT &)
Provides the size of a multivariate argument.
Definition size_mvt.hpp:25
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_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:19
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:18
int64_t max_size_mvt(const T1 &x1, const Ts &... xs)
Calculate the size of the largest multivariate input.
static constexpr double LOG_PI
The natural logarithm of , .
Definition constants.hpp:86
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
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:13
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:56
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 ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
std::decay_t< T > type