Stan Math Library  2.11.0
reverse mode automatic differentiation
neg_binomial_2_log_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_LOG_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_LOG_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>
21 
22 namespace stan {
23 
24  namespace math {
25 
26  template <class RNG>
27  inline int
28  neg_binomial_2_log_rng(const double eta,
29  const double phi,
30  RNG& rng) {
31  using boost::variate_generator;
32  using boost::random::negative_binomial_distribution;
33  using boost::random::poisson_distribution;
34  using boost::gamma_distribution;
35 
36  static const char* function("stan::math::neg_binomial_2_log_rng");
37 
38  check_finite(function, "Log-location parameter", eta);
39  check_positive_finite(function, "Precision parameter", phi);
40 
41  double exp_eta_div_phi = std::exp(eta)/phi;
42 
43  // gamma_rng params must be positive and finite
44  check_positive_finite(function,
45  "Exponential of the log-location parameter divided by "
46  "the precision parameter", exp_eta_div_phi);
47 
48  double rng_from_gamma =
49  variate_generator<RNG&, gamma_distribution<> >
50  (rng, gamma_distribution<>(phi, exp_eta_div_phi))();
51 
52  // same as the constraints for poisson_rng
53  check_less(function,
54  "Random number that came from gamma distribution",
55  rng_from_gamma, POISSON_MAX_RATE);
56  check_not_nan(function,
57  "Random number that came from gamma distribution",
58  rng_from_gamma);
59  check_nonnegative(function,
60  "Random number that came from gamma distribution",
61  rng_from_gamma);
62 
63  return variate_generator<RNG&, poisson_distribution<> >
64  (rng, poisson_distribution<>(rng_from_gamma))();
65  }
66  }
67 }
68 #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.
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
const double POISSON_MAX_RATE
Largest rate parameter allowed in Poisson RNG.
Definition: constants.hpp:72
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
int neg_binomial_2_log_rng(const double eta, const double phi, RNG &rng)
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–2016, Stan Development Team.