bundles / numpy 2.4.4 / numpy / random / _generator / Generator / binomial
cython_function_or_method
numpy.random._generator:Generator.binomial
Signature
def binomial ( n , p , size = None ) Summary
Draw samples from a binomial distribution.
Extended Summary
Samples are drawn from a binomial distribution with specified parameters, n trials and p probability of success where n an integer >= 0 and p is in the interval [0,1]. (n may be input as a float, but it is truncated to an integer in use)
Parameters
n: int or array_like of intsParameter of the distribution, >= 0. Floats are also accepted, but they will be truncated to integers.
p: float or array_like of floatsParameter of the distribution, >= 0 and <=1.
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 ifnandpare both scalars. Otherwise,np.broadcast(n, p).sizesamples are drawn.
Returns
out: ndarray or scalarDrawn samples from the parameterized binomial distribution, where each sample is equal to the number of successes over the n trials.
Notes
The probability mass function (PMF) for the binomial distribution is
where is the number of trials, is the probability of success, and is the number of successes.
When estimating the standard error of a proportion in a population by using a random sample, the normal distribution works well unless the product p*n <=5, where p = population proportion estimate, and n = number of samples, in which case the binomial distribution is used instead. For example, a sample of 15 people shows 4 who are left handed, and 11 who are right handed. Then p = 4/15 = 27%. 0.27*15 = 4, so the binomial distribution should be used in this case.
Examples
Draw samples from the distribution:rng = np.random.default_rng() n, p, size = 10, .5, 10000 s = rng.binomial(n, p, 10000)✓
n, p, size = 9, 0.1, 20000
✓np.sum(rng.binomial(n=n, p=p, size=size) == 0)/size
✗import matplotlib.pyplot as plt from scipy.stats import binom n, p, size = 100, 0.4, 10000 sample = rng.binomial(n, p, size=size) count, bins, _ = plt.hist(sample, 30, density=True) x = np.arange(n) y = binom.pmf(x, n, p)✓
plt.plot(x, y, linewidth=2, color='r')
✗See also
- scipy.stats.binom
probability density function, distribution or cumulative density function, etc.
Aliases
-
numpy.random.Generator.binomial