Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BINOMIAL_RNG_HPP
2#define STAN_MATH_PRIM_PROB_BINOMIAL_RNG_HPP
3
8#include <boost/random/binomial_distribution.hpp>
9#include <boost/random/variate_generator.hpp>
10
11namespace stan {
12namespace math {
13
31template <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 using T_N_ref = ref_type_t<T_N>;
37 using T_theta_ref = ref_type_t<T_theta>;
38 static constexpr const char* function = "binomial_rng";
39 check_consistent_sizes(function, "Population size parameter", N,
40 "Probability Parameter", theta);
41 T_N_ref N_ref = N;
42 T_theta_ref theta_ref = theta;
43 check_nonnegative(function, "Population size parameter", N_ref);
44 check_bounded(function, "Probability parameter", value_of(theta_ref), 0.0,
45 1.0);
46
47 scalar_seq_view<T_N_ref> N_vec(N_ref);
48 scalar_seq_view<T_theta_ref> theta_vec(theta_ref);
49 size_t M = max_size(N, theta);
51
52 for (size_t m = 0; m < M; ++m) {
53 variate_generator<RNG&, binomial_distribution<> > binomial_rng(
54 rng, binomial_distribution<>(N_vec[m], theta_vec[m]));
55
56 output[m] = binomial_rng();
57 }
58
59 return output.data();
60}
61
62} // namespace math
63} // namespace stan
64#endif
typename helper::type type
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
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...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...