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
9#include <boost/random/binomial_distribution.hpp>
10#include <boost/random/variate_generator.hpp>
11
12namespace stan {
13namespace math {
14
32template <typename T_N, typename T_theta, class RNG>
34 const T_N& N, const T_theta& theta, RNG& rng) {
35 using boost::binomial_distribution;
36 using boost::variate_generator;
37 using T_N_ref = ref_type_t<T_N>;
38 using T_theta_ref = ref_type_t<T_theta>;
39 static constexpr const char* function = "binomial_rng";
40 check_consistent_sizes(function, "Population size parameter", N,
41 "Probability Parameter", theta);
42 if (size_zero(N, theta)) {
43 return {};
44 }
45
46 T_N_ref N_ref = N;
47 T_theta_ref theta_ref = theta;
48 check_nonnegative(function, "Population size parameter", N_ref);
49 check_bounded(function, "Probability parameter", value_of(theta_ref), 0.0,
50 1.0);
51
52 scalar_seq_view<T_N_ref> N_vec(N_ref);
53 scalar_seq_view<T_theta_ref> theta_vec(theta_ref);
54 size_t M = max_size(N, theta);
56
57 for (size_t m = 0; m < M; ++m) {
58 variate_generator<RNG&, binomial_distribution<> > binomial_rng(
59 rng, binomial_distribution<>(N_vec[m], theta_vec[m]));
60
61 output[m] = binomial_rng();
62 }
63
64 return output.data();
65}
66
67} // namespace math
68} // namespace stan
69#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.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
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:56
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...