hypergeometric_rng.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_RNG_HPP
#define STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_RNG_HPP
#include <boost/math/distributions/hypergeometric.hpp>
#include <stan/math/prim/scal/err/check_bounded.hpp>
#include <stan/math/prim/scal/err/check_positive.hpp>
#include <stan/math/prim/scal/prob/uniform_rng.hpp>
namespace stan {
namespace math {
template <class RNG>
inline int
hypergeometric_rng(int N, int a, int b, RNG& rng) {
using boost::variate_generator;
using boost::math::hypergeometric_distribution;
static const char* function("hypergeometric_rng");
check_bounded(function, "Draws parameter", N, 0, a+b);
check_positive(function, "Draws parameter", N);
check_positive(function, "Successes in population parameter", a);
check_positive(function, "Failures in population parameter", b);
hypergeometric_distribution<> dist(b, N, a + b);
double u = uniform_rng(0.0, 1.0, rng);
int min = 0;
int max = a - 1;
while (min < max) {
int mid = (min + max) / 2;
if (cdf(dist, mid + 1) > u)
max = mid;
else
min = mid + 1;
}
return min + 1;
}
}
}
#endif
stan::math::check_bounded
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.
Definition: check_bounded.hpp:90
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
Definition: log_sum_exp.hpp:8
uniform_rng.hpp
stan::math::max
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:22
check_bounded.hpp
stan::math::check_positive
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
Definition: check_positive.hpp:63
stan::math::uniform_rng
double uniform_rng(double alpha, double beta, RNG &rng)
Definition: uniform_rng.hpp:20
stan::math::hypergeometric_rng
int hypergeometric_rng(int N, int a, int b, RNG &rng)
Definition: hypergeometric_rng.hpp:15

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