TExcelTAD
Home › Basic Math & Pre-Algebra › Range and Standard Deviation

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:

text
server A: 48, 50, 51, 49, 52, 50      steady
server B: 20, 85, 30, 90, 25, 50      all over the place

Both 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:

range A=52−48=4 msrange B=90−20=70 ms\begin{aligned} \text{range A} &= 52 - 48 = 4\ \text{ms} \\ \text{range B} &= 90 - 20 = 70\ \text{ms} \end{aligned}

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:

1. Find the mean xˉ.2. For each value, find its deviation x−xˉ.3. Square each deviation and average the squares→ the variance4. Take the square root of the variance→ the standard deviation\begin{aligned} &\text{1. Find the mean } \bar{x}. \\ &\text{2. For each value, find its deviation } x - \bar{x}. \\ &\text{3. Square each deviation and average the squares} && \to\ \text{the variance} \\ &\text{4. Take the square root of the variance} && \to\ \text{the standard deviation} \end{aligned}

For server A (mean 50):

deviations: −2,0,1,−1,2,0squares: 4,0,1,1,4,0sum=10variance=106≈1.667standard deviation=1.667≈1.29 ms\begin{aligned} \text{deviations: } & -2, 0, 1, -1, 2, 0 \\ \text{squares: } & 4, 0, 1, 1, 4, 0 && \text{sum} = 10 \\ \text{variance} &= \frac{10}{6} \approx 1.667 \\ \text{standard deviation} &= \sqrt{1.667} \approx 1.29\ \text{ms} \end{aligned}

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".

python · try it yourself
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 nn and is the population standard deviation (σ\sigma). When the data is a sample from a bigger group and you want to estimate the whole group's spread, statisticians divide by n−1n - 1 instead, giving the sample standard deviation (ss), which is slightly larger. Libraries differ in their defaults:

python · try it yourself
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 n−1n - 1 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 "mean±std dev\text{mean} \pm \text{std dev}", for example 12.4±0.3 ms12.4 \pm 0.3\ \text{ms}. 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 27−3=2427 - 3 = 24.

Exercise 2: Standard deviation by hand

Find the population standard deviation of 2, 4, 4, 4, 5, 5, 7, 9.

Answer
Solution
mean=408=5deviations: −3,−1,−1,−1,0,0,2,4squares: 9,1,1,1,0,0,4,16sum=32variance=328=4std dev=4=2\begin{aligned} \text{mean} &= \frac{40}{8} = 5 \\ \text{deviations: } & -3, -1, -1, -1, 0, 0, 2, 4 \\ \text{squares: } & 9, 1, 1, 1, 0, 0, 4, 16 && \text{sum} = 32 \\ \text{variance} &= \frac{32}{8} = 4 \\ \text{std dev} &= \sqrt{4} = 2 \end{aligned}
python · try it yourself
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 x−xˉx - \bar{x} do not change. Doubling doubles every deviation, so the range doubles and the standard deviation (average squared deviation\sqrt{\text{average squared deviation}}) doubles too.

python · try it yourself
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
python · try it yourself
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 ±1\pm 1, ±2\pm 2, ±3\pm 3 and 0, so the variance is 1+1+4+4+9+9+07=287=4\frac{1+1+4+4+9+9+0}{7} = \frac{28}{7} = 4 and the standard deviation 2. The value z=x−meansdz = \frac{x - \text{mean}}{\text{sd}} counts "how many standard deviations away".

Test yourself

What is the range of 7, 2, 11, 5?

  1. 9
  2. 6.25
  3. 11
  4. 4

Why are the deviations squared when computing the standard deviation?

  1. To make the result bigger
  2. So positive and negative deviations don't cancel out
  3. Because the mean is a square
  4. To remove outliers

np.std(data) with no other arguments computes…

  1. the sample standard deviation (divide by n−1n - 1)
  2. the variance
  3. the population standard deviation (divide by nn)
  4. the range