| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 <stan/math/prim/scal/err/check_finite.hpp>
5 #include <stan/math/prim/scal/err/check_positive.hpp>
6 #include <stan/math/prim/scal/fun/constants.hpp>
7 #include <stan/math/prim/scal/meta/include_summand.hpp>
8 #include <stan/math/prim/mat/err/check_pos_definite.hpp>
9 #include <stan/math/prim/mat/err/check_symmetric.hpp>
10 #include <stan/math/prim/mat/fun/trace_inv_quad_form_ldlt.hpp>
11 #include <stan/math/prim/mat/fun/log_determinant_ldlt.hpp>
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
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
trace_inv_quad_form_ldlt.hpp
check_symmetric.hpp
include_summand.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:66
stan::math::multi_normal_rng
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...
Definition: multi_normal_rng.hpp:35
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:60
constants.hpp
check_finite.hpp
stan::math::check_pos_definite
bool check_pos_definite(const char *function, const char *name, const Eigen::Matrix< T_y,-1,-1 > &y)
Return true if the specified square, symmetric matrix is positive definite.
Definition: check_pos_definite.hpp:33
stan::math::check_symmetric
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is symmetric.
Definition: check_symmetric.hpp:36
check_pos_definite.hpp
log_determinant_ldlt.hpp

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