{ } Raw JSON

bundles / scipy latest / scipy / stats / _discrete_distns / nhypergeom_gen

class

scipy.stats._discrete_distns:nhypergeom_gen

source: /scipy/stats/_discrete_distns.py :738

Signature

class   nhypergeom_gen ( a = 0 b = inf name = None badvalue = None moment_tol = 1e-08 values = None inc = 1 longname = None shapes = None seed = None )

Members

Summary

A negative hypergeometric discrete random variable.

Extended Summary

Consider a box containing balls:, red and blue. We randomly sample balls from the box, one at a time and without replacement, until we have picked blue balls. nhypergeom is the distribution of the number of red balls we have picked.

%(before_notes)s

Notes

The symbols used to denote the shape parameters (M, n, and r) are not universally accepted. See the Examples for a clarification of the definitions used here.

The probability mass function is defined as,

for , , , and the binomial coefficient is:

It is equivalent to observing successes in samples with 'th sample being a failure. The former can be modelled as a hypergeometric distribution. The probability of the latter is simply the number of failures remaining divided by the size of the remaining population . This relationship can be shown as:

where is probability mass function (PMF) of the negative hypergeometric distribution and is the PMF of the hypergeometric distribution.

%(after_notes)s

Examples

import numpy as np
from scipy.stats import nhypergeom
import matplotlib.pyplot as plt
Suppose we have a collection of 20 animals, of which 7 are dogs. Then if we want to know the probability of finding a given number of dogs (successes) in a sample with exactly 12 animals that aren't dogs (failures), we can initialize a frozen distribution and plot the probability mass function:
M, n, r = [20, 7, 12]
rv = nhypergeom(M, n, r)
x = np.arange(0, n+2)
pmf_dogs = rv.pmf(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, pmf_dogs, 'bo')
ax.vlines(x, 0, pmf_dogs, lw=2)
ax.set_xlabel('# of dogs in our group with given 12 failures')
ax.set_ylabel('nhypergeom PMF')
plt.show()
fig-7773d8764d43697e.png
Instead of using a frozen distribution we can also use `nhypergeom` methods directly. To for example obtain the probability mass function, use:
prb = nhypergeom.pmf(x, M, n, r)
And to generate random numbers:
R = nhypergeom.rvs(M, n, r, size=10)
To verify the relationship between `hypergeom` and `nhypergeom`, use:
from scipy.stats import hypergeom, nhypergeom
M, n, r = 45, 13, 8
k = 6
nhypergeom.pmf(k, M, n, r)
hypergeom.pmf(k, M, n, k+r-1) * (M - n - (r-1)) / (M - (k+r-1))

See also

binom
hypergeom
nbinom

Aliases

  • scipy.stats._discrete_distns.nhypergeom_gen