MULTINOMIAL
Overview
The MULTINOMIAL
function computes probabilities, log-probabilities, entropy, and covariance for outcomes in a multinomial experiment, or draws random samples from the multinomial distribution. The multinomial distribution generalizes the binomial distribution to more than two categories, describing the probability of counts for each category after a fixed number of independent trials, where each trial results in exactly one of several possible outcomes. The probability mass function (PMF) is given by:
where is the total number of trials, is the count for category , and is the probability for category (). Entropy and covariance are computed using standard formulas for the multinomial distribution. For more details, see the scipy.stats.multinomial documentation .
This wrapper function exposes only the most commonly used parameters (x
, n
, p
, method
, and optionally size
for random sampling), and does not support random seed or state control. This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=MULTINOMIAL(x, n, p, [method], [size])
x
(2D list of int, required for ‘pmf’ and ‘logpmf’): Table of counts for each category. Each row is a set of counts that sum ton
.n
(int, required): Number of trials.p
(2D list of float, required): Table of probabilities for each category. Each row must sum to 1.method
(str, optional, default=‘pmf’): Which calculation to perform:pmf
,logpmf
,entropy
,cov
, orrvs
.size
(int, optional): Number of samples to draw if method isrvs
.
The function returns a single value (float), a 2D array (for covariance), or an error message (string) if the input is invalid. For pmf
and logpmf
, the result is the probability or log-probability for the given counts. For entropy
, the result is the entropy of the distribution. For cov
, the result is the covariance matrix. For rvs
, the result is a table of random samples.
Examples
Example 1: Probability Mass Function (pmf)
Inputs:
x | n | p | method | ||||
---|---|---|---|---|---|---|---|
2 | 1 | 2 | 5 | 0.2 | 0.3 | 0.5 | pmf |
Excel formula:
=MULTINOMIAL({2,1,2}, 5, {0.2,0.3,0.5}, "pmf")
Expected output:
Result |
---|
0.180 |
Example 2: Log Probability Mass Function (logpmf)
Inputs:
x | n | p | method | ||||
---|---|---|---|---|---|---|---|
2 | 1 | 2 | 5 | 0.2 | 0.3 | 0.5 | logpmf |
Excel formula:
=MULTINOMIAL({2,1,2}, 5, {0.2,0.3,0.5}, "logpmf")
Expected output:
Result |
---|
-1.715 |
Example 3: Entropy
Inputs:
n | p | method | ||
---|---|---|---|---|
5 | 0.2 | 0.3 | 0.5 | entropy |
Excel formula:
=MULTINOMIAL(, 5, {0.2,0.3,0.5}, "entropy")
Expected output:
Result |
---|
2.409 |
Example 4: Covariance Matrix
Inputs:
n | p | method | ||
---|---|---|---|---|
5 | 0.2 | 0.3 | 0.5 | cov |
Excel formula:
=MULTINOMIAL(, 5, {0.2,0.3,0.5}, "cov")
Expected output:
Covariance Matrix |
---|
0.800 -0.300 -0.500 |
-0.300 1.050 -0.750 |
-0.500 -0.750 1.250 |
This table shows the covariance between each pair of categories for the specified multinomial distribution.
Python Code
from scipy.stats import multinomial as scipy_multinomial
from typing import List, Optional, Union
def multinomial(
x: Optional[List[List[int]]] = None,
n: Optional[int] = None,
p: Optional[List[List[float]]] = None,
method: str = 'pmf',
size: Optional[int] = None
) -> Union[List[List[Optional[float]]], str]:
"""
Computes the probability mass function, log-PMF, entropy, covariance, or draws random samples from a multinomial distribution.
Args:
x: 2D list of nonnegative integers. Number of successes in each category. Required for 'pmf' and 'logpmf'.
n: Number of trials (int). Required for all methods.
p: 2D list of float values. Probability of each category. Each row should sum to 1. Required for all methods.
method: Which method to compute (str): 'pmf', 'logpmf', 'entropy', 'cov', 'rvs'. Default is 'pmf'.
size: Number of samples to draw if method is 'rvs'. Optional.
Returns:
2D list of results for each input, or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate n
if n is None or not isinstance(n, int) or n < 0:
return "Invalid input: n must be a nonnegative integer."
# Validate p
if p is None or not isinstance(p, list) or len(p) == 0:
return "Invalid input: p must be a 2D list of probabilities."
if not all(isinstance(row, list) and len(row) > 0 for row in p):
return "Invalid input: p must be a 2D list of probabilities."
for row in p:
if not all(isinstance(val, (int, float)) and 0 <= val <= 1 for val in row):
return "Invalid input: probabilities in p must be between 0 and 1."
if abs(sum(row) - 1.0) > 1e-8:
return "Invalid input: each row in p must sum to 1."
# Validate x for pmf/logpmf
if method in ['pmf', 'logpmf']:
if x is None or not isinstance(x, list) or len(x) == 0:
return "Invalid input: x must be a 2D list of nonnegative integers for pmf/logpmf."
if not all(isinstance(row, list) and len(row) == len(p[0]) for row in x):
return "Invalid input: x must be a 2D list with same number of columns as p."
for row in x:
if not all(isinstance(val, int) and val >= 0 for val in row):
return "Invalid input: x must contain nonnegative integers."
if sum(row) != n:
return "Invalid input: sum of each row in x must equal n."
# Validate method
if method not in ['pmf', 'logpmf', 'entropy', 'cov', 'rvs']:
return "Invalid input: method must be one of 'pmf', 'logpmf', 'entropy', 'cov', 'rvs'."
# Validate size for rvs
if method == 'rvs' and size is not None:
if not isinstance(size, int) or size <= 0:
return "Invalid input: size must be a positive integer."
# Compute results
try:
dist = scipy_multinomial(n, p[0])
if method == 'pmf':
result = [[float(dist.pmf(row))] for row in x]
elif method == 'logpmf':
result = [[float(dist.logpmf(row))] for row in x]
elif method == 'entropy':
result = [[float(dist.entropy())]]
elif method == 'cov':
cov = dist.cov()
# Convert numpy array to 2D list of floats
result = cov.tolist()
elif method == 'rvs':
samples = dist.rvs(size=size if size is not None else 1)
# If only one sample, wrap in a list
if hasattr(samples[0], '__iter__'):
result = [list(map(int, row)) for row in samples]
else:
result = [[int(val) for val in samples]]
else:
return "Invalid method."
except Exception as e:
return f"scipy.stats.multinomial error: {e}"
return result