Beginner: data foundations · Statistics made simple · topic 7 of 62 · 30 min
Spread: range, variance and standard deviation
Explanation
Why the centre is not enough
Two classes both have mean marks of 70. In class A the marks are 68, 70, 70, 72, 70. In class B they are 40, 55, 70, 85, 100. Same mean, completely different classes: in A everyone is close to 70, in B some students struggle and some excel. A teacher would plan very differently for each. Spread is the number that tells them apart.
The range
The range is the largest value minus the smallest. Class A has a range of 72 − 68 = 4; class B has 100 − 40 = 60. The range is easy to understand, but it uses only two values, so a single outlier can make it huge.
Deviations
A deviation is how far one value is from the mean: value minus mean. Values above the mean have positive deviations, values below have negative ones. You might think "average the deviations", but they always add up to zero, because the mean is exactly the balance point. So we need another idea.
Variance: the average squared deviation
Squaring each deviation makes it positive and gives big distances extra weight. The variance is the average of those squared deviations. In words: find the mean, find each value's distance from it, square the distances, then average them.
times = [30, 35, 40, 45, 50] # delivery times in minutes
mean = sum(times) / len(times) # 40.0
deviations = [t - mean for t in times] # [-10.0, -5.0, 0.0, 5.0, 10.0]
squares = [d ** 2 for d in deviations] # [100.0, 25.0, 0.0, 25.0, 100.0]
variance = sum(squares) / len(squares)
print(variance) # 50.0
print(variance ** 0.5) # 7.0710678118654755Standard deviation
Variance is in squared units (square minutes, square rupees), which nobody can picture. Taking the square root brings it back to the original units. That is the standard deviation (SD): roughly, the typical distance of a value from the mean. Here the SD is about 7.07 minutes, so most deliveries land within about 7 minutes of 40.
Population or sample: n or n − 1
If your data is the whole population (every delivery this month), divide the squared deviations by n, the number of values. If it is a sample used to estimate a bigger population, divide by n − 1. This is called Bessel's correction. The reason: a sample's own mean sits closer to its own points than the true population mean does, so the squared deviations come out a little too small. Dividing by one less makes up for that on average.
import statistics
times = [30, 35, 40, 45, 50]
print(statistics.pvariance(times), statistics.pstdev(times)) # 50 7.0710678118654755
print(statistics.variance(times), statistics.stdev(times)) # 62.5 7.905694150420948| Tool | Population (÷ n) | Sample (÷ n − 1) |
|---|---|---|
| Python statistics | pvariance, pstdev | variance, stdev |
| NumPy | np.std(x) (default ddof=0) | np.std(x, ddof=1) |
| pandas | df["col"].std(ddof=0) | df["col"].std() (default ddof=1) |
| Excel | STDEV.P | STDEV.S |
With large data the difference is tiny (dividing by 9,999 or 10,000 hardly matters). With small samples, like 5 values, it is noticeable: 7.07 versus 7.91 above.
Common mistakes
- Comparing averages without spread. Fix: report the mean (or median) together with the SD (or IQR, next topic).
- Forgetting the square root and reporting variance as if it were in the original units. Fix: SD = square root of the variance.
- Mixing population and sample formulas between tools. Fix: check ddof or the function name, and be consistent.
- Trusting the range when there is an outlier. Fix: use the SD or the IQR, which depend on all or the middle values.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Spread: range, variance and standard deviation · 1/7
In this lesson you will measure spread: how far values sit from the centre.
Prefer a video by a teacher? Find free videos on “variance and standard deviation explained simply population vs sample” on YouTube ↗
In real life: Delivery time promises
A quick-commerce or food delivery app does not only track the average delivery time; it also tracks how much delivery times vary. Two areas with the same 25-minute average are very different if one is always 22–28 minutes and the other swings between 10 and 45. Teams use the spread to set realistic promised times and to find areas that need more riders. The numbers below are made up.
import statistics
area_a = [22, 24, 25, 26, 28]
area_b = [10, 15, 25, 30, 45]
for name, t in [("A", area_a), ("B", area_b)]:
print(name, statistics.mean(t), round(statistics.pstdev(t), 2))Practice (0/3 done)
Write value_range(values) that returns the largest value minus the smallest. Return None for an empty list.
Without the statistics module, write pop_std(values) that returns the POPULATION standard deviation (divide by n), rounded to 2 decimal places. Return None for an empty list.
Without the statistics module, write sample_variance(values) that returns the SAMPLE variance (divide the sum of squared deviations by n − 1), rounded to 2 decimal places. With fewer than 2 values, return None.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Percentiles, quartiles and outliersStatistics made simple · same module
- The normal distribution and z-scoresStatistics made simple · same module
- Samples, the central limit theorem and confidence intervalsProbability and inference
- Linear regression and least squaresThe ML workflow, regression and classification
- Scaling and transforming numbersClustering, dimensionality reduction and feature engineering
- CSV files and simple statisticsPython Programming · Working with data
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Why are deviations squared before they are averaged?
Answer: Because the raw deviations always add up to zero
Positive and negative deviations cancel exactly around the mean, so squaring keeps every distance positive.
Delivery times have a variance of 49 square minutes. What is the standard deviation?
Answer: 7 minutes
The SD is the square root of the variance: √49 = 7, back in minutes.
You surveyed 50 of a company's 5,000 employees and want to estimate the spread of all salaries. Which formula fits?
Answer: Sample SD, dividing by n − 1
The 50 people are a sample used to estimate the population, so Bessel's correction (n − 1) applies.
For the same list, which is true about np.std(x) and pandas Series(x).std()?
Answer: NumPy divides by n and pandas by n − 1, so pandas gives a slightly bigger answer
np.std defaults to ddof=0 and pandas .std() to ddof=1; dividing by a smaller number gives a larger result.
Which measure of spread is most affected by a single extreme value?
Answer: The range
The range uses only the smallest and largest values, so one outlier changes it completely. The median and mode are not measures of spread.
A 95% CI for the average delivery time is 28 to 32 minutes. Which reading is best?Related
Answer: The method used catches the true average about 95% of the time; here it gives 28 to 32 minutes
A CI describes uncertainty about the mean, not where individual deliveries fall.
From Samples, the central limit theorem and confidence intervals
Which point does the least-squares line always pass through?Related
Answer: (x̄, ȳ), the two means
Because b = ȳ − w·x̄, the line goes through the mean point.
Where should the scaler learn its mean and standard deviation?Related
Answer: From the training data only
Anything else leaks test information.
Monthly salaries in a team are mostly ₹30–40 thousand, plus one of ₹5 lakh. Which summary best describes a typical salary?Related
Answer: Median
The median ignores how far out the one huge salary is; the mean is pulled up by it.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Statistics made simple” before its exam: Percentiles, quartiles and outliers, The normal distribution and z-scores, Correlation: how two things move together.
- Module 2 exam: Statistics made simple · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Probability and inference. 62 topics left in the course before the final exam.
← Previous: Types of data, and the centre: mean, median, mode