Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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 
22 
23 namespace stan {
24 
25  namespace math {
26 
27  template <class RNG>
28  inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
29  wishart_rng(const double nu,
30  const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& S,
31  RNG& rng) {
32  using Eigen::MatrixXd;
37 
38  static const char* function("stan::math::wishart_rng");
39 
40  typename index_type<MatrixXd>::type k = S.rows();
41 
42  check_positive(function, "degrees of freedom", nu);
43  check_square(function, "scale parameter", S);
44 
45  MatrixXd B = MatrixXd::Zero(k, k);
46 
47  for (int j = 0; j < k; ++j) {
48  for (int i = 0; i < j; ++i)
49  B(i, j) = normal_rng(0, 1, rng);
50  B(j, j) = std::sqrt(chi_square_rng(nu - j, rng));
51  }
52 
53  return stan::math::crossprod(B * S.llt().matrixU());
54  }
55 
56 
57  }
58 
59 }
60 #endif
double chi_square_rng(const double nu, RNG &rng)
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > wishart_rng(const double nu, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > &S, RNG &rng)
Definition: wishart_rng.hpp:29
Eigen::Matrix< fvar< T >, C, C > crossprod(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: crossprod.hpp:17
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
double normal_rng(const double mu, const double sigma, RNG &rng)
Definition: normal_rng.hpp:20

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