---
title: Random variables, mean and variance
heading: Two numbers that summarize anything random
eyebrow: A11 · Lesson 9 · Random variables, mean and variance
xp: 40
---

::: hook
A bag of 1 kΩ, ±5% resistors doesn't contain 1 kΩ resistors. It contains values scattered between 950 and 1050 Ω. Put ten in series and the worst case is off by 500 Ω, but you'll almost never see that: the errors mostly cancel. How much do they cancel? Two numbers, the mean and the standard deviation, answer that for any random quantity, and they combine by simple rules when quantities add.

::: play
- Each draw is the total error of $k$ resistors in series, each one somewhere between $-t$% and $+t$% of 1 kΩ, all values equally likely.
- Start with $k = 1$. The histogram is flat. Now raise $k$. How does its shape change?
- Compare the measured spread with the worst case as $k$ grows. Which grows faster?
:::
:::

```sim plot
view: random
x: d = -400..400 Ω
slider: k = 1..10 step 1 = 1
slider: t = 1..5 step 0.5 = 5 %
sample: 1000*t/100*(2*rand(k) - k) | mean: M | sd: S
trials: 20000
bins: 50
readout: mean error = M Ω
readout: spread (sd) = S Ω
readout: theory: 10t·√(k/3) = 10*t*sqrt(k/3) Ω
readout: worst case = 10*t*k Ω
```

::: predict
## Ten ±5% resistors of 1 kΩ are put in series. The worst-case error is ±500 Ω. What is the typical size of the error (the standard deviation)?

- [ ] About 500 Ω
- [ ] About 290 Ω
- [x] About 90 Ω
- [ ] About 29 Ω

::: reveal
About 90 Ω. One resistor's error, spread evenly over ±50 Ω, has a standard deviation of $50/\sqrt3 \approx 29$ Ω. For independent errors the *variances* add, so ten of them give $29\sqrt{10} \approx 91$ Ω. The worst case needs all ten at their limits in the same direction, which essentially never happens. Designing to worst case is safe but expensive; designing to the standard deviation is how mass production works.
:::
:::

::: nameit
## Random variables

A **random variable** is a number that comes out of a random experiment: the face of a die, a resistor's value, the noise voltage at an instant. Its **distribution** says how likely each value is: a list of probabilities for a discrete variable, or a **probability density** $p(x)$ for a continuous one, where $p(x)\,dx$ is the chance of landing in a small range $dx$.

**The mean** (expected value) is the long-run average:

$$\mu = E[X] = \sum x_i\,P(x_i) \qquad\text{or}\qquad \int x\,p(x)\,dx$$

**The variance** is the mean squared distance from the mean, and the **standard deviation** σ is its square root, back in the variable's own units:

$$\sigma^2 = E\big[(X - \mu)^2\big] = E[X^2] - \mu^2$$

**Common distributions:**

| Variable | Mean | Variance |
|---|---|---|
| one fair die | 3.5 | 35/12 |
| coin, 1 with probability $p$ | $p$ | $p(1 - p)$ |
| uniform between $a$ and $b$ | $\frac{a + b}{2}$ | $\frac{(b - a)^2}{12}$ |

**Rules** for constants $a$ and $b$:

- Scaling and shifting: $E[aX + b] = aE[X] + b$ and $\text{Var}(aX + b) = a^2\,\text{Var}(X)$. Shifting doesn't change the spread.
- Sums: means always add. For **independent** variables, variances add too:

$$\sigma_{X + Y} = \sqrt{\sigma_X^2 + \sigma_Y^2}$$

Standard deviations don't add; they combine like the sides of a right triangle. That's the **root-sum-square** (RSS) rule for stacking tolerances: $n$ identical independent errors give $\sigma\sqrt n$, not $n\sigma$.
:::

::: example
## Find the mean, variance and standard deviation of one fair die.

::: solution
1. **Mean.**
   $$\begin{aligned} \mu &= \frac{1 + 2 + \cdots + 6}{6} \\ &= 3.5 \end{aligned}$$
2. **Mean square.**
   $$\begin{aligned} E[X^2] &= \frac{1 + 4 + 9 + 16 + 25 + 36}{6} \\ &= \frac{91}{6} \end{aligned}$$
