Stan Math Library  2.14.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 
11 
12 namespace stan {
13  namespace math {
14 
15  template <class RNG>
16  inline Eigen::MatrixXd
17  wishart_rng(double nu, const Eigen::MatrixXd& S, RNG& rng) {
18  static const char* function("wishart_rng");
19 
20  using Eigen::MatrixXd;
21  typename index_type<MatrixXd>::type k = S.rows();
22 
23  check_square(function, "scale parameter", S);
24  check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
25 
26  MatrixXd B = MatrixXd::Zero(k, k);
27  for (int j = 0; j < k; ++j) {
28  for (int i = 0; i < j; ++i)
29  B(i, j) = normal_rng(0, 1, rng);
30  B(j, j) = std::sqrt(chi_square_rng(nu - j, rng));
31  }
32  return crossprod(B * S.llt().matrixU());
33  }
34 
35  }
36 }
37 #endif
Eigen::MatrixXd wishart_rng(double nu, const Eigen::MatrixXd &S, RNG &rng)
Definition: wishart_rng.hpp:17
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
double normal_rng(double mu, double sigma, RNG &rng)
Definition: normal_rng.hpp:18
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:17
void check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is square.
double chi_square_rng(double nu, RNG &rng)
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.

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