Deviation, variance and standard deviation · Statistics… | StemPy Lab
Course 2.3·Lesson 1 of 3
Deviation, variance and standard deviation
A deviation is the distance of a value from the mean. Their average with signs is always zero, so we either drop the signs, which gives the mean absolute deviation, or square them, which leads to the variance and the standard deviation. This lesson names both standard deviations: the population one divides by n, the sample one by n−1
Lesson 1 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
find the deviations, the mean absolute deviation (MAD), the variance and the standard deviation of a small data set;
tell the population standard deviation σ (divide by n) from the sample standard deviation s (divide by n−1), and choose the right one;
explain in words why a sample divides by n−1.
Six loaves
A bakery says its loaves weigh 500 g. An inspector weighs six loaves from one made-up morning: 496,498,500,501,502 and 503 g. Their mean is exactly 500 g. Does that mean every loaf is fine? No. We also need to know how far the loaves stray from 500 g.
Deviations
The deviation of a value is the value minus the mean, x−xˉ. For the loaves the deviations are −4,−2,0,1,2 and 3 g.
Six loaves of bread shown as blue dots, one on each row, on a scale of mass from 493 to 506 grams. A red vertical line marks the mean at 500 grams. From the mean an arrow runs to each dot, labelled with its deviation: minus 4 grams, minus 2 grams, 0 grams, plus 1 gram, plus 2 grams and plus 3 grams. Arrows to the left are orange and arrows to the right are green.
Add them: −4−2+0+1+2+3=0. This always happens, because the mean is the balance point. So the plain average of the deviations is useless as a measure of spread. There are two ways around it.
Way 1: drop the signs
The mean absolute deviation is the mean of the distances, without signs:
MAD=n∑∣x−xˉ∣.
For the loaves, MAD=64+2+0+1+2+3=612=2 g. A typical loaf is about 2 g from the mean. The MAD is easy to explain, but it is awkward in later mathematics.
Way 2: square the deviations
Squares are never negative. The mean of the squared deviations is the variance, and its square root is the standard deviation (SD), back in the units of the data. Here statistics makes a careful split.
When the data are the whole population, every member of the group you care about, divide by n. The results are written σ2 and σ (the Greek letter sigma):
σ=n∑(x−μ)2.
When the data are a sample from a bigger population, divide by n−1. The results are written s2 and s:
s=n−1∑(x−xˉ)2.
For the loaves, the squared deviations are 16,4,0,1,4 and 9, which add up to 34g2. The six loaves are a sample of everything the bakery makes, so the right choice is s=534≈2.61 g. If the six loaves were the whole batch, we would use σ=634≈2.38 g.
First Steps used the version that divides by n. Calculators often show both, as σn and σn−1, or as σx and sx. In Python, statistics.pstdev divides by n and statistics.stdev divides by n−1. Always say which one you used.
Why n−1 for a sample?
We want the spread around the true mean of the whole population. But a sample only gives us its own mean, xˉ. The sample mean always sits in the middle of the sample's own values, closer to them than the true mean usually is. So the squared deviations from xˉ come out a little too small. Dividing by n−1 instead of n makes the answer a little larger and corrects this, on average. The second program below checks it with a die.
You should find that all three measures grow as the amber value moves out, but the standard deviations grow faster than the MAD. Squaring makes a far value count much more. The sample SD is always a little larger than the population SD of the same five values.
Practise with new numbers
Try it in Python
The first program finds the deviations, the MAD and both standard deviations of the six loaves, and checks them with the statistics module.
The second program takes 20 000 samples of three rolls of a fair die. For each sample it finds the variance both ways, then averages them. The true variance of one roll is 1235≈2.92.
Common mistakes
Averaging the signed deviations. They always add up to zero.
Stopping at the variance. Its units are squared, such as g2. Take the square root.
Using the wrong version. A whole group: divide by n. A sample: divide by n−1.
Forgetting a zero deviation. A value equal to the mean still counts in n.