Automatic Differentiation
 
Loading...
Searching...
No Matches
inv_wishart_cholesky_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_INV_WISHART_CHOLESKY_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_INV_WISHART_CHOLESKY_LPDF_HPP
3
11
12namespace stan {
13namespace math {
14
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) {
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 = "inv_wishart_cholesky_lpdf";
51 Eigen::Index k = L_Y.rows();
52 check_greater(function, "Degrees of freedom parameter", nu, k - 1);
53
54 check_square(function, "Cholesky random variable", L_Y);
55 check_square(function, "Cholesky Scale parameter", L_S);
56 check_size_match(function, "side length of random variable", L_Y.rows(),
57 "side length of scale parameter", L_S.rows());
58
59 T_L_Y_ref L_Y_ref = L_Y;
60 check_cholesky_factor(function, "Cholesky random variable", L_Y_ref);
61
62 T_nu_ref nu_ref = nu;
63 T_L_S_ref L_S_ref = L_S;
64 check_cholesky_factor(function, "Cholesky Scale matrix", L_S_ref);
65
66 T_return lp(0.0);
67
69 lp += k * LOG_TWO * (1 - 0.5 * nu_ref);
70 lp -= lmgamma(k, 0.5 * nu_ref);
71 }
72
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;
78
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;
82 }
83
84 lp += -0.5 * dot_LYinvLS
85 - dot_product(linspaced_rv, log(L_Y_ref.diagonal()))
86 + nu_ref * sum(log(L_S_ref.diagonal()));
87 }
88
89 return lp;
90}
91
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);
96}
97
98} // namespace math
99} // namespace stan
100#endif
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)
Definition log.hpp:18
static constexpr double LOG_TWO
The natural logarithm of 2, .
Definition constants.hpp:80
fvar< return_type_t< T, int > > lmgamma(int x1, const fvar< T > &x2)
Definition lmgamma.hpp:14
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
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 ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...