| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

dirichlet_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_DIRICHLET_RNG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_DIRICHLET_RNG_HPP
3
4 #include <boost/math/special_functions/gamma.hpp>
5 #include <boost/random/gamma_distribution.hpp>
6 #include <boost/random/uniform_real_distribution.hpp>
7 #include <boost/random/variate_generator.hpp>
8
9 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
10 #include <stan/math/prim/scal/err/check_positive.hpp>
11 #include <stan/math/prim/mat/err/check_simplex.hpp>
12 #include <stan/math/prim/scal/fun/constants.hpp>
13 #include <stan/math/prim/mat/fun/log_sum_exp.hpp>
14 #include <stan/math/prim/scal/fun/multiply_log.hpp>
15 #include <stan/math/prim/scal/meta/include_summand.hpp>
16
17 #include <cmath>
18
19 namespace stan {
20
21 namespace math {
22
44 template <class RNG>
45 inline Eigen::VectorXd
46 dirichlet_rng(const Eigen::Matrix<double, Eigen::Dynamic, 1>& alpha,
47 RNG& rng) {
48 using boost::variate_generator;
49 using boost::gamma_distribution;
50 using boost::random::uniform_real_distribution;
51 using Eigen::VectorXd;
52 using std::exp;
53 using std::log;
54
55 // separate algorithm if any parameter is less than 1
56 if (alpha.minCoeff() < 1) {
57 variate_generator<RNG&, uniform_real_distribution<> >
58 uniform_rng(rng, uniform_real_distribution<>(0.0, 1.0));
59 VectorXd log_y(alpha.size());
60 for (int i = 0; i < alpha.size(); ++i) {
61 variate_generator<RNG&, gamma_distribution<> >
62 gamma_rng(rng, gamma_distribution<>(alpha(i) + 1, 1));
63 double log_u = log(uniform_rng());
64 log_y(i) = log(gamma_rng()) + log_u / alpha(i);
65 }
66 double log_sum_y = log_sum_exp(log_y);
67 VectorXd theta(alpha.size());
68 for (int i = 0; i < alpha.size(); ++i)
69 theta(i) = exp(log_y(i) - log_sum_y);
70 return theta;
71 }
72
73 // standard normalized gamma algorithm
74 Eigen::VectorXd y(alpha.rows());
75 for (int i = 0; i < alpha.rows(); i++) {
76 variate_generator<RNG&, gamma_distribution<> >
77 gamma_rng(rng, gamma_distribution<>(alpha(i, 0), 1e-7));
78 y(i) = gamma_rng();
79 }
80 return y / y.sum();
81 }
82
83 }
84 }
85 #endif
check_simplex.hpp
stan::math::gamma_rng
double gamma_rng(const double alpha, const double beta, RNG &rng)
Definition: gamma_rng.hpp:30
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::log_sum_exp
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:14
include_summand.hpp
multiply_log.hpp
stan::math::dirichlet_rng
Eigen::VectorXd dirichlet_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &alpha, RNG &rng)
Return a draw from a Dirichlet distribution with specified parameters and pseudo-random number genera...
Definition: dirichlet_rng.hpp:46
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::uniform_rng
double uniform_rng(const double alpha, const double beta, RNG &rng)
Definition: uniform_rng.hpp:21
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
constants.hpp
log_sum_exp.hpp
check_consistent_sizes.hpp

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