1#ifndef STAN_MATH_PRIM_PROB_CATEGORICAL_LOGIT_RNG_HPP
2#define STAN_MATH_PRIM_PROB_CATEGORICAL_LOGIT_RNG_HPP
10#include <boost/random/uniform_01.hpp>
11#include <boost/random/variate_generator.hpp>
37 using boost::uniform_01;
38 using boost::variate_generator;
39 static constexpr const char* function =
"categorical_logit_rng";
46 auto is_pos_inf = (
beta.array() ==
INFTY);
47 int num_infty = is_pos_inf.count();
49 Eigen::VectorXd theta;
54 theta = is_pos_inf.template cast<double>() / num_infty;
59 variate_generator<RNG&, uniform_01<> > uniform01_rng(rng, uniform_01<>());
63 int last = theta.size() - 1;
64 while (theta(last) == 0) {
67 double c = uniform01_rng();
69 while (b < last && c >= index(b)) {
int categorical_logit_rng(const Eigen::VectorXd &beta, RNG &rng)
Return a draw from a Categorical distribution given a vector of unnormalized log probabilities and a ...
static constexpr double NEGATIVE_INFTY
Negative infinity.
auto softmax(T &&x)
Return the softmax of each vector in a container of fvar values.
auto cumulative_sum(T_vec &&v)
Return the cumulative sum of the specified vector.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
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.
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...