---
title: Statistics of measurements
heading: How sure are you of that average?
eyebrow: A4 · Lesson 48 · Statistics of measurements
xp: 40
---

::: hook
You measure a reference voltage 25 times: the readings scatter by a few millivolts around 2.500 V. The average is your best estimate, but how good is it? Ten times as many readings won't make each one less noisy, but it will pin down the average about three times better. Reporting a measurement properly means giving a value with an uncertainty, and that uncertainty comes from a few short formulas that this lesson puts into code.
:::

::: predict
## The readings scatter with a standard deviation of 4 mV. You average 100 of them. Roughly how uncertain is the average?

- [ ] 4 mV
- [x] 0.4 mV
- [ ] 0.04 mV
- [ ] 40 mV

::: reveal
About 0.4 mV. The uncertainty of a mean of $n$ independent readings is the spread of one reading divided by $\sqrt{n}$, and $\sqrt{100} = 10$. That's the **standard error of the mean**. Averaging more helps, but slowly: a hundred times more readings for ten times less uncertainty.
:::
:::

::: nameit
## Spread and the uncertainty of a mean

For readings $x_1, \ldots, x_n$:

| Quantity | Formula | In NumPy |
|---|---|---|
| mean | $\bar x = \frac{1}{n}\sum x_k$ | `x.mean()` |
| sample standard deviation | $s = \sqrt{\frac{1}{n - 1}\sum (x_k - \bar x)^2}$ | `x.std(ddof=1)` |
| standard error of the mean | $s / \sqrt{n}$ | `x.std(ddof=1) / np.sqrt(len(x))` |

`ddof=1` gives the $n - 1$ version; NumPy's default divides by $n$.

For readings whose noise is roughly normal (bell-shaped), the true mean lies within about $\pm 1.96$ standard errors of $\bar x$ in 95% of experiments: that's an approximate **95% confidence interval**. With fewer than about 20 readings, a slightly larger multiplier from the $t$ distribution is more accurate (A11 covers it).

## Histograms

`counts, edges = np.histogram(x, bins=20)` counts readings in equal bins. Looking at the histogram before trusting a mean and standard deviation catches two common problems: two groups mixed together (two humps) and outliers far from the rest.

## Combining uncertainties

When a result is calculated from measured inputs, its uncertainty follows from theirs. For independent inputs, **relative** uncertainties of a product or quotient add in quadrature, each weighted by its power:

$$y = \frac{V^2}{R} \quad\Rightarrow\quad \frac{\sigma_y}{y} = \sqrt{\left(2\frac{\sigma_V}{V}\right)^2 + \left(\frac{\sigma_R}{R}\right)^2}$$

For a sum or difference, the absolute uncertainties add in quadrature: $\sigma_{a+b} = \sqrt{\sigma_a^2 + \sigma_b^2}$. Adding in quadrature is smaller than adding straight, because independent errors partly cancel; A1's worst-case sum is the pessimistic bound.

## Combining measurements

Two measurements of the same thing with different uncertainties combine best as a **weighted mean**, each weighted by $1/\sigma^2$:

$$\bar x = \frac{x_1/\sigma_1^2 + x_2/\sigma_2^2}{1/\sigma_1^2 + 1/\sigma_2^2} \qquad \sigma = \frac{1}{\sqrt{1/\sigma_1^2 + 1/\sigma_2^2}}$$

The better measurement counts for more, and the combination is better than either.
:::

::: example
## 25 readings of a reference have a mean of 2.5012 V and a sample standard deviation of 3.0 mV. Report the voltage with a 95% interval.

::: solution
1. **The standard error.**
   $$\begin{aligned} \frac{s}{\sqrt{n}} &= \frac{3.0\ \text{mV}}{\sqrt{25}} \\ &= 0.60\ \text{mV} \end{aligned}$$
2. **The 95% interval.**
   $$\pm 1.96 \times 0.60\ \text{mV} \approx \pm 1.2\ \text{mV}$$
3. **Report it.** $2.5012 \pm 0.0012$ V (95%).
4. **The small-sample correction.** With 25 readings the $t$-distribution multiplier would be 2.06 instead of 1.96, giving ±1.24 mV, a small correction.

**Answer:** $2.5012 \pm 0.0012$ V, at 95% confidence.
:::
:::

::: example
## You measure 12.00 V ± 0.5% and 47.0 Ω ± 1% and compute $P = V^2/R$. What is the uncertainty in $P$?

::: solution
1. **The power.**
   $$\begin{aligned} P &= \frac{144}{47}\ \text{W} \\ &= 3.064\ \text{W} \end{aligned}$$
2. **The relative uncertainty.** $V$ is squared, so its 0.5% counts twice; combine in quadrature:
   $$\begin{aligned} u &= \sqrt{(2 \times 0.5\%)^2 + (1\%)^2} \\ &= \sqrt{1 + 1}\,\% \\ &= 1.41\% \end{aligned}$$
