Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_2_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_RNG_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_RNG_HPP
3
10#include <boost/random/gamma_distribution.hpp>
11#include <boost/random/poisson_distribution.hpp>
12#include <boost/random/variate_generator.hpp>
13
14namespace stan {
15namespace math {
16
35template <typename T_loc, typename T_prec, class RNG>
37neg_binomial_2_rng(const T_loc& mu, const T_prec& phi, RNG& rng) {
38 using boost::gamma_distribution;
39 using boost::variate_generator;
40 using boost::random::poisson_distribution;
41 using T_mu_ref = ref_type_t<T_loc>;
42 using T_phi_ref = ref_type_t<T_prec>;
43 static constexpr const char* function = "neg_binomial_2_rng";
44 check_consistent_sizes(function, "Location parameter", mu,
45 "Precision parameter", phi);
46 if (size_zero(mu, phi)) {
47 return {};
48 }
49 T_mu_ref mu_ref = mu;
50 T_phi_ref phi_ref = phi;
51 check_positive_finite(function, "Location parameter", mu_ref);
52 check_positive_finite(function, "Precision parameter", phi_ref);
53
54 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
55 scalar_seq_view<T_phi_ref> phi_vec(phi_ref);
56 size_t N = max_size(mu, phi);
58
59 for (size_t n = 0; n < N; ++n) {
60 double mu_div_phi = static_cast<double>(mu_vec[n]) / phi_vec[n];
61
62 // gamma_rng params must be positive and finite
63 check_positive_finite(function,
64 "Location parameter divided by the "
65 "precision parameter",
66 mu_div_phi);
67
68 double rng_from_gamma = variate_generator<RNG&, gamma_distribution<> >(
69 rng, gamma_distribution<>(phi_vec[n], mu_div_phi))();
70
71 // same as the constraints for poisson_rng
72 check_less(function, "Random number that came from gamma distribution",
73 rng_from_gamma, POISSON_MAX_RATE);
74 check_not_nan(function, "Random number that came from gamma distribution",
75 rng_from_gamma);
76 check_nonnegative(function,
77 "Random number that came from gamma distribution",
78 rng_from_gamma);
79
80 output[n] = variate_generator<RNG&, poisson_distribution<> >(
81 rng, poisson_distribution<>(rng_from_gamma))();
82 }
83
84 return output.data();
85}
86
87} // namespace math
88} // namespace stan
89#endif
typename helper::type type
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
VectorBuilder< true, int, T_loc, T_prec >::type neg_binomial_2_rng(const T_loc &mu, const T_prec &phi, RNG &rng)
Return a negative binomial random variate with the specified location and precision parameters using ...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
const double POISSON_MAX_RATE
Largest rate parameter allowed in Poisson RNG.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
void check_less(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not strictly less than high.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:56
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...