Automatic Differentiation
 
Loading...
Searching...
No Matches
wishart_cholesky_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_WISHART_CHOLESKY_RNG_HPP
2#define STAN_MATH_PRIM_PROB_WISHART_CHOLESKY_RNG_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13
30template <class RNG>
31inline Eigen::MatrixXd wishart_cholesky_rng(double nu,
32 const Eigen::MatrixXd& L_S,
33 RNG& rng) {
34 using Eigen::MatrixXd;
35 static constexpr const char* function = "wishart_cholesky_rng";
36 index_type_t<MatrixXd> k = L_S.rows();
37 check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
38 check_square(function, "Cholesky Scale matrix", L_S);
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(i, j) = normal_rng(0, 1, rng);
45 }
46 B(j, j) = std::sqrt(chi_square_rng(nu - j, rng));
47 }
48 return L_S.template triangularView<Eigen::Lower>() * B.transpose();
49}
50
51} // namespace math
52} // namespace stan
53#endif
Eigen::MatrixXd wishart_cholesky_rng(double nu, const Eigen::MatrixXd &L_S, RNG &rng)
Return a random Cholesky factor of the inverse covariance matrix of the specified dimensionality draw...
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
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 ...