Range and Standard Deviation
Two data sets can have the same average and still look completely different. Measures of spread describe how far the values scatter around their centre. The simplest is the range; the most widely used is the standard deviation, which you meet in every statistics library, A/B test and anomaly alert.
Same mean, different story
Two servers both answer requests in 50 ms on average:
server A: 48, 50, 51, 49, 52, 50 steady
server B: 20, 85, 30, 90, 25, 50 all over the placeBoth means are 50 ms, but server B is far less predictable. We need a number that says so.
The range
The range is the largest value minus the smallest:
It is easy to compute and to explain, but it looks only at the two most extreme values. A single outlier changes it completely, and it says nothing about the values in between.
The standard deviation
The standard deviation measures the typical distance of the values from the mean. It is built in four steps:
For server A (mean 50):
Why square? Deviations above and below the mean would cancel out if we just added them (they always sum to 0). Squaring makes them all positive, and the square root at the end brings the result back to milliseconds instead of "squared milliseconds".
import math
def spread(values):
n = len(values)
mean = sum(values) / n
variance = sum((x - mean) ** 2 for x in values) / n
return mean, max(values) - min(values), math.sqrt(variance)
for name, data in [("A", [48, 50, 51, 49, 52, 50]), ("B", [20, 85, 30, 90, 25, 50])]:
mean, rng, sd = spread(data)
print(f"server {name}: mean {mean:.1f} range {rng} std dev {sd:.2f}")server A: mean 50.0 range 4 std dev 1.29
server B: mean 50.0 range 70 std dev 28.14
Population or sample?
The version above divides by and is the population standard deviation (). When the data is a sample from a bigger group and you want to estimate the whole group's spread, statisticians divide by instead, giving the sample standard deviation (), which is slightly larger. Libraries differ in their defaults:
import statistics as st
import numpy as np
b = [20, 85, 30, 90, 25, 50]
print("statistics.pstdev (population):", round(st.pstdev(b), 2))
print("statistics.stdev (sample) :", round(st.stdev(b), 2))
print("numpy std default (population):", round(float(np.std(b)), 2))
print("numpy std ddof=1 (sample) :", round(float(np.std(b, ddof=1)), 2))statistics.pstdev (population): 28.14
statistics.stdev (sample) : 30.82
numpy std default (population): 28.14
numpy std ddof=1 (sample) : 30.82
For an introduction, the key idea is the same either way. The Standard Deviation lesson in the Statistics course explains the in detail.
Note: For roughly bell-shaped data, about 68% of values lie within one standard deviation of the mean and about 95% within two. So "mean 50, std dev 1.29" tells you almost every request of server A takes between about 47 and 53 ms.
In software
- Anomaly alerts: a monitor can flag a metric when it is more than 3 standard deviations from its recent mean ("3-sigma rule"). For server A, 60 ms would be an alarm; for server B it is normal.
- Benchmarks report results as "", for example . A large spread means the measurement is noisy and you need more runs.
- Machine learning: features are often standardised: subtract the mean and divide by the standard deviation, so every feature has mean 0 and spread 1 (see scale).
- Game balance: two weapons with the same average damage feel different if one has a much larger spread.
Exercises
Exercise 1: Range
Find the range of 14, 3, 27, 9, 18.
Answer
Solution
Largest 27, smallest 3, so the range is .
Exercise 2: Standard deviation by hand
Find the population standard deviation of 2, 4, 4, 4, 5, 5, 7, 9.
Answer
Solution
import statistics as st
print(st.pstdev([2, 4, 4, 4, 5, 5, 7, 9]))2.0
Exercise 3: Shift and scale
Every value in a data set is increased by 10. What happens to the mean, the range and the standard deviation? What if every value is instead doubled?
Answer
Adding 10: the mean goes up by 10; range and standard deviation stay the same. Doubling: all three double.
Solution
Adding a constant moves every value and the mean by the same amount, so the deviations do not change. Doubling doubles every deviation, so the range doubles and the standard deviation () doubles too.
import statistics as st
d = [2, 4, 4, 4, 5, 5, 7, 9]
for label, data in [("original", d), ("+10", [x + 10 for x in d]), ("x2", [2 * x for x in d])]:
print(label, st.mean(data), max(data) - min(data), st.pstdev(data))original 5 7 2.0
+10 15 7 2.0
x2 10 14 4.0
Exercise 4: 3-sigma alert
Recent response times are [101, 98, 103, 99, 100, 97, 102]. Using the population standard deviation, which of the new values 104, 106 and 95 are more than 3 standard deviations from the mean?
Answer
Mean 100, standard deviation 2, so the alert band is 94 to 106. None of them is outside (106 is exactly on the edge).
Solution
import statistics as st
recent = [101, 98, 103, 99, 100, 97, 102]
m, sd = st.mean(recent), st.pstdev(recent)
print("mean", m, "sd", sd)
for x in [104, 106, 95]:
z = (x - m) / sd
print(x, "z =", z, "alert" if abs(z) > 3 else "ok")mean 100 sd 2.0
104 z = 2.0 ok
106 z = 3.0 ok
95 z = -2.5 ok
The deviations are , , and 0, so the variance is and the standard deviation 2. The value counts "how many standard deviations away".
Test yourself
What is the range of 7, 2, 11, 5?
- 9
- 6.25
- 11
- 4
Why are the deviations squared when computing the standard deviation?
- To make the result bigger
- So positive and negative deviations don't cancel out
- Because the mean is a square
- To remove outliers
np.std(data) with no other arguments computes…
- the sample standard deviation (divide by )
- the variance
- the population standard deviation (divide by )
- the range