1#ifndef STAN_MATH_PRIM_PROB_WISHART_CHOLESKY_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_WISHART_CHOLESKY_LPDF_HPP
39template <
bool propto,
typename T_y,
typename T_dof,
typename T_scale,
40 require_stan_scalar_t<T_dof>* =
nullptr,
41 require_all_matrix_t<T_y, T_scale>* =
nullptr>
50 static constexpr const char* function =
"wishart_cholesky_lpdf";
51 Eigen::Index k = L_Y.rows();
53 check_greater(function,
"Degrees of freedom parameter", nu, k - 1);
58 "side length of scale parameter", L_S.rows());
60 T_L_Y_ref L_Y_ref = L_Y;
63 T_L_S_ref L_S_ref = L_S;
70 lp += k *
LOG_TWO * (1 - 0.5 * nu_ref);
71 lp += -
lmgamma(k, 0.5 * nu_ref);
74 auto L_SinvL_Y = mdivide_left_tri<Eigen::Lower>(L_S_ref, L_Y_ref);
75 T_return dot_LSinvLY(0.0);
76 Eigen::Matrix<T_return, 1, Eigen::Dynamic> linspaced_rv(k);
77 T_return nu_minus_1 = nu_ref - 1;
79 for (
int i = 0; i < k; i++) {
80 dot_LSinvLY +=
dot_self(L_SinvL_Y.row(i).head(i + 1));
81 linspaced_rv(i) = nu_minus_1 - i;
83 lp += -0.5 * dot_LSinvLY
85 - nu_ref *
sum(
log(L_S_ref.diagonal()));
90template <
typename T_y,
typename T_dof,
typename T_scale>
92 const T_y& LW,
const T_dof& nu,
const T_scale& L_S) {
93 return wishart_cholesky_lpdf<false>(LW, nu, L_S);
return_type_t< T_y, T_dof, T_scale > wishart_cholesky_lpdf(const T_y &L_Y, const T_dof &nu, const T_scale &L_S)
Return the natural logarithm of the unnormalized Wishart density of the specified lower-triangular Ch...
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...