1 #ifndef STAN_MATH_PRIM_MAT_PROB_MATRIX_NORMAL_PREC_RNG_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_MATRIX_NORMAL_PREC_RNG_HPP 11 #include <boost/random/normal_distribution.hpp> 12 #include <boost/random/variate_generator.hpp> 37 const Eigen::MatrixXd &Sigma,
38 const Eigen::MatrixXd &D,
40 using boost::normal_distribution;
41 using boost::variate_generator;
43 static const char *
function =
"matrix_normal_prec_rng";
54 "Rows of Sigma", Sigma.rows());
56 "Rows of D", D.rows());
60 Eigen::LDLT<Eigen::MatrixXd> Sigma_ldlt(Sigma);
65 Eigen::LDLT<Eigen::MatrixXd> D_ldlt(D);
91 variate_generator<RNG &, normal_distribution<>> std_normal_rng(
92 rng, normal_distribution<>(0, 1));
96 Eigen::MatrixXd X(m, n);
97 Eigen::VectorXd row_stddev
98 = Sigma_ldlt.vectorD().array().inverse().sqrt().matrix();
99 Eigen::VectorXd col_stddev
100 = D_ldlt.vectorD().array().inverse().sqrt().matrix();
103 double stddev = row_stddev(
row) * col_stddev(
col);
105 X(
row,
col) = stddev * std_normal_rng();
115 Eigen::MatrixXd Y = Mu
116 + (Sigma_ldlt.transpositionsP().transpose()
117 * (D_ldlt.matrixU().solve(
118 (Sigma_ldlt.matrixU().solve(X)).
transpose()))
120 * D_ldlt.transpositionsP());
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
void check_pos_semidefinite(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is positive definite.
Eigen::Matrix< T, 1, Eigen::Dynamic > row(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m, size_t i)
Return the specified row of the specified matrix, using start-at-1 indexing.
Eigen::MatrixXd matrix_normal_prec_rng(const Eigen::MatrixXd &Mu, const Eigen::MatrixXd &Sigma, const Eigen::MatrixXd &D, RNG &rng)
Sample from the the matrix normal distribution for the given Mu, Sigma and D where Sigma and D are gi...
Eigen::Matrix< T, Eigen::Dynamic, 1 > col(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m, size_t j)
Return the specified column of the specified matrix using start-at-1 indexing.
matrix_cl transpose(const matrix_cl &src)
Takes the transpose of the matrix on the OpenCL device.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_symmetric(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is symmetric.