Automatic Differentiation
 
Loading...
Searching...
No Matches
dirichlet_multinomial_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_DIRICHLET_MULTINOMIAL_RNG_HPP
2#define STAN_MATH_PRIM_PROB_DIRICHLET_MULTINOMIAL_RNG_HPP
3
9#include <vector>
10
11namespace stan {
12namespace math {
13
36template <class RNG>
37inline std::vector<int> dirichlet_multinomial_rng(
38 const Eigen::Matrix<double, Eigen::Dynamic, 1>& alpha, int N, RNG& rng) {
39 static const char* function = "dirichlet_multinomial_rng";
40 const auto& alpha_ref = to_ref(alpha);
41 check_positive_finite(function, "prior size variable", alpha_ref);
42 check_nonnegative(function, "number of trials variables", N);
43
44 // special case N = 0
45 if (N == 0) {
46 return std::vector<int>(alpha.size(), 0);
47 }
48
49 // sample a simplex theta from the Dirichlet distribution
50 auto theta = dirichlet_rng(alpha_ref, rng);
51
52 // using the simplex theta, sample from the multinomial distribution
53 return multinomial_rng(theta, N, rng);
54}
55
56} // namespace math
57} // namespace stan
58#endif
std::vector< int > dirichlet_multinomial_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &alpha, int N, RNG &rng)
Return a draw from a Dirichlet-Multinomial distribution with specified parameters and and pseudo-ra...
std::vector< int > multinomial_rng(const T_theta &theta, int N, RNG &rng)
Return a draw from a Multinomial distribution given a probability simplex, a total count,...
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...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...