Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 
4 #include <boost/math/special_functions/digamma.hpp>
5 #include <boost/random/negative_binomial_distribution.hpp>
6 #include <boost/random/variate_generator.hpp>
23 
24 namespace stan {
25 
26  namespace math {
27 
28  template <class RNG>
29  inline int
30  neg_binomial_rng(const double alpha,
31  const double beta,
32  RNG& rng) {
33  using boost::variate_generator;
34  using boost::random::negative_binomial_distribution;
35  using boost::random::poisson_distribution;
36  using boost::gamma_distribution;
37 
38  static const char* function("stan::math::neg_binomial_rng");
39 
40  // gamma_rng params must be positive and finite
41  check_positive_finite(function, "Shape parameter", alpha);
42  check_positive_finite(function, "Inverse scale parameter", beta);
43 
44  double rng_from_gamma =
45  variate_generator<RNG&, gamma_distribution<> >
46  (rng, gamma_distribution<>(alpha, 1.0 / beta))();
47 
48  // same as the constraints for poisson_rng
49  check_less(function,
50  "Random number that came from gamma distribution",
51  rng_from_gamma, POISSON_MAX_RATE);
52  check_not_nan(function,
53  "Random number that came from gamma distribution",
54  rng_from_gamma);
55  check_nonnegative(function,
56  "Random number that came from gamma distribution",
57  rng_from_gamma);
58 
59  return variate_generator<RNG&, poisson_distribution<> >
60  (rng, poisson_distribution<>(rng_from_gamma))();
61  }
62  }
63 }
64 #endif
bool check_less(const char *function, const char *name, const T_y &y, const T_high &high)
Return true if y is strictly less than high.
Definition: check_less.hpp:81
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
int neg_binomial_rng(const double alpha, const double beta, RNG &rng)
const double POISSON_MAX_RATE
Largest rate parameter allowed in Poisson RNG.
Definition: constants.hpp:72
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.

     [ Stan Home Page ] © 2011–2015, Stan Development Team.