Automatic Differentiation
 
Loading...
Searching...
No Matches
wishart_cholesky_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_WISHART_CHOLESKY_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_WISHART_CHOLESKY_LPDF_HPP
3
11
12namespace stan {
13namespace math {
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>
43 const T_dof& nu,
44 const T_scale& L_S) {
45 using Eigen::Lower;
46 using T_L_Y_ref = ref_type_t<T_y>;
47 using T_nu_ref = ref_type_t<T_dof>;
48 using T_L_S_ref = ref_type_t<T_scale>;
50 static constexpr const char* function = "wishart_cholesky_lpdf";
51 Eigen::Index k = L_Y.rows();
52
53 check_greater(function, "Degrees of freedom parameter", nu, k - 1);
54
55 check_square(function, "Cholesky random variable", L_Y);
56 check_square(function, "Cholesky scale parameter", L_S);
57 check_size_match(function, "side length of random variable", L_Y.rows(),
58 "side length of scale parameter", L_S.rows());
59
60 T_L_Y_ref L_Y_ref = L_Y;
61 check_cholesky_factor(function, "Cholesky random variable", L_Y_ref);
62
63 T_L_S_ref L_S_ref = L_S;
64 check_cholesky_factor(function, "Cholesky scale matrix", L_S_ref);
65
66 T_nu_ref nu_ref = nu;
67 T_return lp(0.0);
68
70 lp += k * LOG_TWO * (1 - 0.5 * nu_ref);
71 lp += -lmgamma(k, 0.5 * nu_ref);
72 }
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;
78
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;
82 }
83 lp += -0.5 * dot_LSinvLY
84 + dot_product(linspaced_rv, log(L_Y_ref.diagonal()))
85 - nu_ref * sum(log(L_S_ref.diagonal()));
86 }
87 return lp;
88}
89
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);
94}
95
96} // namespace math
97} // namespace stan
98#endif
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)
Definition log.hpp:15
static constexpr double LOG_TWO
The natural logarithm of 2, .
Definition constants.hpp:80
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
fvar< return_type_t< T, int > > lmgamma(int x1, const fvar< T > &x2)
Definition lmgamma.hpp:14
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.
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.
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
Definition ref_type.hpp:55
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...