| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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
6 #include <stan/math/prim/scal/err/check_bounded.hpp>
7 #include <stan/math/prim/scal/err/check_positive.hpp>
8 #include <stan/math/prim/scal/prob/uniform_rng.hpp>
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;
19 using stan::math::check_bounded;
20 using stan::math::check_positive;
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
stan::math::min
int min(const std::vector< int > &x)
Returns the minimum coefficient in the specified column vector.
Definition: min.hpp:20
check_positive.hpp
stan::math::check_bounded
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.
Definition: check_bounded.hpp:95
uniform_rng.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
stan::math::uniform_rng
double uniform_rng(const double alpha, const double beta, RNG &rng)
Definition: uniform_rng.hpp:22
stan::math::max
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:21
check_bounded.hpp
stan::math::dist
double dist(const std::vector< double > &x, const std::vector< double > &y)
Definition: dist.hpp:11
stan::math::hypergeometric_rng
int hypergeometric_rng(int N, int a, int b, RNG &rng)
Definition: hypergeometric_rng.hpp:16

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