Stan Math Library  2.14.0
reverse mode automatic differentiation
multi_normal_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_RNG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_RNG_HPP
3 
12 #include <boost/random/normal_distribution.hpp>
13 #include <boost/random/variate_generator.hpp>
14 
15 namespace stan {
16  namespace math {
17 
33  template <class RNG>
34  inline Eigen::VectorXd
35  multi_normal_rng(const Eigen::VectorXd& mu, const Eigen::MatrixXd& S,
36  RNG& rng) {
37  using boost::variate_generator;
38  using boost::normal_distribution;
39 
40  static const char* function("multi_normal_rng");
41 
42  check_positive(function, "Covariance matrix rows", S.rows());
43  check_symmetric(function, "Covariance matrix", S);
44  check_finite(function, "Location parameter", mu);
45 
46  Eigen::LLT<Eigen::MatrixXd> llt_of_S = S.llt();
47  check_pos_definite("multi_normal_rng", "covariance matrix argument",
48  llt_of_S);
49 
50  variate_generator<RNG&, normal_distribution<> >
51  std_normal_rng(rng, normal_distribution<>(0, 1));
52 
53  Eigen::VectorXd z(S.cols());
54  for (int i = 0; i < S.cols(); i++)
55  z(i) = std_normal_rng();
56 
57  return mu + llt_of_S.matrixL() * z;
58  }
59 
60  }
61 }
62 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
void check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is symmetric.
Eigen::VectorXd multi_normal_rng(const Eigen::VectorXd &mu, const Eigen::MatrixXd &S, RNG &rng)
Return a pseudo-random vector with a multi-variate normal distribution given the specified location p...
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_pos_definite(const char *function, const char *name, const Eigen::Matrix< T_y, -1, -1 > &y)
Check if the specified square, symmetric matrix is positive definite.

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