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_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;
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>(
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;
159 lp += -
sum(
log(L_val.diagonal())) * size_vec;
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);
167 for (
size_t i = 0; i < size_vec; i++) {
170 y_val_minus_mu_val =
eval(y_val - mu_val);
172 half = mdivide_left_tri<Eigen::Lower>(L_val, y_val_minus_mu_val)
175 scaled_diff = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose();
177 T_partials_return dot_half =
dot_self(half);
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));
186 scaled_diff *= nu_plus_dims / (dot_half + nu_val);
189 partials_vec<0>(ops_partials)[i] += -scaled_diff;
193 partials_vec<2>(ops_partials)[i] += scaled_diff;
199 = (scaled_diff * half).
template triangularView<Eigen::Lower>();
202 += (scaled_diff * half).
template triangularView<Eigen::Lower>();
206 sum_lp_vec +=
log1p(dot_half * inv_nu);
210 L_deriv.diagonal().array() -= size_vec / L_val.diagonal().array();
211 partials<3>(ops_partials) += L_deriv;
213 lp += -0.5 * nu_plus_dims * sum_lp_vec;
215 return ops_partials.build(lp);
239template <
bool propto,
typename T_y,
typename T_dof,
typename T_loc,
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";
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>;
262 T_nu_ref nu_ref = nu;
263 T_mu_ref mu_ref = mu;
268 const int size_y = y_ref.size();
269 const int size_mu = mu_ref.size();
275 check_not_nan(function,
"Degrees of freedom parameter", nu_ref);
277 check_finite(function,
"Degrees of freedom parameter", nu_ref);
280 "rows of scale parameter", L.rows());
282 "size of location parameter", size_mu);
284 "rows of scale parameter", L.rows());
286 "columns of scale parameter", L.cols());
293 T_partials_return lp(0);
297 if (include_summand<propto>::value) {
298 lp += -0.5 * size_y *
LOG_PI;
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);
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);
314 if (!is_constant_all<T_dof>::value) {
315 T_partials_return half_nu = 0.5 * nu_val;
316 T_partials_return digamma_vals
318 T_partials_return G =
dot_product(scaled_diff, y_val_minus_mu_val);
320 partials<1>(ops_partials)
322 * (digamma_vals -
log1p(G * inv_nu) + (G - size_y) / (G + nu_val));
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);
331 if (include_summand<propto, T_covar_elem>::value) {
332 lp += -
sum(
log(L_val.diagonal()));
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);
338 if (!is_constant_all<T_y>::value) {
339 partials<0>(ops_partials) += -scaled_diff * scale_val;
341 if (!is_constant_all<T_loc>::value) {
342 partials<2>(ops_partials) += scaled_diff * scale_val;
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;
352 lp += -0.5 * nu_plus_dims *
log1p(dot_half * inv_nu);
355 return ops_partials.build(lp);
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);
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 ...
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...