neg_binomial_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_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 shape and
22  * inverse scale parameters using the given random number generator.
23  *
24  * alpha and beta can each be a scalar or a one-dimensional container. Any
25  * non-scalar inputs must be the same size.
26  *
27  * @tparam T_shape Type of shape parameter
28  * @tparam T_inv Type of inverse scale parameter
29  * @tparam RNG type of random number generator
30  * @param alpha (Sequence of) positive shape parameter(s)
31  * @param beta (Sequence of) positive inverse scale parameter(s)
32  * @param rng random number generator
33  * @return (Sequence of) negative binomial random variate(s)
34  * @throw std::domain_error if alpha or beta are nonpositive
35  * @throw std::invalid_argument if non-scalar arguments are of different
36  * sizes
37  */
38 template <typename T_shape, typename T_inv, class RNG>
40  const T_shape& alpha, const T_inv& beta, 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_rng";
46 
47  // gamma_rng params must be positive and finite
48  check_positive_finite(function, "Shape parameter", alpha);
49  check_positive_finite(function, "Inverse scale parameter", beta);
50  check_consistent_sizes(function, "Shape parameter", alpha,
51  "Inverse scale Parameter", beta);
52 
53  scalar_seq_view<T_shape> alpha_vec(alpha);
54  scalar_seq_view<T_inv> beta_vec(beta);
55  size_t N = max_size(alpha, beta);
57 
58  for (size_t n = 0; n < N; ++n) {
59  double rng_from_gamma = variate_generator<RNG&, gamma_distribution<> >(
60  rng, gamma_distribution<>(alpha_vec[n], 1.0 / beta_vec[n]))();
61 
62  // same as the constraints for poisson_rng
63  check_less(function, "Random number that came from gamma distribution",
64  rng_from_gamma, POISSON_MAX_RATE);
65  check_not_nan(function, "Random number that came from gamma distribution",
66  rng_from_gamma);
67  check_nonnegative(function,
68  "Random number that came from gamma distribution",
69  rng_from_gamma);
70 
71  output[n] = variate_generator<RNG&, poisson_distribution<> >(
72  rng, poisson_distribution<>(rng_from_gamma))();
73  }
74 
75  return output.data();
76 }
77 
78 } // namespace math
79 } // namespace stan
80 #endif
ofstream output
VectorBuilder< true, int, T_shape, T_inv >::type neg_binomial_rng(const T_shape &alpha, const T_inv &beta, RNG &rng)
Double_t beta
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
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