3. **The absolute uncertainty.** 1.41% of 3.064 W:
   $$P = 3.064 \pm 0.043\ \text{W}$$
4. **Compare with the worst case.** The worst-case bound would be $2 \times 0.5\% + 1\% = 2\%$; the quadrature value is what independent errors typically give.

**Answer:** $P = 3.064 \pm 0.043$ W, an uncertainty of 1.41%.
:::
:::

::: check
kind: python
given: k = 1..8 int; n = 16 | 25 | 100

## The starter simulates `n` = {n} noisy readings. Set `m`, `s` (sample standard deviation), `sem` (standard error of the mean) and `ci` to the half-width of an approximate 95% interval, $1.96 \times$ sem.

```python starter
import numpy as np
x = 2.5 + 0.004 * np.random.default_rng(k).normal(size=n)
m = s = sem = ci = 0
print(m, s, sem, ci)
```

```python solution
import numpy as np
x = 2.5 + 0.004 * np.random.default_rng(k).normal(size=n)
m = x.mean()
s = x.std(ddof=1)
sem = s / np.sqrt(n)
ci = 1.96 * sem
print(m, s, sem, ci)
```

```python tests
import numpy as np
xs = 2.5 + 0.004 * np.random.default_rng(k).normal(size=n)
assert abs(m - xs.mean()) < 1e-15, "m is the mean."
assert abs(s - xs.std(ddof=1)) < 1e-15, "s should use n − 1: x.std(ddof=1)."
assert abs(sem - xs.std(ddof=1) / np.sqrt(n)) < 1e-15, "sem is s / √n."
assert abs(ci - 1.96 * xs.std(ddof=1) / np.sqrt(n)) < 1e-15, "ci is 1.96 × sem."
```
:::

::: check
kind: python
given: k = 1..6 int

## The starter's readings are a mix of two groups. Set `counts, edges` with `np.histogram(x, bins=30)` and `mode_center` to the center of the bin with the most counts.

```python starter
import numpy as np
rng = np.random.default_rng(k)
x = np.concatenate([rng.normal(3.3, 0.02, 300), rng.normal(3.1, 0.02, 100)])
counts = edges = None
mode_center = 0
print(mode_center)
```

```python solution
import numpy as np
rng = np.random.default_rng(k)
x = np.concatenate([rng.normal(3.3, 0.02, 300), rng.normal(3.1, 0.02, 100)])
counts, edges = np.histogram(x, bins=30)
i = np.argmax(counts)
mode_center = (edges[i] + edges[i + 1]) / 2
print(mode_center)
```

```python tests
import numpy as np
rng = np.random.default_rng(k)
xs = np.concatenate([rng.normal(3.3, 0.02, 300), rng.normal(3.1, 0.02, 100)])
c, e = np.histogram(xs, bins=30)
i = np.argmax(c)
assert counts is not None and np.array_equal(counts, c), "Use np.histogram(x, bins=30)."
assert abs(mode_center - (e[i] + e[i + 1]) / 2) < 1e-12, "mode_center is the midpoint of the fullest bin's two edges."
```
:::

::: check
kind: python
given: V = 5 | 12; uV = 0.5 | 1; R = 47 | 100; uR = 1 | 5

## You compute $P = V^2/R$ from `V` = {V} V (±{uV}%) and `R` = {R} Ω (±{uR}%), both independent. Set `P` and `uP` to its relative uncertainty in percent, combined in quadrature.

```python starter
import numpy as np
P = V**2 / R
uP = uV + uR
print(P, uP)
```

```python solution
import numpy as np
P = V**2 / R
uP = np.sqrt((2 * uV)**2 + uR**2)
print(P, uP)
```

```python tests
import numpy as np
assert abs(P - V**2 / R) < 1e-12, "P is V²/R."
assert abs(uP - np.sqrt((2 * uV)**2 + uR**2)) < 1e-12, "V is squared, so its relative uncertainty counts twice; combine the two as √((2·uV)² + uR²)."
```
:::

::: check
kind: python
given: x1 = 3.301 | 3.298; s1 = 0.002 | 0.004; x2 = 3.296 | 3.305; s2 = 0.005 | 0.001

## Two instruments measure the same voltage: `x1` = {x1} V ± {s1} V and `x2` = {x2} V ± {s2} V. Set `xw` to the weighted mean and `sw` to its uncertainty.

```python starter
xw = (x1 + x2) / 2
sw = 0
print(xw, sw)
```

```python solution
w1, w2 = 1 / s1**2, 1 / s2**2
xw = (w1 * x1 + w2 * x2) / (w1 + w2)
sw = (w1 + w2) ** -0.5
print(xw, sw)
```

```python tests
w1, w2 = 1 / s1**2, 1 / s2**2
assert abs(xw - (w1 * x1 + w2 * x2) / (w1 + w2)) < 1e-12, "Weight each reading by 1/σ²."
assert abs(sw - (w1 + w2) ** -0.5) < 1e-12, "The combined uncertainty is 1/√(1/σ1² + 1/σ2²)."
assert sw < min(s1, s2), "The combination should be better than either."
```
:::

