Automatic Differentiation
 
Loading...
Searching...
No Matches
hypergeometric_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_HYPERGEOMETRIC_RNG_HPP
2#define STAN_MATH_PRIM_PROB_HYPERGEOMETRIC_RNG_HPP
3
5#include <boost/math/distributions/hypergeometric.hpp>
6
9
10namespace stan {
11namespace math {
12
13template <class RNG>
14inline int hypergeometric_rng(int N, int a, int b, RNG& rng) {
15 using boost::variate_generator;
16 using boost::math::hypergeometric_distribution;
17 static constexpr const char* function = "hypergeometric_rng";
18 check_bounded(function, "Draws parameter", value_of(N), 0, a + b);
19 check_positive(function, "Draws parameter", N);
20 check_positive(function, "Successes in population parameter", a);
21 check_positive(function, "Failures in population parameter", b);
22
23 hypergeometric_distribution<> dist(b, N, a + b);
24
25 double u = uniform_rng(0.0, 1.0, rng);
26 int min = 0;
27 int max = a - 1;
28 while (min < max) {
29 int mid = (min + max) / 2;
30 if (cdf(dist, mid + 1) > u) {
31 max = mid;
32 } else {
33 min = mid + 1;
34 }
35 }
36 return min + 1;
37}
38
39} // namespace math
40} // namespace stan
41#endif
VectorBuilder< true, double, T_alpha, T_beta >::type uniform_rng(const T_alpha &alpha, const T_beta &beta, RNG &rng)
Return a uniform random variate for the given upper and lower bounds using the specified random numbe...
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.
int hypergeometric_rng(int N, int a, int b, RNG &rng)
auto min(T1 x, T2 y)
Returns the minimum coefficient of the two specified scalar arguments.
Definition min.hpp:24
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
auto max(T1 x, T2 y)
Returns the maximum value of the two specified scalar arguments.
Definition max.hpp:25
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9