Automatic Differentiation
 
Loading...
Searching...
No Matches
inv_wishart_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_INV_WISHART_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_INV_WISHART_LPDF_HPP
3
11
12namespace stan {
13namespace math {
14
44template <bool propto, typename T_y, typename T_dof, typename T_scale>
46 const T_dof& nu,
47 const T_scale& S) {
48 using Eigen::Dynamic;
49 using Eigen::Matrix;
50 using T_W_ref = ref_type_t<T_y>;
51 using T_nu_ref = ref_type_t<T_dof>;
52 using T_S_ref = ref_type_t<T_scale>;
53 static constexpr const char* function = "inv_wishart_lpdf";
54 check_size_match(function, "Rows of random variable", W.rows(),
55 "columns of scale parameter", S.rows());
56 check_square(function, "random variable", W);
57 check_square(function, "scale parameter", S);
58 Eigen::Index k = S.rows();
59 T_nu_ref nu_ref = nu;
60 T_S_ref S_ref = S;
61 T_W_ref W_ref = W;
62 check_greater(function, "Degrees of freedom parameter", nu_ref, k - 1);
63 check_symmetric(function, "random variable", W_ref);
64 check_symmetric(function, "scale parameter", S_ref);
65
66 auto ldlt_W = make_ldlt_factor(W_ref);
67 check_ldlt_factor(function, "LDLT_Factor of random variable", ldlt_W);
68 auto ldlt_S = make_ldlt_factor(S_ref);
69 check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_S);
70
72
74 lp -= lmgamma(k, 0.5 * nu_ref);
75 }
77 lp += 0.5 * nu_ref * log_determinant_ldlt(ldlt_S);
78 }
80 lp -= 0.5 * (nu_ref + k + 1.0) * log_determinant_ldlt(ldlt_W);
81 }
83 lp -= 0.5 * trace(mdivide_left_ldlt(ldlt_W, S_ref));
84 }
86 lp -= nu_ref * k * HALF_LOG_TWO;
87 }
88 return lp;
89}
90
91template <typename T_y, typename T_dof, typename T_scale>
93 const T_dof& nu,
94 const T_scale& S) {
95 return inv_wishart_lpdf<false>(W, nu, S);
96}
97
98} // namespace math
99} // namespace stan
100#endif
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
return_type_t< T_y, T_dof, T_scale > inv_wishart_lpdf(const T_y &W, const T_dof &nu, const T_scale &S)
The log of the Inverse-Wishart density for the given W, degrees of freedom, and scale matrix.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
static constexpr double HALF_LOG_TWO
The value of half the natural logarithm 2, .
value_type_t< T > log_determinant_ldlt(LDLT_factor< T > &A)
Returns log(abs(det(A))) given a LDLT_factor of A.
auto make_ldlt_factor(const T &A)
Make an LDLT_factor with matrix type plain_type_t<T>
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
value_type_t< T > trace(const T &m)
Calculates trace (sum of diagonal) of given kernel generator expression.
Definition trace.hpp:22
Eigen::Matrix< value_type_t< EigMat >, Eigen::Dynamic, EigMat::ColsAtCompileTime > mdivide_left_ldlt(LDLT_factor< T > &A, const EigMat &b)
Returns the solution of the system Ax=b given an LDLT_factor of A.
fvar< return_type_t< T, int > > lmgamma(int x1, const fvar< T > &x2)
Definition lmgamma.hpp:14
void check_ldlt_factor(const char *function, const char *name, LDLT_factor< T > &A)
Raise domain error if the specified LDLT factor is invalid.
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.
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...