1#ifndef STAN_MATH_PRIM_PROB_MULTINOMIAL_RNG_HPP
2#define STAN_MATH_PRIM_PROB_MULTINOMIAL_RNG_HPP
13template <
class T_theta,
class RNG,
14 require_eigen_col_vector_t<T_theta>* =
nullptr>
16 static constexpr const char* function =
"multinomial_rng";
17 const auto& theta_ref =
to_ref(theta);
18 check_simplex(function,
"Probabilities parameter", theta_ref);
21 std::vector<int> result(theta.size(), 0);
22 double mass_left = 1.0;
24 for (
int k = 0; n_left > 0 && k < theta.size(); ++k) {
25 double p = theta_ref.coeff(k) / mass_left;
31 mass_left -= theta_ref(k);
VectorBuilder< true, int, T_N, T_theta >::type binomial_rng(const T_N &N, const T_theta &theta, RNG &rng)
Return a pseudorandom binomial random variable for the given population size and chance of success pa...
std::vector< int > multinomial_rng(const T_theta &theta, int N, RNG &rng)
void check_simplex(const char *function, const char *name, const T &theta)
Throw an exception if the specified vector is not a simplex.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...