Standard Deviation
The standard deviation is the most widely used measure of spread. Roughly, it is the typical distance of the values from their mean. It is the square root of the variance, the average squared deviation from the mean.
The formulas
For a population of N values with mean μ:
variance σ² = Σ(x − μ)² / N
std. dev. σ = √σ²For a sample of n values with mean x̄:
variance s² = Σ(x − x̄)² / (n − 1)
std. dev. s = √s²Dividing by n − 1 (called Bessel's correction) makes s² an unbiased estimate of the population variance (see Parameters and Statistics). n − 1 is the number of degrees of freedom: once you know the mean and n − 1 deviations, the last deviation is forced, because deviations sum to zero.
Step by step
Five friends' scores in a quiz:
import math
scores = [6, 9, 7, 10, 8]
n = len(scores)
mean = sum(scores) / n
print("mean:", mean)
print(" x x-mean (x-mean)^2")
squares = []
for x in scores:
d = x - mean
squares.append(d * d)
print(f"{x:2d} {d:6.1f} {d * d:9.2f}")
ss = sum(squares)
print("sum of squares:", ss)
print("sample variance s^2 =", ss / (n - 1), " s =", round(math.sqrt(ss / (n - 1)), 4))
print("population var σ^2 =", ss / n, " σ =", round(math.sqrt(ss / n), 4))mean: 8.0
x x-mean (x-mean)^2
6 -2.0 4.00
9 1.0 1.00
7 -1.0 1.00
10 2.0 4.00
8 0.0 0.00
sum of squares: 10.0
sample variance s^2 = 2.5 s = 1.5811
population var σ^2 = 2.0 σ = 1.4142
Squaring does two things: it stops positive and negative deviations from cancelling, and it gives large deviations extra weight. Taking the square root at the end returns the result to the original units (quiz points rather than squared quiz points).
In Python
Watch out for the different defaults. NumPy uses the population formula unless you pass ddof=1; pandas and statistics.stdev use the sample formula:
import statistics
import numpy as np
import pandas as pd
scores = [6, 9, 7, 10, 8]
print("statistics.stdev :", round(statistics.stdev(scores), 4)) # sample
print("statistics.pstdev:", round(statistics.pstdev(scores), 4)) # population
print("np.std (default) :", round(np.std(scores), 4)) # population! ddof=0
print("np.std(ddof=1) :", round(np.std(scores, ddof=1), 4)) # sample
print("pandas .std() :", round(pd.Series(scores).std(), 4)) # sample, ddof=1statistics.stdev : 1.5811
statistics.pstdev: 1.4142
np.std (default) : 1.4142
np.std(ddof=1) : 1.5811
pandas .std() : 1.5811
Note: Forgetting
ddof=1with NumPy is a classic bug. For largenthe two versions are nearly identical; for small samples the difference matters.
Interpreting the standard deviation
For data that is roughly bell-shaped, the empirical rule (68–95–99.7 rule) says:
| Interval | Share of the data |
|---|---|
| mean ± 1 SD | about 68% |
| mean ± 2 SD | about 95% |
| mean ± 3 SD | about 99.7% |
Let's check it on simulated adult heights:
import numpy as np
rng = np.random.default_rng(99)
heights = rng.normal(172, 9, size=10_000)
m, s = heights.mean(), heights.std(ddof=1)
print(f"mean = {m:.2f} cm, sd = {s:.2f} cm")
for k in [1, 2, 3]:
inside = np.mean(np.abs(heights - m) <= k * s)
print(f"within {k} SD ({m - k * s:.1f} to {m + k * s:.1f} cm): {inside:.1%}")mean = 171.99 cm, sd = 8.93 cm
within 1 SD (163.1 to 180.9 cm): 68.0%
within 2 SD (154.1 to 189.8 cm): 95.7%
within 3 SD (145.2 to 198.8 cm): 99.7%
The rule only holds for approximately normal data. For skewed data, use it with care, or use percentiles instead.
Standard scores (z-scores)
The standard deviation is also a natural unit of distance. The z-score tells you how many standard deviations a value lies from the mean:
z = (x − x̄) / sz-scores make values from different scales comparable. Which is more impressive: 78 points on a maths test (mean 65, SD 8) or 82 points on a history test (mean 74, SD 10)?
def z_score(x, mean, sd):
return (x - mean) / sd
print("maths :", z_score(78, 65, 8))
print("history:", z_score(82, 74, 10))maths : 1.625
history: 0.8
The maths result is 1.625 SDs above average, the history result only 0.8 SDs. Relative to their classmates, the maths result is far more impressive. You will meet z-scores again in the Standard Normal Distribution lesson.
Test yourself
What is the population standard deviation of 2, 4, 4, 4, 5, 5, 7, 9 (mean = 5)?
- 1
- 2
- 4
- 2.14
Why does the sample variance divide by n − 1 instead of n?
- To make the result smaller
- To correct the downward bias that comes from measuring deviations from the sample mean
- Because one value is always an outlier
- It is just a convention with no effect
A value has a z-score of −2. What does that mean?
- It is 2 units below the mean
- It is 2 standard deviations below the mean
- It is an error
- It is in the bottom 2%