Lesson 2 of 5 · 8 min · Centre and spread
Spread: range, variance and standard deviation
What you will learn
- Why two datasets with the same mean can behave very differently
- Range, variance and standard deviation, with a worked example
- Sample versus population formulas (n versus n minus 1)
- The coefficient of variation for comparing spread
- Compute spread in Excel, SQL and Python
In simple words
Two delivery services both take 5 days on average. One always delivers in 4 to 6 days. The other sometimes takes 1 day and sometimes 12. The averages are equal but you would trust only one of them. Spread tells you how much the values wobble around the average. The standard deviation is the usual number for that wobble: small means steady, large means unpredictable.
Words to know
- Spread: how far values are scattered around the centre.
- Variance: the average of the squared distances from the mean.
- Standard deviation (SD): the square root of the variance, in the same units as the data.
- Population and sample: the whole group you care about, and the part of it you actually measured.
Why the average alone is not enough
Delivery days for two couriers, both with a mean of 5:
Courier A: 4, 5, 5, 5, 6 (steady)
Courier B: 1, 2, 5, 8, 9 (unpredictable)
If you only reported the mean, they look identical. Spread separates them.
Range and IQR
The range is the largest value minus the smallest: for A it is 2, for B it is 8. It is easy to explain but depends on just two values, so one outlier changes it entirely. The interquartile range (IQR) is the distance between the 25th and 75th percentile (see the percentiles lesson) and covers the middle half of the data, which makes it robust.
Variance and standard deviation
For each value, find its distance from the mean, square it (so negatives do not cancel out), and average those squares. That is the variance. Its square root is the standard deviation.
Using the earlier delivery data, 2, 3, 3, 3, 4, 4, 5, 6, 15, with mean 5:
| Value | Distance from mean | Squared |
|---|---|---|
| 2 | -3 | 9 |
| 3 | -2 | 4 |
| 3 | -2 | 4 |
| 3 | -2 | 4 |
| 4 | -1 | 1 |
| 4 | -1 | 1 |
| 5 | 0 | 0 |
| 6 | 1 | 1 |
| 15 | 10 | 100 |
| Sum | 124 |
- Sample variance: 124 / (9 - 1) = 15.5, so the sample standard deviation is the square root, about 3.94 days.
- Population variance: 124 / 9 = 13.78, so the population standard deviation is about 3.71 days.
Notice that the single 15-day delivery contributes 100 of the 124. Squaring makes the standard deviation very sensitive to outliers, which is useful when you want to be warned about big deviations and misleading when you want a robust view.
The standard deviation is in the same units as the data (days), which is why we take the square root. The variance is in squared units (days squared), harder to talk about but useful in calculations.
Sample or population?
- Use the population formula (divide by n) when your data is the whole group: every order this month, all employees.
- Use the sample formula (divide by n minus 1) when your data is a sample used to learn about a bigger group: 200 surveyed customers out of 50,000. Dividing by n minus 1 corrects for the fact that a sample tends to underestimate the spread of the full group.
In practice, analysts nearly always use the sample version (STDEV.S, stdev, STDDEV_SAMP). For large n the difference is tiny. Know that the two exist, because interviewers ask.
What a standard deviation means
For roughly bell-shaped data, about 68 percent of values fall within one SD of the mean, 95 percent within two, and 99.7 percent within three (the next lesson). If delivery times have mean 5 and SD 1, then a 9-day delivery is four SDs away and is surely worth investigating. If the SD is 4, a 9-day delivery is unremarkable. The same absolute gap means different things at different spreads.
Comparing spread: coefficient of variation
Is a spread of 5,000 large? For monthly salaries around 30,000 it is; for ones around 5,00,000 it is not. The coefficient of variation (CV) divides SD by the mean, so you can compare spread across different scales:
CV = standard deviation / mean
For the delivery data: 3.94 / 5 = 0.79, or 79 percent: relative spread is large. Use CV only for data with a meaningful zero and positive values (money, durations, counts), not for temperatures in Celsius.
Calculating spread
Excel:
=STDEV.S(A2:A10) =STDEV.P(A2:A10) =VAR.S(A2:A10) =MAX(A2:A10)-MIN(A2:A10)
SQL (PostgreSQL and MySQL):
SELECT STDDEV_SAMP(days) AS sd_sample,
STDDEV_POP(days) AS sd_population,
VAR_SAMP(days) AS var_sample,
MAX(days) - MIN(days) AS range_days
FROM deliveries;
Python:
import statistics as st
days = [2, 3, 3, 3, 4, 4, 5, 6, 15]
st.stdev(days) # 3.937 (sample)
st.pstdev(days) # 3.712 (population)
st.variance(days) # 15.5
pandas: df["days"].std() uses the sample formula by default (ddof=1), while NumPy's np.std defaults to the population formula (ddof=0). This difference surprises many people; set ddof explicitly when it matters.
Common mistakes
- Reporting the mean without any measure of spread.
- Mixing up the sample and population formulas between tools.
- Calling a data point an outlier without saying how many SDs away it is.
- Using the standard deviation on very skewed data where it is dominated by the tail (use IQR instead).
- Forgetting the units: the SD is in the data's units; the variance is squared.
Try it yourself
- Compute the mean and SD of Courier A and Courier B above by hand. Which is more reliable?
- Calculate the sample and population SD of your own list of 10 numbers with Python and check the difference.
- Add a 60 to the delivery data and see how the SD and the IQR change.
- Work out the coefficient of variation for two datasets on different scales and compare.
Check yourself
Why is the standard deviation more useful than the variance for reporting?
It is in the same units as the data, so it is easier to interpret; the variance is in squared units.
When do you divide by n minus 1 instead of n?
When the data is a sample used to estimate the spread of a larger population.
Why does one outlier affect the standard deviation so much?
Distances are squared, so a large deviation contributes disproportionately to the variance.
What does the coefficient of variation allow you to do?
Compare spread between datasets on different scales, since it is SD divided by the mean.
Interview questions
Two products have the same average rating of 4.0. How do you tell which is more consistent?
Compare their standard deviations (and look at the distribution of ratings); the lower SD is more consistent.
What is the difference between STDEV.S and STDEV.P?
STDEV.S divides by n minus 1 for a sample; STDEV.P divides by n for a whole population.
What does it mean if a value is three standard deviations from the mean?
It is very unusual if the data is roughly normal (under about 0.3 percent of values), so check whether it is an error or a real extreme case.
Summary
- The mean alone hides how much values vary; always report spread.
- Range is simple but fragile; IQR is robust; standard deviation is the standard measure.
- Divide by n minus 1 for samples and by n for a whole population; tools differ in their default.
- Use the coefficient of variation to compare spread across scales.
Going deeper
- statistics module: measures of spread:
stdev,pstdev,variance. - pandas DataFrame.std: the
ddofparameter. - PostgreSQL statistics aggregates:
stddev_samp,stddev_pop,var_samp.
See how much of this you can explain
A free mock interview asks you new questions on Statistics for analysts for Data Analyst and scores every answer, with what to fix.