urhyper function

UNU.RAN Hypergeometric random variate generator

UNU.RAN Hypergeometric random variate generator

UNU.RAN random variate generator for the Hypergeometric distribution. It also allows sampling from the truncated distribution.

[Special Generator] -- Sampling Function: Hypergeometric.

urhyper(nn, m, n, k, lb=max(0,k-n), ub=min(k,m))

Arguments

  • nn: number of observations.
  • m: the number of white balls in the urn.
  • n: the number of black balls in the urn.
  • k: the number of balls drawn from the urn.
  • lb: lower bound of (truncated) distribution.
  • ub: upper bound of (truncated) distribution.

Details

The Hypergeometric distribution is used for sampling without

replacement. The density of this distribution with parameters m, n and k (named NpNp, NNpN-Np, and nn, respectively in the reference below) is given by

p(x)=(mx)(nkx)/(m+nk)p(x)=choose(m,x)choose(n,kx)/choose(m+n,k) p(x) = \left. {m \choose x}{n \choose k-x} \right/ {m+n \choose k}p(x) = choose(m, x) choose(n, k-x) / choose(m+n, k)

for x=0,,kx = 0, \ldots, k.

The generation algorithm uses guide table based inversion. The parameters lb and ub can be used to generate variates from the Hypergeometric distribution truncated to the interval (lb,ub).

See Also

runif and .Random.seed about random number generation, unuran for the UNU.RAN class, and rhyper for the built-in generator.

References

W. H"ormann, J. Leydold, and G. Derflinger (2004): Automatic Nonuniform Random Variate Generation. Springer-Verlag, Berlin Heidelberg

Author(s)

Josef Leydold and Wolfgang H"ormann unuran@statmath.wu.ac.at .

Note

This function is a wrapper for the UNU.RAN class in . Compared to rhyper, urhyper is faster, especially for larger sample sizes. However, in opposition to rhyper vector arguments are ignored, i.e. only the first entry is used.

Examples

## Create a sample of size 1000 x <- urhyper(nn=20,m=15,n=5,k=7)
  • Maintainer: Josef Leydold
  • License: GPL (>= 2)
  • Last published: 2025-04-07