::: check
kind: python
given: k = 1..6 int

## The starter's 200 readings include a few glitches. Set `m` and `s` to their mean and sample standard deviation, then `clean` to an array of the readings within 3 `s` of `m`, `n_removed` to how many were dropped, and `m_clean` to the mean of `clean`.

```python starter
import numpy as np
rng = np.random.default_rng(k)
x = rng.normal(2.500, 0.002, 200)
x[rng.choice(200, 4, replace=False)] = [2.62, 2.41, 2.70, 2.35]
m = x.mean()
s = x.std()
clean = x
n_removed = 0
m_clean = m
print(m, m_clean, n_removed)
```

```python solution
import numpy as np
rng = np.random.default_rng(k)
x = rng.normal(2.500, 0.002, 200)
x[rng.choice(200, 4, replace=False)] = [2.62, 2.41, 2.70, 2.35]
m = x.mean()
s = x.std(ddof=1)
keep = np.abs(x - m) <= 3 * s
clean = x[keep]
n_removed = int((~keep).sum())
m_clean = clean.mean()
print(m, m_clean, n_removed)
```

```python tests
import numpy as np
rng = np.random.default_rng(k)
xx = rng.normal(2.500, 0.002, 200)
xx[rng.choice(200, 4, replace=False)] = [2.62, 2.41, 2.70, 2.35]
mm, ss = xx.mean(), xx.std(ddof=1)
kk = np.abs(xx - mm) <= 3 * ss
assert abs(s - ss) < 1e-15, "s is the sample standard deviation: x.std(ddof=1)."
assert len(clean) == kk.sum() and np.allclose(np.sort(clean), np.sort(xx[kk])), "clean keeps readings within 3*s of the mean."
assert n_removed == int((~kk).sum()), "n_removed counts the readings dropped."
assert abs(m_clean - xx[kk].mean()) < 1e-15, "m_clean is the mean of clean."
```

::: right
Right. The four glitches pull the mean and inflate the standard deviation; without them the mean sits back at the reference. Notice that the glitches themselves widen the 3σ window, which is why one pass may not catch every outlier.
:::
:::

::: check
given: s = 2 | 4 | 5 | 8; u = 0.5 | 0.2
answer: ceil((s/u)^2)
tolerance: 0.1

## Readings scatter with a standard deviation of {s} mV. How many must you average to bring the standard error of the mean down to {u} mV or less?

::: right
Right. The standard error is $s/\sqrt{n}$, so you need $\sqrt{n} \ge \given{s}/\given{u}$, or $n \ge (\given{s}/\given{u})^2$: {answer} readings.
:::

::: misses
- [ceil(s/u)] That makes $n$, not $\sqrt n$, equal to the ratio. Averaging $n$ readings divides the spread by $\sqrt n$, so square the ratio.
- [ceil((s/u)^2) - 1] One short: with that many the standard error is still above {u} mV.
- [any] Not yet. Solve $\given{s}/\sqrt{n} \le \given{u}$ for $n$ and round up.
:::
:::

::: explain
## Why does averaging 100 readings only make the result 10 times more precise, not 100 times?

::: model
The random errors in the readings partly cancel when you add them, but only partly: the spread of a sum of $n$ independent errors grows as $\sqrt{n}$, not $n$. Dividing by $n$ to get the average leaves a spread of $\sigma\sqrt{n}/n = \sigma/\sqrt{n}$. So a hundred readings shrink the uncertainty by $\sqrt{100} = 10$.
:::
:::

::: practice

::: check
kind: python
given: s = 1 | 4 | 10; target = 0.1 | 0.5

## Readings have a standard deviation of `s` = {s} mV. Set `n` to the smallest number of readings whose standard error of the mean is at most `target` = {target} mV.

```python starter
import math
n = 1
print(n)
```

```python solution
import math
n = math.ceil((s / target)**2)
print(n)
```

```python tests
import math
assert s / math.sqrt(n) <= target + 1e-12 and (n == 1 or s / math.sqrt(n - 1) > target), "Solve s/√n ≤ target: n = ceil((s/target)²)."
```
:::

::: check
kind: python
given: a = 10 | 20; ua = 0.1 | 0.3; b = 5 | 7; ub = 0.2 | 0.1

## Set `d = a - b` and `ud` to its uncertainty, with `a` = {a} ± {ua} and `b` = {b} ± {ub}, independent.

```python starter
d = a - b
ud = ua + ub
print(d, ud)
```

```python solution
d = a - b
ud = (ua**2 + ub**2) ** 0.5
print(d, ud)
```

```python tests
assert abs(ud - (ua**2 + ub**2) ** 0.5) < 1e-12, "For a sum or difference, add the absolute uncertainties in quadrature."
```
:::
:::
