Variance
Average squared distance.
To measure spread using every value, ask, how far is each value from the mean? Then average those distances. There's just one catch, which we'll solve with squaring.

- How far from the mean?
- For every value
- Then average
Step by step
First, the deviations, each score minus the mean. Some are negative, and if we just averaged them, they'd cancel out to zero. So we square them, which makes them all positive. The variance is the mean of the squared deviations. Run it. NP dot var gives the same answer, three hundred and twenty four.
import numpy as np
scores = np.array([55, 72, 90, 40, 81])
dev = scores - scores.mean()
print("deviations:", dev.round(1))
print("sum:", dev.sum().round(1))
print("variance:", (dev ** 2).mean().round(2))
print("np.var: ", np.var(scores).round(2))deviations: [-12.6 4.4 22.4 -27.6 13.4] sum: 0.0 variance: 324.24 np.var: 324.24
Samples divide by N minus one
There's a small twist. When you compute the variance of a sample, to estimate a population, you divide by N minus one instead of N. It corrects a slight underestimate. In NumPy, that's NP dot var with DDOF equals one. Pandas uses N minus one by default.
| key | value |
|---|---|
| Population | divide by n |
| Sample | divide by n − 1 |
| NumPy | np.var(x, ddof=1) |
| pandas | n − 1 by default |
Summary
The variance is the average squared distance from the mean. For a sample, divide by N minus one. Next, the standard deviation, which puts it back in normal units.
Key takeaways- Deviations from the mean
- Square, then average
- ddof=1 for samples