Stan Math Library  2.20.0
reverse mode automatic differentiation
wishart_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_WISHART_RNG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_WISHART_RNG_HPP
3 
10 
11 namespace stan {
12 namespace math {
13 
14 template <class RNG>
15 inline Eigen::MatrixXd wishart_rng(double nu, const Eigen::MatrixXd& S,
16  RNG& rng) {
17  static const char* function = "wishart_rng";
18 
19  using Eigen::MatrixXd;
20  typename index_type<MatrixXd>::type k = S.rows();
21 
22  check_square(function, "scale parameter", S);
23  check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
24 
25  MatrixXd B = MatrixXd::Zero(k, k);
26  for (int j = 0; j < k; ++j) {
27  for (int i = 0; i < j; ++i)
28  B(i, j) = normal_rng(0, 1, rng);
29  B(j, j) = std::sqrt(chi_square_rng(nu - j, rng));
30  }
31  return crossprod(B * S.llt().matrixU());
32 }
33 
34 } // namespace math
35 } // namespace stan
36 #endif
Eigen::MatrixXd wishart_rng(double nu, const Eigen::MatrixXd &S, RNG &rng)
Definition: wishart_rng.hpp:15
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:13
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
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...
Definition: normal_rng.hpp:33
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
Eigen::Matrix< fvar< T >, C, C > crossprod(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: crossprod.hpp:12
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Check if y is strictly greater than low.
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...

     [ Stan Home Page ] © 2011–2018, Stan Development Team.