3. **Variance.**
   $$\begin{aligned} \sigma^2 &= \frac{91}{6} - 3.5^2 \\ &= \frac{35}{12} \\ &= 2.92 \end{aligned}$$
4. **Standard deviation.**
   $$\sigma = 1.71$$

**Answer:** the mean is 3.5, the variance $\frac{35}{12} = 2.92$ and the standard deviation 1.71.
:::
:::

::: example
## Three resistors in series each have a value spread of σ = 10 Ω, independently. What is the spread of the total? And of $Y = 2X + 1$ for a variable $X$ with σ = 10?

::: solution
1. **Variances add.**
   $$\begin{aligned} \sigma_\text{total} &= \sqrt{3 \times 10^2} \\ &= 17.3\ \Omega \end{aligned}$$
   less than the 30 Ω you'd get by adding the spreads.
2. **The shift.** For $Y = 2X + 1$, adding 1 does nothing to the spread.
3. **The scale doubles it.**
   $$\sigma_Y = 20$$

**Answer:** the total has a spread of 17.3 Ω, and $Y$ has $\sigma_Y = 20$.
:::
:::

::: example
## A random bit is +1 V with probability 0.7 and −1 V otherwise. Find its mean and variance.

::: solution
1. **Mean.**
   $$\begin{aligned} E[X] &= 0.7 - 0.3 \\ &= 0.4\ \text{V} \end{aligned}$$
2. **Mean square.** Both levels square to 1, so $E[X^2] = 1$.
3. **Variance.**
   $$\begin{aligned} \text{Var} &= 1 - 0.4^2 \\ &= 0.84\ \text{V}^2 \end{aligned}$$
   a standard deviation of 0.917 V.
4. **The DC component.** An unbalanced bit stream has a DC component of 0.4 V, which an AC-coupled link has to cope with; line codes are designed to keep it at zero.

**Answer:** the mean is 0.4 V and the variance 0.84 V², a standard deviation of 0.917 V.
:::
:::

::: check
kind: parts
given: a = 1 | 2; b = 4 | 5; c = 7 | 9

## A random voltage is {a} V with probability 0.2, {b} V with probability 0.5 and {c} V with probability 0.3.

::: part
answer: 0.2*a + 0.5*b + 0.3*c
unit: V
tolerance: 0.5%

### What is its mean?

::: misses
- [(a + b + c)/3] The values aren't equally likely: weight each by its probability.
- [any] $\mu = \sum x_iP(x_i)$.
:::
:::

::: part
answer: sqrt(0.2*a^2 + 0.5*b^2 + 0.3*c^2 - (0.2*a + 0.5*b + 0.3*c)^2)
unit: V
tolerance: 0.5%

### What is its standard deviation?

::: misses
- [0.2*a^2 + 0.5*b^2 + 0.3*c^2 - (0.2*a + 0.5*b + 0.3*c)^2] That's the variance, in V². Take the square root.
- [any] $\sigma = \sqrt{E[X^2] - \mu^2}$.
:::
:::

::: right
Right. The mean is pulled toward the likely values, and σ measures the typical distance from it.
:::
:::

::: check
given: a = 10 | 20 | 50 | 100
answer: a/sqrt(3)
unit: mV
tolerance: 0.5%

## An error is equally likely to be anywhere between −{a} mV and +{a} mV. What is its standard deviation?

::: right
Right. Uniform over a width of $2a$: $\sigma = 2a/\sqrt{12} = a/\sqrt3$ = {answer}.
:::

::: misses
- [a] That's the largest the error can be. Typical errors are smaller.
- [a/2] Average distance and standard deviation differ: for a uniform spread, $\sigma = a/\sqrt3$.
- [2*a/12] Take the square root: the variance is $(2a)^2/12$.
- [any] Not yet. Use the uniform distribution's variance, $(b - a)^2/12$.
:::
:::

::: check
given: m = 2..9 int; s = 1 | 2 | 3; a = 2 | 3 | 5; b = -4 | -1 | 6
answer: a*s
tolerance: 0.5%

