---
title: Sums of random variables
heading: Convolution, tolerance stack-up and why sums turn Gaussian
eyebrow: A11 · Lesson 25 · Sums of random variables
xp: 40
---

::: hook
Five 1 kΩ resistors, each within ±1%, are put in series. The worst case is off by ±50 Ω, but you'd have to make thousands of strings to see that. What does a typical string look like? The total is a sum of independent random errors, and sums have a simple rule for their mean and spread and a less obvious one for their shape: the density of a sum is the convolution of the densities, the same operation A10 used for filters. Repeated convolution is also why sums of almost anything end up Gaussian.
:::

::: play
- Each draw adds two independent errors, uniform over widths $a$ and $b$. The histogram is the sum; the line is the convolution of the two flat densities.
- With $a$ much smaller than $b$ the sum is nearly flat. Make $a = b$: the convolution of two equal rectangles is a triangle.
- The sum's standard deviation is the root-sum-square of the parts. Check it against the readout.
:::
:::

```sim plot
view: random
x: s = -0.1..3.1
slider: a = 0.1..1 step 0.05 = 0.5
slider: b = 1..2 step 0.05 = 1
sample: a*rand(1) + b*rand(1) | sd: S
trials: 30000
bins: 64
density: (s*step(a - s)*step(s) + a*step(s - a)*step(b - s) + (a + b - s)*step(s - b)*step(a + b - s))/(a*b)
readout: standard deviation, measured = S | digits: 3
readout: square root of (a² + b²)/12 = sqrt((a^2 + b^2)/12) | digits: 3
```

::: predict
## Five resistors in series each have an error uniform within ±10 Ω, independently. Roughly what is the standard deviation of the total error?

- [ ] 50 Ω
- [ ] 29 Ω
- [x] 13 Ω
- [ ] 6 Ω

::: reveal
About 13 Ω. One error, uniform over 20 Ω, has standard deviation $\frac{20}{\sqrt{12}} = 5.77$ Ω. Five independent ones add in variance, giving $\sqrt5 \times 5.77 = 12.9$ Ω. The worst case of 50 Ω is almost four standard deviations out, and a sum like this is nearly Gaussian, so fewer than one string in 10,000 gets anywhere near it.
:::
:::

::: nameit
## Mean and variance of a sum

For any random variables, $E[X + Y] = E[X] + E[Y]$. For independent ones (or merely uncorrelated, lesson 22), variances add too:

$$\text{Var}(X_1 + \cdots + X_n) = \sigma_1^2 + \cdots + \sigma_n^2$$

So $n$ identical independent errors give a total spread of $\sigma\sqrt n$, while the worst case grows as $n$.

## The distribution of a sum

If $X$ and $Y$ are independent with densities $p_X$ and $p_Y$, the density of $S = X + Y$ is their **convolution**:

$$p_S(s) = \int p_X(x)\,p_Y(s - x)\,dx$$

For each total $s$, add up every way of splitting it as $x$ plus $s - x$. Two flat densities convolve to a trapezoid, or a triangle when they're equal. For discrete counts the integral becomes a sum, which is how two dice give a triangle from 2 to 12.

Some families are closed under addition:

| Independent parts | Sum |
|---|---|
| Gaussian, means $\mu_i$, variances $\sigma_i^2$ | Gaussian, mean $\sum\mu_i$, variance $\sum\sigma_i^2$ |
| Poisson, means $\lambda_i$ | Poisson, mean $\sum\lambda_i$ |
| binomial, same $p$, $n_i$ tries | binomial, $\sum n_i$ tries |

## Toward the Gaussian

Every convolution smooths. Convolve enough independent pieces, none dominating, and the result approaches a Gaussian with the summed mean and variance, whatever the pieces looked like: the central limit theorem of lesson 16, seen as repeated convolution.

## Tolerance stack-up

Designers combine part tolerances two ways:

- **worst case:** add the limits, guaranteed but pessimistic;
- **statistical (RSS):** add the variances, and design for a margin of, say, $3\sigma$ of the total.

For many independent parts, the statistical method is far less pessimistic and, thanks to the Gaussian shape of the sum, still very safe.
:::

::: example
## Five 1 kΩ resistors in series each have errors uniform within ±1%. Compare the worst case with a $3\sigma$ statistical limit.

