wishart_rng.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_PROB_WISHART_RNG_HPP
#define STAN_MATH_PRIM_MAT_PROB_WISHART_RNG_HPP
#include <stan/math/prim/mat/err/check_square.hpp>
#include <stan/math/prim/mat/fun/crossprod.hpp>
#include <stan/math/prim/mat/meta/index_type.hpp>
#include <stan/math/prim/scal/err/check_positive.hpp>
#include <stan/math/prim/scal/err/check_greater.hpp>
#include <stan/math/prim/scal/prob/chi_square_rng.hpp>
#include <stan/math/prim/scal/prob/normal_rng.hpp>
namespace stan {
namespace math {
template <class RNG>
inline Eigen::MatrixXd
wishart_rng(double nu, const Eigen::MatrixXd& S, RNG& rng) {
static const char* function("wishart_rng");
using Eigen::MatrixXd;
typename index_type<MatrixXd>::type k = S.rows();
check_square(function, "scale parameter", S);
check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
MatrixXd B = MatrixXd::Zero(k, k);
for (int j = 0; j < k; ++j) {
for (int i = 0; i < j; ++i)
B(i, j) = normal_rng(0, 1, rng);
B(j, j) = std::sqrt(chi_square_rng(nu - j, rng));
}
return crossprod(B * S.llt().matrixU());
}
}
}
#endif
stan::math::wishart_rng
Eigen::MatrixXd wishart_rng(double nu, const Eigen::MatrixXd &S, RNG &rng)
Definition: wishart_rng.hpp:17
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
crossprod.hpp
stan::math::normal_rng
double normal_rng(double mu, double sigma, RNG &rng)
Definition: normal_rng.hpp:18
normal_rng.hpp
check_greater.hpp
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
index_type.hpp
stan::math::crossprod
Eigen::Matrix< fvar< T >, C, C > crossprod(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: crossprod.hpp:17
chi_square_rng.hpp
stan::math::check_square
void check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is square.
Definition: check_square.hpp:27
stan::math::chi_square_rng
double chi_square_rng(double nu, RNG &rng)
Definition: chi_square_rng.hpp:23
stan::math::check_greater
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Check if y is strictly greater than low.
Definition: check_greater.hpp:78
check_square.hpp

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