binomial_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BINOMIAL_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BINOMIAL_RNG_HPP
3 
10 #include <boost/random/binomial_distribution.hpp>
11 #include <boost/random/variate_generator.hpp>
12 
13 namespace stan {
14 namespace math {
15 
16 /**
17  * Return a pseudorandom binomial random variable for the given population
18  * size and chance of success parameters using the specified random number
19  * generator.
20  *
21  * beta can be a scalar or a one-dimensional container.
22  *
23  * @tparam T_N Type of population size parameter
24  * @tparam T_theta Type of change of success parameter
25  * @tparam RNG class of rng
26  * @param N (Sequence of) population size parameter(s)
27  * @param theta (Sequence of) chance of success parameter(s)
28  * @param rng random number generator
29  * @return (Sequence of) binomial random variate(s)
30  * @throw std::domain_error if N is negative
31  * @throw std::domain_error if theta is not a valid probability
32  */
33 template <typename T_N, typename T_theta, class RNG>
35  const T_N& N, const T_theta& theta, RNG& rng) {
36  using boost::binomial_distribution;
37  using boost::variate_generator;
38 
39  static const char* function = "binomial_rng";
40 
41  check_nonnegative(function, "Population size parameter", N);
42  check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
43  check_consistent_sizes(function, "Population size parameter", N,
44  "Probability Parameter", theta);
45 
46  scalar_seq_view<T_N> N_vec(N);
47  scalar_seq_view<T_theta> theta_vec(theta);
48  size_t M = max_size(N, theta);
50 
51  for (size_t m = 0; m < M; ++m) {
52  variate_generator<RNG&, binomial_distribution<> > binomial_rng(
53  rng, binomial_distribution<>(N_vec[m], theta_vec[m]));
54 
55  output[m] = binomial_rng();
56  }
57 
58  return output.data();
59 }
60 
61 } // namespace math
62 } // namespace stan
63 #endif
ofstream output
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
void check_nonnegative(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
VectorBuilder< true, int, T_N, T_theta >::type binomial_rng(const T_N &N, const T_theta &theta, RNG &rng)
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)