TExcelTAD
Home › NumPy › Multinomial Distribution

Multinomial Distribution

The multinomial distribution generalises the binomial. Instead of two outcomes (success/failure), each trial has several possible outcomes, each with its own probability. The result tells you how many times each outcome occurred.

Examples: rolling a die 30 times and counting each face; the votes for four candidates from 1,000 voters; the colours of 50 sweets taken from a bag.

Parameter Meaning
n number of trials
pvals list of probabilities, one per outcome (must sum to 1)
size how many experiments to run

Generating multinomial data

The result of one experiment is an array of counts with one entry per outcome. The counts always add up to n:

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
die = [1/6] * 6
counts = rng.multinomial(n=30, pvals=die)
print(counts)                       # how often faces 1..6 came up
print("total:", counts.sum())
[6 4 7 5 2 6]
total: 30

With size, each row is a separate experiment:

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
# A bag of sweets: 50 % red, 30 % green, 20 % yellow. Four kids each take 10.
kids = rng.multinomial(10, [0.5, 0.3, 0.2], size=4)
print(kids)
print("row sums:", kids.sum(axis=1))
print("average per colour:", kids.mean(axis=0))
[[6 3 1]
 [7 1 2]
 [3 2 5]
 [6 2 2]]
row sums: [10 10 10 10]
average per colour: [5.5 2.  2.5]

Expected counts

The expected count for outcome i is n · p_i. Averaging many experiments confirms it:

python · try it yourself
import numpy as np

rng = np.random.default_rng(7)
candidates = np.array(["Avery", "Blake", "Casey", "Drew"])
support = np.array([0.40, 0.25, 0.20, 0.15])
voters = 1000

polls = rng.multinomial(voters, support, size=5000)    # 5000 simulated elections
avg = polls.mean(axis=0)
for name, a, e in zip(candidates, avg, voters * support):
    print(f"{name:6} simulated {a:7.2f}   expected {e:6.1f}")

wins = np.bincount(polls.argmax(axis=1), minlength=4) / len(polls)
print("chance of winning:", dict(zip(candidates.tolist(), wins.round(3).tolist())))
Avery  simulated  399.89   expected  400.0
Blake  simulated  249.93   expected  250.0
Casey  simulated  200.24   expected  200.0
Drew   simulated  149.93   expected  150.0
chance of winning: {'Avery': 1.0, 'Blake': 0.0, 'Casey': 0.0, 'Drew': 0.0}

The argmax along each row finds the winner of each simulated election, and np.bincount counts how often each candidate won. With a 40 % share Avery wins every simulation: with 1,000 voters, randomness alone almost never closes a 15-point gap.

Each outcome on its own is binomial

If you look at a single outcome (say "red sweets"), its count follows a binomial distribution with the same n and that outcome's probability. A quick check:

python · try it yourself
import numpy as np

rng = np.random.default_rng(1)
multi = rng.multinomial(20, [0.5, 0.3, 0.2], size=100_000)[:, 1]  # just "green"
binom = rng.binomial(20, 0.3, size=100_000)
print(f"multinomial green: mean {multi.mean():.3f}, var {multi.var():.3f}")
print(f"binomial(20, 0.3): mean {binom.mean():.3f}, var {binom.var():.3f}")
multinomial green: mean 6.001, var 4.179
binomial(20, 0.3): mean 5.989, var 4.207

Both have mean 20 × 0.3 = 6 and variance 20 × 0.3 × 0.7 = 4.2.

Visualising

A stacked bar chart shows the composition of each experiment:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
labels = ["red", "green", "yellow"]
colors = ["#e07a5f", "#81b29a", "#f2cc8f"]
draws = rng.multinomial(10, [0.5, 0.3, 0.2], size=8)

plt.figure(figsize=(6, 3.5))
bottom = np.zeros(len(draws))
for i in range(3):
    plt.bar(np.arange(1, 9), draws[:, i], bottom=bottom, color=colors[i], label=labels[i])
    bottom += draws[:, i]
plt.xlabel("kid")
plt.ylabel("sweets")
plt.legend()
plt.tight_layout()
plt.show()
print("Plotted 8 handfuls of 10 sweets")
Plotted 8 handfuls of 10 sweets

Every bar has the same total height (10), but the colour mix varies from kid to kid.

Test yourself

What does rng.multinomial(12, [0.25, 0.25, 0.5]) return?

  1. One integer
  2. An array of 3 counts that add up to 12
  3. Twelve random outcomes
  4. Three probabilities

Which distribution is a multinomial with exactly two outcomes?

  1. Poisson
  2. Binomial
  3. Uniform
  4. Normal

With n=200 and an outcome probability of 0.1, what is that outcome's expected count?

  1. 0.1
  2. 2
  3. 20
  4. 200