Stan Math Library  2.15.0
reverse mode automatic differentiation
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 
16 
17 #include <cmath>
18 
19 namespace stan {
20  namespace math {
21 
43  template <class RNG>
44  inline Eigen::VectorXd
45  dirichlet_rng(const Eigen::Matrix<double, Eigen::Dynamic, 1>& alpha,
46  RNG& rng) {
47  using boost::variate_generator;
48  using boost::gamma_distribution;
49  using boost::random::uniform_real_distribution;
50  using Eigen::VectorXd;
51  using std::exp;
52  using std::log;
53 
54  // separate algorithm if any parameter is less than 1
55  if (alpha.minCoeff() < 1) {
56  variate_generator<RNG&, uniform_real_distribution<> >
57  uniform_rng(rng, uniform_real_distribution<>(0.0, 1.0));
58  VectorXd log_y(alpha.size());
59  for (int i = 0; i < alpha.size(); ++i) {
60  variate_generator<RNG&, gamma_distribution<> >
61  gamma_rng(rng, gamma_distribution<>(alpha(i) + 1, 1));
62  double log_u = log(uniform_rng());
63  log_y(i) = log(gamma_rng()) + log_u / alpha(i);
64  }
65  double log_sum_y = log_sum_exp(log_y);
66  VectorXd theta(alpha.size());
67  for (int i = 0; i < alpha.size(); ++i)
68  theta(i) = exp(log_y(i) - log_sum_y);
69  return theta;
70  }
71 
72  // standard normalized gamma algorithm
73  Eigen::VectorXd y(alpha.rows());
74  for (int i = 0; i < alpha.rows(); i++) {
75  variate_generator<RNG&, gamma_distribution<> >
76  gamma_rng(rng, gamma_distribution<>(alpha(i, 0), 1e-7));
77  y(i) = gamma_rng();
78  }
79  return y / y.sum();
80  }
81 
82  }
83 }
84 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:13
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...
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
double gamma_rng(double alpha, double beta, RNG &rng)
Definition: gamma_rng.hpp:28
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
double uniform_rng(double alpha, double beta, RNG &rng)
Definition: uniform_rng.hpp:20

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