Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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 
12  namespace math {
13 
14  template <class RNG>
15  inline int
16  hypergeometric_rng(int N, int a, int b, RNG& rng) {
17  using boost::variate_generator;
18  using boost::math::hypergeometric_distribution;
21 
22  static const char* function("stan::math::hypergeometric_rng");
23 
24  check_bounded(function, "Draws parameter", N, 0, a+b);
25  check_positive(function, "Draws parameter", N);
26  check_positive(function, "Successes in population parameter", a);
27  check_positive(function, "Failures in population parameter", b);
28 
29  hypergeometric_distribution<> dist(b, N, a + b);
30 
31  double u = uniform_rng(0.0, 1.0, rng);
32  int min = 0;
33  int max = a - 1;
34  while (min < max) {
35  int mid = (min + max) / 2;
36  if (cdf(dist, mid + 1) > u)
37  max = mid;
38  else
39  min = mid + 1;
40  }
41  return min + 1;
42  }
43 
44  }
45 
46 }
47 
48 #endif
int min(const std::vector< int > &x)
Returns the minimum coefficient in the specified column vector.
Definition: min.hpp:20
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true if the value is between the low and high values, inclusively.
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
double uniform_rng(const double alpha, const double beta, RNG &rng)
Definition: uniform_rng.hpp:22
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:21
double dist(const std::vector< double > &x, const std::vector< double > &y)
Definition: dist.hpp:11
int hypergeometric_rng(int N, int a, int b, RNG &rng)

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