bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / random / _generator / Generator / multinomial
cython_function_or_method
numpy.random._generator:Generator.multinomial
Signature
def multinomial(n, pvals, size = None)Summary
Draw samples from a multinomial distribution.
Extended Summary
The multinomial distribution is a multivariate generalization of the binomial distribution. Take an experiment with one of p possible outcomes. An example of such an experiment is throwing a dice, where the outcome can be 1 through 6. Each sample drawn from the distribution represents n such experiments. Its values, X_i = [X_0, X_1, ..., X_p], represent the number of times the outcome was i.
Parameters
n: int or array-like of intsNumber of experiments.
pvals: array-like of floatsProbabilities of each of the
pdifferent outcomes with shape(k0, k1, ..., kn, p). Each elementpvals[i,j,...,:]must sum to 1 (however, the last element is always assumed to account for the remaining probability, as long assum(pvals[..., :-1], axis=-1) <= 1.0. Must have at least 1 dimension where pvals.shape[-1] > 0.size: int or tuple of ints, optionalOutput shape. If the given shape is, e.g.,
(m, n, k), thenm * n * ksamples are drawn each withpelements. Default is None where the output size is determined by the broadcast shape ofnand all by the final dimension ofpvals, which is denoted asb=(b0, b1, ..., bq). If size is not None, then it must be compatible with the broadcast shapeb. Specifically, size must haveqor more elements and size[-(q-j):] must equalbj.
Returns
out: ndarrayThe drawn samples, of shape size, if provided. When size is provided, the output shape is size + (p,) If not specified, the shape is determined by the broadcast shape of
nandpvals,(b0, b1, ..., bq)augmented with the dimension of the multinomial,p, so that that output shape is(b0, b1, ..., bq, p).Each entry
out[i,j,...,:]is ap-dimensional value drawn from the distribution.
Examples
Throw a dice 20 times:rng = np.random.default_rng()
✓rng.multinomial(20, [1/6.]*6, size=1)
✗array([[3, 2, 2, 5, 4, 4]])It landed 4 times on 1, once on 2, etc. Now, throw the dice 20 times, and 20 times again:
rng.multinomial(20, [1/6.]*6, size=2)
✗array([[2, 6, 4, 2, 2, 4],
[2, 4, 6, 3, 3, 2]])
For the first run, we threw 3 times 1, 4 times 2, etc. For the second,
we threw 2 times 1, 4 times 2, etc.
Now, do one experiment throwing the dice 10 time, and 10 times again,
and another throwing the dice 20 times, and 20 times again:
rng.multinomial([[10], [20]], [1/6.]*6, size=(2, 2))
✗array([[[2, 2, 1, 1, 1, 3],
[2, 2, 1, 0, 3, 2]],
[[4, 3, 2, 4, 4, 3],
[4, 3, 8, 2, 1, 2]]])
The first array shows the outcomes of throwing the dice 10 times, and
the second shows the outcomes from throwing the dice 20 times.
A loaded die is more likely to land on number 6:
rng.multinomial(100, [1/7.]*5 + [2/7.])
✗array([12, 14, 18, 14, 15, 27])Simulate 10 throws of a 4-sided die and 20 throws of a 6-sided die
rng.multinomial([10, 20],[[1/4]*4 + [0]*2, [1/6]*6])
✗array([[2, 2, 3, 3, 0, 0],
[1, 6, 3, 3, 3, 4]])
Generate categorical random variates from two categories where the
first has 3 outcomes and the second has 2.
rng.multinomial(1, [[.1, .5, .4 ], [.3, .7, .0]])
✗array([[0, 0, 1],
[0, 1, 0]])
``argmax(axis=-1)`` is then used to return the categories.
pvals = [[.1, .5, .4 ], [.3, .7, .0]] rvs = rng.multinomial(1, pvals, size=(4,2))✓
rvs.argmax(axis=-1)
✗array([[2, 0],
[0, 1],
[1, 1],
[2, 0]])
The same output dimension can be produced using broadcasting.
rvs = rng.multinomial([[1]] * 4, pvals)
✓rvs.argmax(axis=-1)
✗array([[1, 0],
[1, 1],
[0, 0],
[1, 1]])
The probability inputs should be normalized. As an implementation
detail, the value of the last entry is ignored and assumed to take
up any leftover probability mass, but this should not be relied on.
A biased coin which has twice as much weight on one side as on the
other should be sampled like so:
rng.multinomial(100, [1.0 / 3, 2.0 / 3]) # RIGHT
✗array([38, 62])not like:
rng.multinomial(100, [1.0, 2.0]) # WRONG
✓Traceback (most recent call last):
File "<doctest numpy.random._generator:Generator.multinomial[0]>", line 1, in <module>
rng.multinomial(100, [1.0, 2.0]) # WRONG
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "numpy/random/_generator.pyx", line 4108, in numpy.random._generator.Generator.multinomial
File "numpy/random/_common.pyx", line 393, in numpy.random._common.check_array_constraint
File "numpy/random/_common.pyx", line 379, in numpy.random._common._check_array_cons_bounded_0_1
ValueError: pvals < 0, pvals > 1 or pvals contains NaNs
Aliases
numpy.random.Generator.multinomial