neg_binomial_2_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_RNG_HPP
3 
13 #include <boost/random/gamma_distribution.hpp>
14 #include <boost/random/poisson_distribution.hpp>
15 #include <boost/random/variate_generator.hpp>
16 
17 namespace stan {
18 namespace math {
19 
20 /**
21  * Return a negative binomial random variate with the specified location and
22  * precision parameters using the given random number generator.
23  *
24  * mu and phi can each be a scalar or a one-dimensional container. Any
25  * non-scalar inputs must be the same size.
26  *
27  * @tparam T_loc Type of location parameter
28  * @tparam T_prec Type of precision parameter
29  * @tparam RNG type of random number generator
30  * @param mu (Sequence of) positive location parameter(s)
31  * @param phi (Sequence of) positive precision parameter(s)
32  * @param rng random number generator
33  * @return (Sequence of) negative binomial random variate(s)
34  * @throw std::domain_error if mu or phi are nonpositive
35  * @throw std::invalid_argument if non-scalar arguments are of different
36  * sizes
37  */
38 template <typename T_loc, typename T_prec, class RNG>
40 neg_binomial_2_rng(const T_loc& mu, const T_prec& phi, RNG& rng) {
41  using boost::gamma_distribution;
42  using boost::random::poisson_distribution;
43  using boost::variate_generator;
44 
45  static const char* function = "neg_binomial_2_rng";
46 
47  check_positive_finite(function, "Location parameter", mu);
48  check_positive_finite(function, "Precision parameter", phi);
49  check_consistent_sizes(function, "Location parameter", mu,
50  "Precision parameter", phi);
51 
52  scalar_seq_view<T_loc> mu_vec(mu);
53  scalar_seq_view<T_prec> phi_vec(phi);
54  size_t N = max_size(mu, phi);
56 
57  for (size_t n = 0; n < N; ++n) {
58  double mu_div_phi = static_cast<double>(mu_vec[n]) / phi_vec[n];
59 
60  // gamma_rng params must be positive and finite
61  check_positive_finite(function,
62  "Location parameter divided by the "
63  "precision parameter",
64  mu_div_phi);
65 
66  double rng_from_gamma = variate_generator<RNG&, gamma_distribution<> >(
67  rng, gamma_distribution<>(phi_vec[n], mu_div_phi))();
68 
69  // same as the constraints for poisson_rng
70  check_less(function, "Random number that came from gamma distribution",
71  rng_from_gamma, POISSON_MAX_RATE);
72  check_not_nan(function, "Random number that came from gamma distribution",
73  rng_from_gamma);
74  check_nonnegative(function,
75  "Random number that came from gamma distribution",
76  rng_from_gamma);
77 
78  output[n] = variate_generator<RNG&, poisson_distribution<> >(
79  rng, poisson_distribution<>(rng_from_gamma))();
80  }
81 
82  return output.data();
83 }
84 
85 } // namespace math
86 } // namespace stan
87 #endif
ofstream output
void check_nonnegative(const char *function, const char *name, const T_y &y)
void check_positive_finite(const char *function, const char *name, const T_y &y)
void check_not_nan(const char *function, const char *name, const T_y &y)
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
const double POISSON_MAX_RATE
Definition: constants.hpp:68
VectorBuilder< true, int, T_loc, T_prec >::type neg_binomial_2_rng(const T_loc &mu, const T_prec &phi, RNG &rng)
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
void check_less(const char *function, const char *name, const T_y &y, const T_high &high)
Definition: check_less.hpp:72