bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / stats / _multivariate / multinomial_gen
class
scipy.stats._multivariate:multinomial_gen
source: build-install/usr/lib/python3.14/site-packages/scipy/stats/_multivariate.py:3886
Signature
class multinomial_gen(seed = None)Members
Summary
A multinomial random variable.
Parameters
%(_doc_default_callparams)s%(_doc_random_state)s
Methods
pmf(x, n, p)Probability mass function.
logpmf(x, n, p)Log of the probability mass function.
rvs(n, p, size=1, random_state=None)Draw random samples from a multinomial distribution.
entropy(n, p)Compute the entropy of the multinomial distribution.
cov(n, p)Compute the covariance matrix of the multinomial distribution.
Notes
%(_doc_callparams_note)s
The probability mass function for multinomial is
supported on where each is a nonnegative integer and their sum is .
Examples
from scipy.stats import multinomial rv = multinomial(8, [0.3, 0.2, 0.5])✓
rv.pmf([1, 3, 4])
✗np.float64(0.04200000000000007)The multinomial distribution for :math:`k=2` is identical to the corresponding binomial distribution (tiny numerical differences notwithstanding):
from scipy.stats import binom
✓multinomial.pmf([3, 4], n=7, p=[0.4, 0.6]) binom.pmf(3, 7, 0.4)✗
np.float64(0.290304)The functions ``pmf``, ``logpmf``, ``entropy``, and ``cov`` support broadcasting, under the convention that the vector parameters (``x`` and ``p``) are interpreted as if each row along the last axis is a single object. For instance:
multinomial.pmf([[3, 4], [3, 5]], n=[7, 8], p=[.3, .7])
✗array([0.22689, 0.25412])Here, ``x.shape == (2, 2)``, ``n.shape == (2,)``, and ``p.shape == (2,)``, but following the rules mentioned above they behave as if the rows ``[3, 4]`` and ``[3, 5]`` in ``x`` and ``[.3, .7]`` in ``p`` were a single object, and as if we had ``x.shape = (2,)``, ``n.shape = (2,)``, and ``p.shape = ()``. To obtain the individual elements without broadcasting, we would do this:
multinomial.pmf([3, 4], n=7, p=[.3, .7]) multinomial.pmf([3, 5], 8, p=[.3, .7])✗
np.float64(0.2541218399999999)This broadcasting also works for ``cov``, where the output objects are square matrices of size ``p.shape[-1]``. For example:
multinomial.cov([4, 5], [[.3, .7], [.4, .6]])
✗array([[[ 0.84, -0.84],
[-0.84, 0.84]],
[[ 1.2 , -1.2 ],
[-1.2 , 1.2 ]]])
In this example, ``n.shape == (2,)`` and ``p.shape == (2, 2)``, and
following the rules above, these broadcast as if ``p.shape == (2,)``.
Thus the result should also be of shape ``(2,)``, but since each output is
a :math:`2 \times 2` matrix, the result in fact has shape ``(2, 2, 2)``,
where ``result[0]`` is equal to ``multinomial.cov(n=4, p=[.3, .7])`` and
``result[1]`` is equal to ``multinomial.cov(n=5, p=[.4, .6])``.
Alternatively, the object may be called (as a function) to fix the `n` and
`p` parameters, returning a "frozen" multinomial random variable:
rv = multinomial(n=7, p=[.3, .7])
✓See also
- numpy.random.Generator.multinomial
Sampling from the multinomial distribution.
- scipy.stats.binom
The binomial distribution.
- scipy.stats.multivariate_hypergeom
The multivariate hypergeometric distribution.
Aliases
scipy.stats._multivariate.multinomial_gen