Automatic Differentiation
 
Loading...
Searching...
No Matches
inv_wishart_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_INV_WISHART_RNG_HPP
2#define STAN_MATH_PRIM_PROB_INV_WISHART_RNG_HPP
3
8
9namespace stan {
10namespace math {
11
12template <class RNG>
13inline Eigen::MatrixXd inv_wishart_rng(double nu, const Eigen::MatrixXd& S,
14 RNG& rng) {
15 using Eigen::MatrixXd;
16 static constexpr const char* function = "inv_wishart_rng";
17 index_type_t<MatrixXd> k = S.rows();
18 check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
19 check_square(function, "scale parameter", S);
20 check_symmetric(function, "scale parameter", S);
21
22 MatrixXd S_inv = MatrixXd::Identity(k, k);
23 Eigen::LDLT<Eigen::MatrixXd> ldlt_of_S = S.ldlt();
24 check_pos_definite("inv_wishart_rng", "scale parameter", ldlt_of_S);
25 S_inv = ldlt_of_S.solve(S_inv);
26 MatrixXd asym = inverse_spd(wishart_rng(nu, S_inv, rng));
27 return 0.5 * (asym.transpose() + asym); // ensure symmetry
28}
29
30} // namespace math
31} // namespace stan
32#endif
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
typename index_type< T >::type index_type_t
void check_pos_definite(const char *function, const char *name, const EigMat &y)
Check if the specified square, symmetric matrix is positive definite.
Eigen::MatrixXd inv_wishart_rng(double nu, const Eigen::MatrixXd &S, RNG &rng)
Eigen::Matrix< value_type_t< EigMat >, Eigen::Dynamic, Eigen::Dynamic > inverse_spd(const EigMat &m)
Returns the inverse of the specified symmetric, pos/neg-definite matrix.
Eigen::MatrixXd wishart_rng(double nu, const Eigen::MatrixXd &S, RNG &rng)
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...