1#ifndef STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_CHOLESKY_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_CHOLESKY_LPDF_HPP
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";
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>;
77 if (num_y == 0 || num_mu == 0) {
89 check_not_nan(function,
"Degrees of freedom parameter", nu_ref);
91 check_finite(function,
"Degrees of freedom parameter", nu_ref);
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();
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());
109 for (
size_t i = 1, size_mvt_mu = num_mu; i < size_mvt_mu; i++) {
111 "Size of one of the vectors "
112 "of the location variable",
114 "Size of another vector of "
115 "the location variable",
116 mu_vec[i - 1].
size());
120 "rows of scale parameter", L.rows());
122 "size of location parameter", size_mu);
124 "rows of scale parameter", L.rows());
126 "columns of scale parameter", L.cols());
128 for (
size_t i = 0; i < size_vec; i++) {
129 check_finite(function,
"Location parameter", mu_vec[i]);
135 T_partials_return lp(0);
138 lp += -0.5 * num_dims *
LOG_PI * size_vec;
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;
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>>(
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;
160 lp += -
sum(
log(L_val.diagonal())) * size_vec;
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);
168 for (
size_t i = 0; i < size_vec; i++) {
171 y_val_minus_mu_val =
eval(y_val - mu_val);
173 half = mdivide_left_tri<Eigen::Lower>(L_val, y_val_minus_mu_val)
176 scaled_diff = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose();
178 T_partials_return dot_half =
dot_self(half);
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));
187 scaled_diff *= nu_plus_dims / (dot_half + nu_val);
189 if constexpr (is_autodiff_v<T_y>) {
190 partials_vec<0>(ops_partials)[i] += -scaled_diff;
193 if constexpr (is_autodiff_v<T_loc>) {
194 partials_vec<2>(ops_partials)[i] += scaled_diff;
197 if constexpr (is_autodiff_v<T_covar_elem>) {
200 = (scaled_diff * half).
template triangularView<Eigen::Lower>();
203 += (scaled_diff * half).
template triangularView<Eigen::Lower>();
207 sum_lp_vec +=
log1p(dot_half * inv_nu);
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;
214 lp += -0.5 * nu_plus_dims * sum_lp_vec;
216 return ops_partials.build(lp);
240template <
bool propto,
typename T_y,
typename T_dof,
typename T_loc,
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";
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>;
263 T_nu_ref nu_ref = nu;
264 T_mu_ref mu_ref = mu;
269 const int size_y = y_ref.size();
270 const int size_mu = mu_ref.size();
276 check_not_nan(function,
"Degrees of freedom parameter", nu_ref);
278 check_finite(function,
"Degrees of freedom parameter", nu_ref);
281 "rows of scale parameter", L.rows());
283 "size of location parameter", size_mu);
285 "rows of scale parameter", L.rows());
287 "columns of scale parameter", L.cols());
294 T_partials_return lp(0);
298 if constexpr (include_summand<propto>::value) {
299 lp += -0.5 * size_y *
LOG_PI;
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);
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);
316 if constexpr (is_autodiff_v<T_dof>) {
317 T_partials_return half_nu = 0.5 * nu_val;
318 T_partials_return digamma_vals
320 T_partials_return G =
dot_product(scaled_diff, y_val_minus_mu_val);
322 partials<1>(ops_partials)
324 * (digamma_vals -
log1p(G * inv_nu) + (G - size_y) / (G + nu_val));
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);
333 if constexpr (include_summand<propto, T_covar_elem>::value) {
334 lp += -
sum(
log(L_val.diagonal()));
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);
340 if constexpr (is_autodiff_v<T_y>) {
341 partials<0>(ops_partials) += -scaled_diff * scale_val;
343 if constexpr (is_autodiff_v<T_loc>) {
344 partials<2>(ops_partials) += scaled_diff * scale_val;
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;
354 lp += -0.5 * nu_plus_dims *
log1p(dot_half * inv_nu);
357 return ops_partials.build(lp);
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);
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
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.
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>>.
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 ...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
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 , .
fvar< T > log1p(const fvar< T > &x)
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.
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.
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.
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)
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.
typename ref_type_if< true, T >::type ref_type_t
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...