Sums of Discrete Variables
Many quantities are sums of independent random pieces: the total of several dice, the number of errors across many components, the total waiting time over many steps. The distribution of a sum is found by convolution.
Convolution
If and are independent with integer values, then has
Split on the value of : to get a total of , must be some and exactly . The cases are disjoint, and independence lets us multiply. This operation on two distributions is the convolution .
from fractions import Fraction
import numpy as np
die = np.full(6, 1 / 6) # P(1..6), index 0 means value 1
two = np.convolve(die, die) # values 2..12
three = np.convolve(two, die) # values 3..18
print("two dice, P(sum = 2..12) * 36:", np.round(two * 36).astype(int).tolist())
print("three dice, most likely totals:", [int(i + 3) for i in np.flatnonzero(np.isclose(three, three.max()))])
print("P(three dice sum to 10) =", Fraction(round(three[10 - 3] * 216), 216))two dice, P(sum = 2..12) * 36: [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
three dice, most likely totals: [10, 11]
P(three dice sum to 10) = 1/8
numpy.convolve performs exactly the sum above. Convolving a distribution with itself times gives the distribution of the sum of copies; the shape becomes more and more bell-like, a first glimpse of the central limit theorem.
Sums of familiar distributions
Convolution of standard families often stays in the family:
| Binomial(, ) | Binomial(, ) | Binomial(, ) |
| Poisson() | Poisson() | Poisson() |
| Geometric() | Geometric() | Negative binomial(, ) |
The binomial row is obvious from the story (successes in trials plus successes in more trials). The Poisson row can be checked by convolution and the binomial theorem, or in one line with generating functions.
from math import exp, factorial
import numpy as np
def poisson(lam, kmax=40):
return np.array([exp(-lam) * lam ** k / factorial(k) for k in range(kmax)])
conv = np.convolve(poisson(1.5), poisson(2.5))[:12]
direct = poisson(4.0)[:12]
print("max difference:", f"{np.abs(conv - direct).max():.1e}")
print("P(sum = 0..5):", np.round(conv[:6], 4).tolist())max difference: 5.6e-17
P(sum = 0..5): [0.0183, 0.0733, 0.1465, 0.1954, 0.1954, 0.1563]
Repeated convolution and the shape of sums
The sum of dice has mean and standard deviation . Plotting the distribution as a bar chart, the shape approaches a normal curve quickly:
import numpy as np
die = np.full(6, 1 / 6)
dist = die.copy()
for n in range(2, 11):
dist = np.convolve(dist, die)
values = np.arange(10, 61)
mean, sd = 35, np.sqrt(350 / 12)
for v in range(20, 51, 3):
p = dist[v - 10]
normal = np.exp(-((v - mean) / sd) ** 2 / 2) / (sd * np.sqrt(2 * np.pi))
print(f"sum={v:2d} exact {p:.4f} normal curve {normal:.4f} " + "#" * int(p * 400))sum=20 exact 0.0014 normal curve 0.0016
sum=23 exact 0.0063 normal curve 0.0063 ##
sum=26 exact 0.0190 normal curve 0.0184 #######
sum=29 exact 0.0405 normal curve 0.0399 ################
sum=32 exact 0.0629 normal curve 0.0633 #########################
sum=35 exact 0.0727 normal curve 0.0739 #############################
sum=38 exact 0.0629 normal curve 0.0633 #########################
sum=41 exact 0.0405 normal curve 0.0399 ################
sum=44 exact 0.0190 normal curve 0.0184 #######
sum=47 exact 0.0063 normal curve 0.0063 ##
sum=50 exact 0.0014 normal curve 0.0016
For 10 dice the exact probabilities and the normal density agree to about three decimals.
In software
Convolutions of distributions compute the total latency of a request that passes through several services, the total size of a batch of files, and the distribution of scores in games. With many terms or large ranges, the fast Fourier transform computes convolutions quickly (scipy.signal.fftconvolve), which is how exact probabilities of sums of hundreds of dice can be found.
Exercises
Exercise 1: Two dice
Use the convolution formula to find for two dice.
Answer
.
Solution
over : three terms of .
Exercise 2: A die plus a coin
is a die roll and is 0 or 1 by a fair coin. Find the distribution of .
Answer
for the values 1 and 7, and for 2 to 6.
Solution
. For both terms are .
Exercise 3: Binomial closure (code)
Convolve Binomial(5, 0.3) with Binomial(7, 0.3) and compare with Binomial(12, 0.3).
Answer
They agree to rounding error.
Solution
from math import comb
import numpy as np
b = lambda n, p: np.array([comb(n, k) * p ** k * (1 - p) ** (n - k) for k in range(n + 1)])
print(f"{np.abs(np.convolve(b(5, 0.3), b(7, 0.3)) - b(12, 0.3)).max():.1e}")2.8e-17
Test yourself
The distribution of the sum of two independent discrete variables is the … of their distributions.
- product
- convolution
- maximum
- average
The sum of independent Poisson(2) and Poisson(3) variables is…
- Poisson(5)
- Poisson(6)
- Binomial(5, 1/2)
- not Poisson