::: solution
1. **One resistor.** Each error is uniform over ±10 Ω, with
   $$\begin{aligned} \sigma &= \frac{20}{\sqrt{12}} \\ &= 5.77\ \Omega \end{aligned}$$
2. **The total.**
   $$\begin{aligned} \sigma &= \sqrt5 \times 5.77 \\ &= 12.9\ \Omega \end{aligned}$$
3. **Compare.** The statistical limit is $3\sigma = 38.7$ Ω, against a worst case of 50 Ω.
4. **With 20 resistors.** The gap widens: $3\sigma = 77$ Ω against 200 Ω.

**Answer:** the $3\sigma$ limit is 38.7 Ω, against a worst case of 50 Ω.
:::
:::

::: example
## $X$ and $Y$ are independent and uniform on $[0, 1]$. What is the chance their sum exceeds 1.5?

::: solution
1. **The sum's density.** It's a triangle on $[0, 2]$ peaking at 1.
2. **Area above 1.5.** It's a small triangle with base 0.5 and height 0.5:
   $$\frac{0.5 \times 0.5}{2} = 0.125$$
3. **Check.** Equivalently, the corner of the unit square above the line $x + y = 1.5$ has area
   $$\frac{(2 - 1.5)^2}{2} = 0.125$$

**Answer:** the sum exceeds 1.5 with chance 0.125.
:::
:::

::: example
## Two independent delays are each uniform on 0 to 1 ms. What is the chance their total is under 0.5 ms?

::: solution
1. **The sum's density.** It's the convolution of two uniform densities: a triangle rising from 0 at 0 ms to 1 at 1 ms and back to 0 at 2 ms.
2. **Area below 0.5 ms.**
   $$\frac12(0.5)(0.5) = 0.125$$
3. **Why it's small.** Short totals are rare because both delays must be short at once.

**Answer:** the total is under 0.5 ms with chance 0.125.
:::
:::

::: check
given: n = 4 | 10; t = 1 | 5
answer: sqrt(n)*20*t/sqrt(12)
unit: Ω
tolerance: 0.5%

## {n} resistors of 1 kΩ, each with an error uniform within ±{t}%, are in series. What is the standard deviation of the total?

::: right
Right. Each has $\sigma = \frac{20t}{\sqrt{12}}$ Ω; the total is $\sqrt n$ times that: {answer}.
:::

::: misses
- [n*20*t/sqrt(12)] Independent errors add in variance, so multiply by $\sqrt n$, not $n$.
- [sqrt(n)*10*t] That uses the half-width as if it were the standard deviation.
- [any] Not yet. Find one resistor's $\sigma$, then add variances.
:::
:::

::: check
given: n = 4 | 10; t = 1 | 5
answer: n*10*t
unit: Ω
tolerance: 0.5%

## Same string: {n} resistors of 1 kΩ, each within ±{t}%. What is the worst-case error of the total?

::: right
Right. The limits add: {answer}.
:::

::: misses
- [any] Not yet. Every resistor at its limit in the same direction.
:::
:::

::: check
given: s = 1.5 | 1.2 | 1.8
answer: (2 - s)^2/2
tolerance: 0.5%

## $X$ and $Y$ are independent and uniform on $[0, 1]$. What is $P(X + Y > \given{s})$?

::: right
Right. The corner of the triangle beyond {s} has area $\frac{(2 - s)^2}{2}$ = {answer}.
:::

::: misses
- [2 - s] Take the area of the triangle, not its base.
- [(2 - s)/2] The tail of a triangular density is a smaller triangle: square the base.
- [any] Not yet. The sum has a triangular density on $[0, 2]$.
:::
:::

::: check
given: m1 = 1 | 2; m2 = 3 | 4; s1 = 0.3 | 0.4; s2 = 0.4 | 0.3; c = 5.6 | 6.4
answer: Q((c - m1 - m2)/sqrt(s1^2 + s2^2))
tolerance: 1%

## $X$ is Gaussian with mean {m1} and standard deviation {s1}; $Y$, independent, has mean {m2} and standard deviation {s2}. What is $P(X + Y > \given{c})$?

::: right
Right. The sum is Gaussian with mean $\mu_1 + \mu_2$ and $\sigma = \sqrt{\sigma_1^2 + \sigma_2^2}$, so the answer is $Q\left(\frac{c - \mu}{\sigma}\right)$ = {answer}.
:::

