Stan Math Library  2.15.0
reverse mode automatic differentiation
hypergeometric_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_RNG_HPP
3 
4 #include <boost/math/distributions/hypergeometric.hpp>
5 
9 
10 namespace stan {
11  namespace math {
12 
13  template <class RNG>
14  inline int
15  hypergeometric_rng(int N, int a, int b, RNG& rng) {
16  using boost::variate_generator;
17  using boost::math::hypergeometric_distribution;
18 
19  static const char* function("hypergeometric_rng");
20 
21  check_bounded(function, "Draws parameter", N, 0, a+b);
22  check_positive(function, "Draws parameter", N);
23  check_positive(function, "Successes in population parameter", a);
24  check_positive(function, "Failures in population parameter", b);
25 
26  hypergeometric_distribution<> dist(b, N, a + b);
27 
28  double u = uniform_rng(0.0, 1.0, rng);
29  int min = 0;
30  int max = a - 1;
31  while (min < max) {
32  int mid = (min + max) / 2;
33  if (cdf(dist, mid + 1) > u)
34  max = mid;
35  else
36  min = mid + 1;
37  }
38  return min + 1;
39  }
40 
41  }
42 }
43 #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.
int min(const std::vector< int > &x)
Returns the minimum coefficient in the specified column vector.
Definition: min.hpp:20
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:22
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
double uniform_rng(double alpha, double beta, RNG &rng)
Definition: uniform_rng.hpp:20
int hypergeometric_rng(int N, int a, int b, RNG &rng)

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