bundles / numpy 2.4.3 / numpy / random / RandomState / hypergeometric
cython_function_or_method
numpy.random:RandomState.hypergeometric
Signature
def hypergeometric ( ngood , nbad , nsample , size = None ) Summary
Draw samples from a Hypergeometric distribution.
Extended Summary
Samples are drawn from a hypergeometric distribution with specified parameters, ngood (ways to make a good selection), nbad (ways to make a bad selection), and nsample (number of items sampled, which is less than or equal to the sum ngood + nbad).
Parameters
ngood: int or array_like of intsNumber of ways to make a good selection. Must be nonnegative.
nbad: int or array_like of intsNumber of ways to make a bad selection. Must be nonnegative.
nsample: int or array_like of intsNumber of items sampled. Must be at least 1 and at most
ngood + nbad.size: int or tuple of ints, optionalOutput shape. If the given shape is, e.g.,
(m, n, k), thenm * n * ksamples are drawn. If size isNone(default), a single value is returned ifngood,nbad, andnsampleare all scalars. Otherwise,np.broadcast(ngood, nbad, nsample).sizesamples are drawn.
Returns
out: ndarray or scalarDrawn samples from the parameterized hypergeometric distribution. Each sample is the number of good items within a randomly selected subset of size
nsampletaken from a set ofngoodgood items andnbadbad items.
Notes
The probability mass function (PMF) for the Hypergeometric distribution is
where and
for P(x) the probability of x good results in the drawn sample, g = ngood, b = nbad, and n = nsample.
Consider an urn with black and white marbles in it, ngood of them are black and nbad are white. If you draw nsample balls without replacement, then the hypergeometric distribution describes the distribution of black balls in the drawn sample.
Note that this distribution is very similar to the binomial distribution, except that in this case, samples are drawn without replacement, whereas in the Binomial case samples are drawn with replacement (or the sample space is infinite). As the sample space becomes large, this distribution approaches the binomial.
Examples
Draw samples from the distribution:ngood, nbad, nsamp = 100, 2, 10
✗s = np.random.hypergeometric(ngood, nbad, nsamp, 1000) from matplotlib.pyplot import hist✓
hist(s)
✗s = np.random.hypergeometric(15, 15, 15, 100000)
✓sum(s>=12)/100000. + sum(s<=3)/100000.
✗See also
- random.Generator.hypergeometric
which should be used for new code.
- scipy.stats.hypergeom
probability density function, distribution or cumulative density function, etc.
Aliases
-
numpy.random.hypergeometric -
numpy.random.RandomState.hypergeometric