::: misses
- [Q((c - m1 - m2)/(s1 + s2))] Standard deviations combine in quadrature.
- [any] Not yet. Find the sum's mean and standard deviation, then use the Q function.
:::
:::

::: check
given: n = 9 | 25 | 100; s = 2 | 3
answer: s*sqrt(n)
tolerance: 0.5%

## {n} independent errors, each with standard deviation {s}, are added. What is the standard deviation of the sum?

::: right
Right. $\sigma\sqrt n$ = {answer}.
:::

::: misses
- [n*s] That's what worst-case errors would do. Independent ones add in variance.
- [s/sqrt(n)] That's the spread of the average, not the sum.
- [any] Not yet. Variances add.
:::
:::

::: check
kind: choice

## $X$ and $Y$ are independent with densities $p_X$ and $p_Y$. What is the density of $X + Y$?

- [ ] $p_X(s) + p_Y(s)$
- [ ] $p_X(s)\,p_Y(s)$
- [x] The convolution $\int p_X(x)\,p_Y(s - x)\,dx$
- [ ] A Gaussian, always

::: right
Right. Each total $s$ collects every split into $x$ and $s - x$.
:::

::: misses
- [1] Adding densities gives a mixture, a density with total area 2 here; that describes choosing one variable or the other.
- [2] The product is the joint density at one point, not the density of the sum.
- [4] Only approximately, for many pieces; two uniforms give a triangle.
:::
:::

::: check
kind: parts
given: n = 4 | 9; s = 5 | 10

## {n} resistors in series each have independent errors of σ = {s} Ω.

::: part
answer: s*sqrt(n)
unit: Ω
tolerance: 0.5%

### What is the spread of the total?

::: misses
- [s*n] That's the worst-case stacking; independent errors add in squares.
- [any] $\sigma\sqrt n$.
:::
:::
:::

::: check
kind: derivation
vars: t = 0..1

## Two independent delays are each uniform on $[0, 1]$.

::: step
answer: t^2/2

### What is the chance their total is below $t$ (for $t \le 1$)?
:::

::: right
Right. The triangle under the line $x + y = t$ has area $\frac{t^2}{2}$.
:::
:::

::: check
kind: python

## Represent a uniform delay on [0, 1) ms by 1000 equal probabilities on a 1 µs grid. Convolve two of them with `np.convolve` and set `p_short` to the probability that the total is below 0.5 ms (the first 500 bins).

```python starter
import numpy as np

p_short = 0.0
print(p_short)
```

```python solution
import numpy as np

u = np.ones(1000) / 1000
total = np.convolve(u, u)
p_short = total[:500].sum()
print(p_short)
```

```python tests
assert abs(p_short - 0.125) < 0.001, f"The triangle's area below 0.5 is 0.125; got {p_short}."
```

::: right
Right. Convolving distributions gives the distribution of the sum: the triangle appears without any calculus.
:::
:::

::: explain
## Why does the worst-case error grow as $n$ but the typical error only as $\sqrt n$?

::: model
The worst case needs every error at its limit in the same direction. Typically the errors are independent, so some push up and some push down and they partly cancel. Variances add for independent errors, so the variance grows as $n$ and the standard deviation as $\sqrt n$. The more parts there are, the more cancellation there is, and the further the worst case lies from anything you'll actually see.
:::
:::

::: practice

::: check
given: m1 = 2 | 5; m2 = 3 | 7
answer: m1 + m2
tolerance: 0.5%

## $X$ has mean {m1} and $Y$ has mean {m2}. What is the mean of $X + Y$?

::: misses
- [any] Means always add.
:::
:::

::: check
given: s1 = 3 | 6; s2 = 4 | 8
answer: sqrt(s1^2 + s2^2)
tolerance: 0.5%

## Independent errors have standard deviations {s1} and {s2}. What is the standard deviation of their sum?

::: misses
- [s1 + s2] Variances add, not standard deviations.
- [any] $\sqrt{\sigma_1^2 + \sigma_2^2}$.
:::
:::

::: check
given: l1 = 2 | 3; l2 = 4 | 5
answer: sqrt(l1 + l2)
tolerance: 0.5%

## Two independent Poisson counts have means {l1} and {l2}. What is the standard deviation of their total?

::: misses
- [any] The total is Poisson with mean $\lambda_1 + \lambda_2$.
:::
:::
:::
