| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

wishart_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_WISHART_RNG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_WISHART_RNG_HPP
3
4 #include <stan/math/prim/mat/err/check_square.hpp>
5 #include <stan/math/prim/mat/fun/crossprod.hpp>
6 #include <stan/math/prim/mat/meta/index_type.hpp>
7 #include <stan/math/prim/scal/err/check_positive.hpp>
8 #include <stan/math/prim/scal/err/check_greater.hpp>
9 #include <stan/math/prim/scal/prob/chi_square_rng.hpp>
10 #include <stan/math/prim/scal/prob/normal_rng.hpp>
11
12 namespace stan {
13 namespace math {
14
15 template <class RNG>
16 inline Eigen::MatrixXd
17 wishart_rng(double nu, const Eigen::MatrixXd& S, RNG& rng) {
18 static const char* function("wishart_rng");
19
20 using Eigen::MatrixXd;
21 typename index_type<MatrixXd>::type k = S.rows();
22
23 check_square(function, "scale parameter", S);
24 check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
25
26 MatrixXd B = MatrixXd::Zero(k, k);
27 for (int j = 0; j < k; ++j) {
28 for (int i = 0; i < j; ++i)
29 B(i, j) = normal_rng(0, 1, rng);
30 B(j, j) = std::sqrt(chi_square_rng(nu - j, rng));
31 }
32 return crossprod(B * S.llt().matrixU());
33 }
34
35 }
36 }
37 #endif
stan::math::wishart_rng
Eigen::MatrixXd wishart_rng(double nu, const Eigen::MatrixXd &S, RNG &rng)
Definition: wishart_rng.hpp:17
stan::math::chi_square_rng
double chi_square_rng(const double nu, RNG &rng)
Definition: chi_square_rng.hpp:23
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
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
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
Definition: check_square.hpp:28
stan::math::normal_rng
double normal_rng(const double mu, const double sigma, RNG &rng)
Definition: normal_rng.hpp:18
stan::math::check_greater
bool check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is strictly greater than low.
Definition: check_greater.hpp:84
check_square.hpp

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