## A voltage X has mean {m} V and standard deviation {s} V. What is the standard deviation of $Y = \given{a}X + (\given{b})$?

::: right
Right. Scaling by {a} multiplies the spread by {a}; adding a constant just slides everything along: {answer} V.
:::

::: misses
- [a*s + b] Adding a constant shifts the mean but doesn't change the spread.
- [a^2*s] That's how the variance scales. The standard deviation scales by |a|.
- [any] Not yet. $\sigma_Y = |a|\sigma_X$.
:::
:::

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

## Two independent noise sources with standard deviations {s1} mV and {s2} mV add together. What is the standard deviation of the total?

::: right
Right. Variances add: $\sqrt{\given{s1}^2 + \given{s2}^2}$ = {answer}.
:::

::: misses
- [s1 + s2] Standard deviations don't add; variances do.
- [s1^2 + s2^2] That's the variance. Take the square root.
- [any] Not yet. $\sigma = \sqrt{\sigma_1^2 + \sigma_2^2}$.
:::
:::

::: check
given: p = 0.1 | 0.2 | 0.3 | 0.4
answer: sqrt(p*(1 - p))
tolerance: 0.5%

## A bit is 1 with probability {p} and 0 otherwise. What is its standard deviation?

::: right
Right. $\sqrt{p(1 - p)}$ = {answer}.
:::

::: misses
- [p*(1 - p)] That's the variance.
- [p] That's the mean.
- [any] Not yet. Variance $= E[X^2] - \mu^2 = p - p^2$.
:::
:::

::: check
kind: parts
given: p = 0.6 | 0.8; A = 1 | 3

## A bit is $+\given{A}$ V with probability {p} and $-\given{A}$ V otherwise.

::: part
answer: A*(2*p - 1)
unit: V
tolerance: 0.5%

### What is its mean?

::: misses
- [any] $A p - A(1 - p)$.
:::
:::

::: part
answer: A^2*(1 - (2*p - 1)^2)
unit: V²
tolerance: 0.5%

### What is its variance?

::: misses
- [A^2] Subtract the mean squared.
- [any] $E[X^2] - E[X]^2$.
:::
:::
:::

::: check
kind: derivation
vars: a = 1..10, s = 0.1..5, b = -5..5

## $X$ has mean $a$ and standard deviation $s$; $Y = 3X + b$.

::: step
answer: 3*a + b

### What is the mean of $Y$?
:::

::: step
answer: 3*s

### What is its standard deviation?
:::

::: right
Right. Shifting moves the mean only; scaling multiplies the standard deviation by the scale's size.
:::
:::

::: explain
## Why do the standard deviations of independent errors combine as $\sqrt{\sigma_1^2 + \sigma_2^2}$ rather than simply adding?

::: model
Adding standard deviations would assume both errors are always at their typical size in the same direction. Independent errors are just as likely to push opposite ways as the same way, so they partly cancel. Working through the squares, the cross term $E[(X - \mu_X)(Y - \mu_Y)]$ averages to zero when the errors are independent, leaving the variances to add. The total spread is therefore larger than either one but smaller than their sum.
:::
:::

::: practice

::: check
given: n = 2..9 int
answer: 3.5*n
tolerance: 0.5%

## What is the mean of the sum of {n} fair dice?

::: misses
- [3.5] Means add: one die averages 3.5.
- [any] $n \times 3.5$.
:::
:::

::: check
given: n = 4 | 9 | 16 | 25; s = 1 | 2 | 5
answer: s*sqrt(n)
unit: mV
tolerance: 0.5%

## {n} independent errors each have σ = {s} mV. What is the σ of their sum?

::: misses
- [s*n] Variances add, not standard deviations: $\sigma\sqrt n$.
- [s/sqrt(n)] That's for their average. Their sum spreads more.
- [any] $\sigma\sqrt n$.
:::
:::

::: check
given: v = 4 | 9 | 16 | 25
answer: sqrt(v)
unit: mV
tolerance: 0.5%

## A noise voltage has a variance of {v} mV². What is its standard deviation?

::: misses
- [v] Take the square root.
- [any] $\sigma = \sqrt{\text{variance}}$.
:::
:::
:::
