Automatic Differentiation
 
Loading...
Searching...
No Matches
inv_wishart_cholesky_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_INV_WISHART_CHOLESKY_RNG_HPP
2#define STAN_MATH_PRIM_PROB_INV_WISHART_CHOLESKY_RNG_HPP
3
7
8namespace stan {
9namespace math {
10
30template <class RNG>
31inline Eigen::MatrixXd inv_wishart_cholesky_rng(double nu,
32 const Eigen::MatrixXd& L_S,
33 RNG& rng) {
34 using Eigen::MatrixXd;
35 static constexpr const char* function = "inv_wishart_cholesky_rng";
36 index_type_t<MatrixXd> k = L_S.rows();
37 check_square(function, "Cholesky Scale matrix", L_S);
38 check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
39 check_cholesky_factor(function, "Cholesky Scale matrix", L_S);
40
41 MatrixXd B = MatrixXd::Zero(k, k);
42 for (int j = 0; j < k; ++j) {
43 for (int i = 0; i < j; ++i) {
44 B(j, i) = normal_rng(0, 1, rng);
45 }
46 B(j, j) = std::sqrt(chi_square_rng(nu - k + j + 1, rng));
47 }
48
49 return mdivide_right_tri_low(L_S, B);
50}
51
52} // namespace math
53} // namespace stan
54#endif
Eigen::MatrixXd inv_wishart_cholesky_rng(double nu, const Eigen::MatrixXd &L_S, RNG &rng)
Return a random Cholesky factor of a covariance matrix of the specified dimensionality drawn from the...
VectorBuilder< true, double, T_loc, T_scale >::type normal_rng(const T_loc &mu, const T_scale &sigma, RNG &rng)
Return a Normal random variate for the given location and scale using the specified random number gen...
VectorBuilder< true, double, T_deg >::type chi_square_rng(const T_deg &nu, RNG &rng)
Return a chi squared random variate with nu degrees of freedom using the specified random number gene...
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
Eigen::Matrix< value_type_t< EigMat1 >, EigMat1::RowsAtCompileTime, EigMat2::ColsAtCompileTime > mdivide_right_tri_low(const EigMat1 &A, const EigMat2 &b)
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.
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 ...
Definition fvar.hpp:9