Skip to content

Beginner: data foundations · Statistics made simple · topic 7 of 62 · 30 min

Spread: range, variance and standard deviation

Explanation

Two groups can have the same average but behave very differently. Spread measures how far values sit from the centre. The range is max minus min; the variance is the average squared distance from the mean; and the standard deviation is the square root of the variance, back in the original units. For a sample you divide by n − 1 instead of n.

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.

python
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.0710678118654755
The squared deviations add up to 250; divided by 5 values, the variance is 50.0 square minutes.

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

Think of it like this: Think of two chai stalls. Both serve your chai in 5 minutes on average. At the first, it is always 4 to 6 minutes (small SD). At the second, sometimes 1 minute and sometimes 12 (large SD). If you have a train to catch, the SD matters as much as the average.

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.

python
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
The p in pvariance and pstdev means population (divide by n). variance and stdev are the sample versions (divide by n − 1): 250 / 4 = 62.5.
ToolPopulation (÷ n)Sample (÷ n − 1)
Python statisticspvariance, pstdevvariance, stdev
NumPynp.std(x) (default ddof=0)np.std(x, ddof=1)
pandasdf["col"].std(ddof=0)df["col"].std() (default ddof=1)
ExcelSTDEV.PSTDEV.S
Careful: NumPy and pandas use different defaults: np.std divides by n, pandas .std() divides by n − 1. The same data can give two different answers. Always check which one a tool uses, and say which one you report.

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

Spread

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.

python
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))
Output: A 25 2.0, then B 25 12.25. Same average, very different reliability.

Practice (0/3 done)

Task 1 · Code · Python

Write value_range(values) that returns the largest value minus the smallest. Return None for an empty list.

Task 2 · Code · Python

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.

Task 3 · Code · Python

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

Question 1

1.Why are deviations squared before they are averaged?

Question 2

2.Delivery times have a variance of 49 square minutes. What is the standard deviation?

Question 3

3.You surveyed 50 of a company's 5,000 employees and want to estimate the spread of all salaries. Which formula fits?

Question 4

4.For the same list, which is true about np.std(x) and pandas Series(x).std()?

Question 5

5.Which measure of spread is most affected by a single extreme value?

Read more (free)

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.

From Linear regression and least squares

Where should the scaler learn its mean and standard deviation?Related

Answer: From the training data only

Anything else leaks test information.

From Scaling and transforming numbers

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.

From Types of data, and the centre: mean, median, mode

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

  1. 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.
  2. Module 2 exam: Statistics made simple · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. 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