dirichlet_rng.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_PROB_DIRICHLET_RNG_HPP
#define STAN_MATH_PRIM_MAT_PROB_DIRICHLET_RNG_HPP
#include <boost/math/special_functions/gamma.hpp>
#include <boost/random/gamma_distribution.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include <boost/random/variate_generator.hpp>
#include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
#include <stan/math/prim/scal/err/check_positive.hpp>
#include <stan/math/prim/mat/err/check_simplex.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/mat/fun/log_sum_exp.hpp>
#include <stan/math/prim/scal/fun/multiply_log.hpp>
#include <stan/math/prim/scal/meta/include_summand.hpp>
#include <cmath>
namespace stan {
namespace math {
template <class RNG>
inline Eigen::VectorXd
dirichlet_rng(const Eigen::Matrix<double, Eigen::Dynamic, 1>& alpha,
RNG& rng) {
using boost::variate_generator;
using boost::gamma_distribution;
using boost::random::uniform_real_distribution;
using Eigen::VectorXd;
using std::exp;
using std::log;
// separate algorithm if any parameter is less than 1
if (alpha.minCoeff() < 1) {
variate_generator<RNG&, uniform_real_distribution<> >
uniform_rng(rng, uniform_real_distribution<>(0.0, 1.0));
VectorXd log_y(alpha.size());
for (int i = 0; i < alpha.size(); ++i) {
variate_generator<RNG&, gamma_distribution<> >
gamma_rng(rng, gamma_distribution<>(alpha(i) + 1, 1));
double log_u = log(uniform_rng());
log_y(i) = log(gamma_rng()) + log_u / alpha(i);
}
double log_sum_y = log_sum_exp(log_y);
VectorXd theta(alpha.size());
for (int i = 0; i < alpha.size(); ++i)
theta(i) = exp(log_y(i) - log_sum_y);
return theta;
}
// standard normalized gamma algorithm
Eigen::VectorXd y(alpha.rows());
for (int i = 0; i < alpha.rows(); i++) {
variate_generator<RNG&, gamma_distribution<> >
gamma_rng(rng, gamma_distribution<>(alpha(i, 0), 1e-7));
y(i) = gamma_rng();
}
return y / y.sum();
}
}
}
#endif
check_simplex.hpp
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
stan::math::log_sum_exp
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:13
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:45
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::gamma_rng
double gamma_rng(double alpha, double beta, RNG &rng)
Definition: gamma_rng.hpp:29
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
constants.hpp
log_sum_exp.hpp
stan::math::uniform_rng
double uniform_rng(double alpha, double beta, RNG &rng)
Definition: uniform_rng.hpp:20
check_consistent_sizes.hpp

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