Variation
Knowing the centre of the data is only half the story. Variation (also called spread or dispersion) measures how much the values differ from each other and from the centre.
Two cities can both have a mean daily temperature of 15 °C over a year, one with a mild coastal climate (always around 15) and one with freezing winters and hot summers. The averages are equal; the experience of living there is not.
Same mean, different spread
import statistics
coastal = [12, 13, 14, 15, 15, 16, 16, 17, 18, 14]
inland = [-4, 2, 8, 15, 22, 30, 33, 26, 14, 4]
for name, temps in [("coastal", coastal), ("inland", inland)]:
print(f"{name:8s} mean={statistics.mean(temps):5.1f} "
f"min={min(temps):3d} max={max(temps):3d} "
f"stdev={statistics.stdev(temps):5.2f}")coastal mean= 15.0 min= 12 max= 18 stdev= 1.83
inland mean= 15.0 min= -4 max= 33 stdev=12.56
Both lists average exactly 15 °C, but the inland temperatures are spread over a range many times wider.
Measures of variation
| Measure | Formula / idea | Robust to outliers? | Lesson |
|---|---|---|---|
| Range | max − min |
no | Range |
| Interquartile range (IQR) | Q3 − Q1, the spread of the middle 50% |
yes | Interquartile Range |
| Variance | average squared distance from the mean | no | Standard Deviation |
| Standard deviation | square root of the variance, in the data's own units | no | Standard Deviation |
| Coefficient of variation | standard deviation / mean |
no | this lesson |
They pair naturally with averages: report the mean with the standard deviation, and the median with the IQR.
Why spread matters
Variation is often the key business or scientific question:
- A factory filling 500 g bags cares less about the mean weight than about how many bags fall below 500 g.
- A train that is "on average 2 minutes late" but sometimes 40 minutes late is worse than one that is always 3 minutes late.
- In inferential statistics, the more the data varies, the larger the sample you need to estimate the mean precisely.
import numpy as np
rng = np.random.default_rng(15)
machine_a = rng.normal(505, 2, 10_000) # mean 505 g, small spread
machine_b = rng.normal(505, 6, 10_000) # same mean, three times the spread
for name, bags in [("A", machine_a), ("B", machine_b)]:
under = (bags < 500).mean()
print(f"machine {name}: mean={bags.mean():.1f} g, std={bags.std(ddof=1):.2f} g, "
f"underweight bags={under:.2%}")machine A: mean=505.0 g, std=2.00 g, underweight bags=0.60%
machine B: mean=505.1 g, std=5.99 g, underweight bags=19.93%
Both machines aim for 505 g, but machine B produces about 20% underweight bags, against well under 1% for machine A. The difference is entirely due to variation.
Comparing spread across different scales: coefficient of variation
A standard deviation of 5 is huge for exam scores out of 20 and tiny for house prices. The coefficient of variation CV = s / x̄ expresses the spread relative to the mean, so you can compare variables measured in different units (only for ratio data with a positive mean):
import statistics
mouse_g = [21, 24, 19, 23, 22, 20, 25] # mouse weights in grams
elephant_kg = [4100, 5200, 3900, 4800, 6000, 4400, 5100]
for name, x in [("mice", mouse_g), ("elephants", elephant_kg)]:
m, s = statistics.mean(x), statistics.stdev(x)
print(f"{name:9s} mean={m:8.1f} stdev={s:7.1f} CV={s / m:.1%}")mice mean= 22.0 stdev= 2.2 CV=9.8%
elephants mean= 4785.7 stdev= 724.4 CV=15.1%
The elephants' standard deviation is hundreds of times larger, yet relative to their size the two groups vary by a comparable amount: about 10% of the mean for the mice and 15% for the elephants.
Everything at once
pandas computes the common spread measures in one go:
import pandas as pd
temps = pd.DataFrame({
"coastal": [12, 13, 14, 15, 15, 16, 16, 17, 18, 14],
"inland": [-4, 2, 8, 15, 22, 30, 33, 26, 14, 4],
})
spread = pd.DataFrame({
"range": temps.max() - temps.min(),
"IQR": temps.quantile(0.75) - temps.quantile(0.25),
"variance": temps.var(),
"std": temps.std(),
}).round(2)
print(spread) range IQR variance std
coastal 6 2.0 3.33 1.83
inland 37 20.0 157.78 12.56
Test yourself
Two datasets have the same mean. What can you conclude about their spread?
- It is also the same
- Nothing: spread must be measured separately
- The one with more values has less spread
- The spread is zero
Which spread measure is the natural partner of the median?
- Standard deviation
- Variance
- Interquartile range
- Coefficient of variation
What does a coefficient of variation of 25% mean?
- 25% of values are outliers
- The standard deviation is a quarter of the mean
- The range is 25
- The variance is 0.25