Automatic Differentiation
 
Loading...
Searching...
No Matches
multinomial_logit_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_MULTINOMIAL_LOGIT_RNG_HPP
2#define STAN_MATH_PRIM_PROB_MULTINOMIAL_LOGIT_RNG_HPP
3
10#include <vector>
11
12namespace stan {
13namespace math {
14
37template <class RNG, typename T_beta,
38 require_eigen_col_vector_t<T_beta>* = nullptr>
39inline std::vector<int> multinomial_logit_rng(const T_beta& beta, int N,
40 RNG& rng) {
41 static constexpr const char* function = "multinomial_logit_rng";
42 const auto& beta_ref = to_ref(beta);
43 check_nonnegative(function, "number of trials variables", N);
44 if (beta_ref.size() == 0) {
45 return {};
46 }
47 // Throws in nan and all -inf case
48 check_not_nan(function, "Log-probabilities parameter", beta_ref);
49 check_greater(function, "Log-probabilities parameter", beta_ref.maxCoeff(),
51
52 auto is_pos_inf = (beta_ref.array() == INFTY);
53 int num_infty = is_pos_inf.count();
54
56
57 // softmax is NaN with +inf, so split probability evenly over the
58 // +inf entries and give every other entry zero probability
59 if (num_infty > 0) {
60 theta = is_pos_inf.template cast<double>() / num_infty;
61 } else {
62 theta = softmax(beta_ref);
63 }
64
65 std::vector<int> result(theta.size(), 0);
66 double mass_left = 1.0;
67 int n_left = N;
68
69 for (int k = 0; n_left > 0 && k < theta.size(); ++k) {
70 double p = theta.coeff(k) / mass_left;
71 if (p > 1.0) {
72 p = 1.0;
73 }
74 result[k] = binomial_rng(n_left, p, rng);
75 n_left -= result[k];
76 mass_left -= theta.coeff(k);
77 }
78
79 return result;
80} // namespace math
81
82} // namespace math
83} // namespace stan
84#endif
std::vector< int > multinomial_logit_rng(const T_beta &beta, int N, RNG &rng)
Return a draw from a Multinomial distribution given a vector of unnormalized log probabilities,...
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...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:49
auto softmax(T &&x)
Return the softmax of each vector in a container of fvar values.
Definition softmax.hpp:23
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition beta.hpp:51
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not strictly greater than low.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:44
typename plain_type< std::decay_t< T > >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...