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 
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 log-location
22  * and inverse dispersion parameters using the given random number generator.
23  *
24  * eta 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 log-location parameter
28  * @tparam T_inv Type of inverse overdispersion parameter
29  * @tparam RNG type of random number generator
30  * @param eta (Sequence of) positive log-location parameter(s)
31  * @param phi (Sequence of) positive inverse dispersion parameter(s)
32  * @param rng random number generator
33  * @return (Sequence of) negative binomial random variate(s)
34  * @throw std::domain_error if eta is non-finite or phi is nonpositive
35  * @throw std::invalid_argument if non-scalar arguments are of different
36  * sizes
37  */
38 template <typename T_loc, typename T_inv, class RNG>
40 neg_binomial_2_log_rng(const T_loc& eta, const T_inv& 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_log_rng";
46 
47  check_finite(function, "Log-location parameter", eta);
48  check_positive_finite(function, "Inverse dispersion parameter", phi);
49  check_consistent_sizes(function, "Log-location parameter", eta,
50  "Inverse dispersion parameter", phi);
51 
52  scalar_seq_view<T_loc> eta_vec(eta);
53  scalar_seq_view<T_inv> phi_vec(phi);
54  size_t N = max_size(eta, phi);
56 
57  for (size_t n = 0; n < N; ++n) {
58  double exp_eta_div_phi
59  = std::exp(static_cast<double>(eta_vec[n])) / phi_vec[n];
60 
61  // gamma_rng params must be positive and finite
62  check_positive_finite(function,
63  "Exponential of the log-location parameter "
64  "divided by the precision parameter",
65  exp_eta_div_phi);
66 
67  double rng_from_gamma = variate_generator<RNG&, gamma_distribution<> >(
68  rng, gamma_distribution<>(phi_vec[n], exp_eta_div_phi))();
69 
70  // same as the constraints for poisson_rng
71  check_less(function, "Random number that came from gamma distribution",
72  rng_from_gamma, POISSON_MAX_RATE);
73  check_not_nan(function, "Random number that came from gamma distribution",
74  rng_from_gamma);
75  check_nonnegative(function,
76  "Random number that came from gamma distribution",
77  rng_from_gamma);
78 
79  output[n] = variate_generator<RNG&, poisson_distribution<> >(
80  rng, poisson_distribution<>(rng_from_gamma))();
81  }
82 
83  return output.data();
84 }
85 
86 } // namespace math
87 } // namespace stan
88 #endif
ofstream output
void check_finite(const char *function, const char *name, const T_y &y)
VectorBuilder< true, int, T_loc, T_inv >::type neg_binomial_2_log_rng(const T_loc &eta, const T_inv &phi, RNG &rng)
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)
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
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