Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
8 #include <boost/random/binomial_distribution.hpp>
9 #include <boost/random/variate_generator.hpp>
10 
11 namespace stan {
12 namespace math {
13 
31 template <typename T_N, typename T_theta, class RNG>
33  const T_N& N, const T_theta& theta, RNG& rng) {
34  using boost::binomial_distribution;
35  using boost::variate_generator;
36 
37  static const char* function = "binomial_rng";
38 
39  check_nonnegative(function, "Population size parameter", N);
40  check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
41  check_consistent_sizes(function, "Population size parameter", N,
42  "Probability Parameter", theta);
43 
44  scalar_seq_view<T_N> N_vec(N);
45  scalar_seq_view<T_theta> theta_vec(theta);
46  size_t M = max_size(N, theta);
48 
49  for (size_t m = 0; m < M; ++m) {
50  variate_generator<RNG&, binomial_distribution<> > binomial_rng(
51  rng, binomial_distribution<>(N_vec[m], theta_vec[m]));
52 
53  output[m] = binomial_rng();
54  }
55 
56  return output.data();
57 }
58 
59 } // namespace math
60 } // namespace stan
61 #endif
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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)
Return a pseudorandom binomial random variable for the given population size and chance of success pa...
VectorBuilder allocates type T1 values to be used as intermediate values.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.

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