1#ifndef STAN_MATH_PRIM_PROB_MATRIX_NORMAL_PREC_RNG_HPP
2#define STAN_MATH_PRIM_PROB_MATRIX_NORMAL_PREC_RNG_HPP
7#include <boost/random/normal_distribution.hpp>
8#include <boost/random/variate_generator.hpp>
33 const Eigen::MatrixXd &Sigma,
34 const Eigen::MatrixXd &D,
36 using boost::normal_distribution;
37 using boost::variate_generator;
38 static constexpr const char *function =
"matrix_normal_prec_rng";
46 "Rows of Sigma", Sigma.rows());
48 "Rows of D", D.rows());
51 Eigen::LDLT<Eigen::MatrixXd> Sigma_ldlt(Sigma);
56 Eigen::LDLT<Eigen::MatrixXd> D_ldlt(D);
83 rng, normal_distribution<>(0, 1));
87 Eigen::MatrixXd X(m, n);
88 Eigen::VectorXd row_stddev
89 = Sigma_ldlt.vectorD().array().inverse().sqrt().matrix();
90 Eigen::VectorXd col_stddev
91 = D_ldlt.vectorD().array().inverse().sqrt().matrix();
94 double stddev = row_stddev(
row) * col_stddev(
col);
106 Eigen::MatrixXd Y = Mu
107 + (Sigma_ldlt.transpositionsP().transpose()
108 * (D_ldlt.matrixU().solve(
109 (Sigma_ldlt.matrixU().solve(X)).transpose()))
111 * D_ldlt.transpositionsP());
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
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...
auto transpose(Arg &&a)
Transposes a kernel generator expression.
auto col(T_x &&x, size_t j)
Return the specified column of the specified kernel generator expression using start-at-1 indexing.
auto row(T_x &&x, size_t j)
Return the specified row of the specified kernel generator expression using start-at-1 indexing.
double std_normal_rng(RNG &rng)
Return a standard Normal random variate using the specified random number generator.
void check_pos_semidefinite(const char *function, const char *name, const EigMat &y)
Check if the specified matrix is positive definite.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...