R Functions Related to Hypergeometric Distributions

To find the hypergeometric probability of seeing exactly $x$ white balls when drawing $k$ balls from an urn containing $m$ white balls and $n$ black balls, or equivalently $$P(x) = \frac{{}_m C_x \cdot {}_n C_{k-x}}{{}_{m+n} C_k}$$ one can use the following R function:

dhyper

Usage
dhyper(x, m, n, k)

Example

Usually 50 potential jurors are held to compose a jury of 12. Suppose that this group of 50 has 15 females and 35 males. To find the probability that the jury will be made up of 4 females and 8 males, one could use the following:

> dhyper(4, 15, 35, 12)
[1] 0.2646333

If one needs to find the probability that in drawing $k$ balls from an urn containing $m$ white balls and $n$ black balls, one sees $x$ or fewer white balls, one can use the related cumulative probability distribution function:

phyper

Usage
phyper(x, m, n, k)

Examples

In a New York State Lotto game, a bettor selects $6$ numbers from $1$ to $59$ (without repetition), and a winning $6$-number combination is later randomly selected. To find the probability that you purchase $1$ ticket with a $6$-number combination and you get more than $2$ of the winning numbers, one could use the following:

> 1 - phyper(2, 6, 53, 6)
[1] 0.0108641

R also has a function that lets you simulate the outcome of a random variable $X$ that follows a hypergeometric distribution:

rhyper

Usage
rhyper(nn, m, n, k)

Note: Here $nn$ represents the number of realizations of your random variable you wish to produce.

Examples

20% of a batch of 30 integrated circuit chips are defective. Simulate the number of defective chips found in 10 random samples of size 8.

> rhyper(10, 6, 24, 8)
 [1] 2 1 1 0 1 2 1 2 0 4