1#ifndef STAN_MATH_PRIM_PROB_INV_WISHART_CHOLESKY_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_INV_WISHART_CHOLESKY_LPDF_HPP
40template <
bool propto,
typename T_y,
typename T_dof,
typename T_scale,
41 require_stan_scalar_t<T_dof>* =
nullptr,
42 require_all_matrix_t<T_y, T_scale>* =
nullptr>
44 const T_y& L_Y,
const T_dof& nu,
const T_scale& L_S) {
50 static constexpr const char* function =
"inv_wishart_cholesky_lpdf";
51 Eigen::Index k = L_Y.rows();
52 check_greater(function,
"Degrees of freedom parameter", nu, k - 1);
57 "side length of scale parameter", L_S.rows());
59 T_L_Y_ref L_Y_ref = L_Y;
63 T_L_S_ref L_S_ref = L_S;
69 lp += k *
LOG_TWO * (1 - 0.5 * nu_ref);
74 auto L_YinvL_S = mdivide_left_tri<Eigen::Lower>(L_Y_ref, L_S_ref);
75 T_return dot_LYinvLS(0.0);
76 Eigen::Matrix<T_return, 1, Eigen::Dynamic> linspaced_rv(k);
77 T_return nu_plus_1 = nu_ref + 1;
79 for (
int i = 0; i < k; i++) {
80 dot_LYinvLS +=
dot_self(L_YinvL_S.row(i).head(i + 1));
81 linspaced_rv(i) = nu_plus_1 + i;
84 lp += -0.5 * dot_LYinvLS
86 + nu_ref *
sum(
log(L_S_ref.diagonal()));
92template <
typename T_y,
typename T_dof,
typename T_scale>
94 const T_y& L_Y,
const T_dof& nu,
const T_scale& L_S) {
95 return inv_wishart_cholesky_lpdf<false>(L_Y, nu, L_S);
return_type_t< T_y, T_dof, T_scale > inv_wishart_cholesky_lpdf(const T_y &L_Y, const T_dof &nu, const T_scale &L_S)
Return the natural logarithm of the unnormalized inverse wishart density of the specified lower-trian...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
fvar< T > log(const fvar< T > &x)
static constexpr double LOG_TWO
The natural logarithm of 2, .
fvar< return_type_t< T, int > > lmgamma(int x1, const fvar< T > &x2)
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.
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.
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not strictly greater than low.
typename ref_type_if< true, T >::type ref_type_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...