TExcelTAD
Home › Probability Theory › Sums of Discrete Variables

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 XX and YY are independent with integer values, then Z=X+YZ = X + Y has

Pr⁡[Z=z]=∑kPr⁡[X=k] Pr⁡[Y=z−k]\Pr[Z = z] = \sum_{k} \Pr[X = k] \, \Pr[Y = z - k]

Split on the value of XX: to get a total of zz, XX must be some kk and YY exactly z−kz - k. The cases are disjoint, and independence lets us multiply. This operation on two distributions is the convolution mX∗mYm_X * m_Y.

python · try it yourself
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 nn times gives the distribution of the sum of nn 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:

XX YY X+YX + Y
Binomial(mm, pp) Binomial(nn, pp) Binomial(m+nm + n, pp)
Poisson(λ\lambda) Poisson(μ\mu) Poisson(λ+μ\lambda + \mu)
Geometric(pp) Geometric(pp) Negative binomial(22, pp)

The binomial row is obvious from the story (successes in mm trials plus successes in nn more trials). The Poisson row can be checked by convolution and the binomial theorem, or in one line with generating functions.

python · try it yourself
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 nn dice has mean 3.5n3.5n and standard deviation 35n/12\sqrt{35n/12}. Plotting the distribution as a bar chart, the shape approaches a normal curve quickly:

python · try it yourself
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 Pr⁡[X+Y=4]\Pr[X + Y = 4] for two dice.

Answer

3/36=1/123/36 = 1/12.

Solution

∑kPr⁡[X=k]Pr⁡[Y=4−k]\sum_k \Pr[X = k]\Pr[Y = 4 - k] over k=1,2,3k = 1, 2, 3: three terms of 1/361/36.

Exercise 2: A die plus a coin

XX is a die roll and YY is 0 or 1 by a fair coin. Find the distribution of X+YX + Y.

Answer

Pr⁡=1/12\Pr = 1/12 for the values 1 and 7, and 1/61/6 for 2 to 6.

Solution

Pr⁡[Z=z]=12Pr⁡[X=z]+12Pr⁡[X=z−1]\Pr[Z = z] = \frac{1}{2}\Pr[X = z] + \frac{1}{2}\Pr[X = z - 1]. For 2≤z≤62 \le z \le 6 both terms are 112\frac{1}{12}.

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
python · try it yourself
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.

  1. product
  2. convolution
  3. maximum
  4. average

The sum of independent Poisson(2) and Poisson(3) variables is…

  1. Poisson(5)
  2. Poisson(6)
  3. Binomial(5, 1/2)
  4. not Poisson