Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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 
4 #include <boost/random/normal_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
14 
17 
18 namespace stan {
19 
20  namespace math {
21  using Eigen::Dynamic;
22 
23  template <class RNG>
24  inline Eigen::VectorXd
25  multi_normal_rng(const Eigen::Matrix<double, Dynamic, 1>& mu,
26  const Eigen::Matrix<double, Dynamic, Dynamic>& S,
27  RNG& rng) {
28  using boost::variate_generator;
29  using boost::normal_distribution;
30 
31  static const char* function("stan::math::multi_normal_rng");
32 
36 
37  check_positive(function, "Covariance matrix rows", S.rows());
38  check_symmetric(function, "Covariance matrix", S);
39  check_finite(function, "Location parameter", mu);
40 
41  variate_generator<RNG&, normal_distribution<> >
42  std_normal_rng(rng, normal_distribution<>(0, 1));
43 
44  Eigen::VectorXd z(S.cols());
45  for (int i = 0; i < S.cols(); i++)
46  z(i) = std_normal_rng();
47 
48  return mu + S.llt().matrixL() * z;
49  }
50  }
51 }
52 
53 #endif
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Dynamic, Dynamic > &y)
Return true if the specified matrix is symmetric.
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Eigen::VectorXd multi_normal_rng(const Eigen::Matrix< double, Dynamic, 1 > &mu, const Eigen::Matrix< double, Dynamic, Dynamic > &S, RNG &rng)

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