Statistics: mean, spread and sampling
About this lesson
Statistics turns a pile of numbers into a few that describe it. Follow one small data set, the minutes a bus was late on 8 days, as you find its centre (the mean and the median), its spread (the variance and the standard deviation), and then see what happens when you only have a random sample: why the sample variance divides by n − 1, and how much a sample mean wobbles.
Start with data
Statistics starts with data. A bus was late on 8 days by 2, 4, 4, 4, 5, 5, 7 and 9 minutes. For now, treat these 8 days as the whole population: every value we care about.
>>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> len(x), min(x), max(x) (8, 2, 9)
Read the lesson step by step
Start with data
Statistics starts with data. A bus was late on 8 days by 2, 4, 4, 4, 5, 5, 7 and 9 minutes. For now, treat these 8 days as the whole population: every value we care about.
>>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> len(x), min(x), max(x) (8, 2, 9)The mean: add, divide
The mean is the plain average: add every value, then divide by how many there are. The 8 values add to 40, and 40 ÷ 8 = 5. The population mean is written μ (mu): μ = 5 minutes.
>>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> sum(x), sum(x) / len(x) (40, 5.0) >>> from statistics import mean >>> mean(x) 5The median: the middle
The median is the middle value of the sorted data. With 8 values, average the middle two: (4 + 5) ÷ 2 = 4.5. Change the 9 to 49 and the mean jumps to 10, but the median stays 4.5.
>>> from statistics import mean, median >>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> median(x) 4.5 >>> y = x[:7] + [49] # one outlier >>> mean(y), median(y) (10, 4.5)Distance from the mean
Spread asks how far the values sit from the centre. Subtract μ = 5 from each value: −3, −1, −1, −1, 0, 0, 2, 4. These deviations always add up to 0, because the mean is the balance point.
>>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> d = [v - 5 for v in x] >>> d [-3, -1, -1, -1, 0, 0, 2, 4] >>> sum(d) 0Square the deviations
Square each deviation, so minus signs cannot cancel and far values count more: 9, 1, 1, 1, 0, 0, 4, 16. Their sum, 32, is the total squared distance from the mean.
>>> d = [-3, -1, -1, -1, 0, 0, 2, 4] >>> [v**2 for v in d] [9, 1, 1, 1, 0, 0, 4, 16] >>> sum(v**2 for v in d) 32Variance and SD
Divide by n = 8 for the average squared distance, the variance: σ² = 32 ÷ 8 = 4. Its square root is the standard deviation, σ = 2, back in minutes. A typical day is about 2 minutes from the mean.
>>> from statistics import pvariance, pstdev >>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> pvariance(x), pstdev(x) (4, 2.0)Take a random sample
Usually we cannot measure the whole population, so we measure a sample. Python's random.choices picks 4 days at random, with replacement: 4, 7, 7, 4. The sample mean x̄ (x-bar) = 22 ÷ 4 = 5.5 estimates μ = 5.
>>> import random >>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> random.seed(1) >>> s = random.choices(x, k=4) >>> s [4, 7, 7, 4] >>> sum(s) / len(s) 5.5Divide by n − 1
To estimate σ² from a sample, divide by n − 1. Squared distances from x̄ = 5.5 add to 9, so s² = 9 ÷ 3 = 3. Over all 4,096 possible samples, ÷ 3 averages exactly σ² = 4; ÷ 4 averages only 3.
>>> from statistics import variance, pvariance >>> s = [4, 7, 7, 4] >>> variance(s), pvariance(s) # ÷ (n-1), ÷ n (3, 2.25) >>> from itertools import product >>> from statistics import mean >>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> mean(variance(t) for t in product(x, repeat=4)) 4.0Sample means vary
Every new sample gives a new mean: 5.5, then 5, 3.75, 4.5. Over all possible samples of 4, the means average μ = 5 with standard deviation σ ÷ √n = 2 ÷ 2 = 1, the standard error. Bigger samples shrink it.
>>> import random >>> from statistics import mean >>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> random.seed(1) >>> [mean(random.choices(x, k=4)) for _ in range(4)] [5.5, 5, 3.75, 4.5] >>> from itertools import product >>> from statistics import pstdev >>> m = [mean(t) for t in product(x, repeat=4)] >>> mean(m), pstdev(m) (5.0, 1.0)The statistics module
Python's statistics module does all of this. mean and median find the centre. pstdev divides by n, for a whole population: 2.0. stdev divides by n − 1, for a sample: about 2.14 here.
>>> from statistics import * >>> x = [2,4,4,4,5,5,7,9] >>> mean(x), median(x) (5, 4.5) >>> pstdev(x), stdev(x) (2.0, 2.138089935299395)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What are the mean and the median of 1, 2, 3, 10?
The mean is (1 + 2 + 3 + 10) ÷ 4 = 16 ÷ 4 = 4. With 4 sorted values the median is the average of the middle two: (2 + 3) ÷ 2 = 2.5. The 10 pulls the mean up, but not the median.
02The values 3, 5, 7 have mean 5. What is their population standard deviation (divide by n)?
The deviations are −2, 0 and 2; their squares add to 8. The variance is 8 ÷ 3 ≈ 2.67, and the standard deviation is √2.67 ≈ 1.63. In Python, statistics.pstdev([3, 5, 7]) gives 1.632…
03Why does the sample variance divide by n − 1 instead of n?
The sample mean is chosen from the same values, so the squared distances to it are a little too small. Dividing by n − 1 makes the estimate right on average (it is unbiased); in the lesson, ÷ 3 averages exactly σ² = 4 over all 4,096 samples, while ÷ 4 averages 3.
04A population has standard deviation σ = 2. How large is the standard error of the mean of a random sample of 16 values?
The standard error is σ ÷ √n = 2 ÷ √16 = 2 ÷ 4 = 0.5. Four times as many values as a sample of 4 halve the standard error.
05You measured 20 page load times from a much larger stream of visits. Which Python function estimates the spread of all visits?
The 20 times are a sample, so use stdev, which divides by n − 1. pstdev divides by n and is for data that is the whole population.
Glossary
- Population
- Every value you care about, such as the delay on every day. Usually too big to measure in full.
- Sample
- The values you actually measured, picked from the population, ideally at random.
- Mean
- The sum of the values divided by how many there are. Written μ (mu) for a population and x̄ (x-bar) for a sample.
- Median
- The middle value of the sorted data; with an even count, the average of the middle two. One extreme value cannot drag it.
- Deviation
- How far one value is from the mean: the value minus the mean. Deviations from the mean always add up to 0.
- Variance
- The average squared deviation: σ² divides the sum of squares by n for a population; s² divides by n − 1 for a sample.
- Standard deviation
- The square root of the variance, in the same units as the data: a typical distance from the mean.
- Standard error
- The standard deviation of a sample mean across repeated samples: σ ÷ √n for n independent values.
Notes and sources
- The variance is in squared units (minutes²), which is hard to picture. The standard deviation, its square root, is back in the units of the data, so it is the number people usually quote.
- Why n − 1: the sample values sit closer to their own mean x̄ than to the true mean μ, so squared distances from x̄ come out too small on average. Dividing by n − 1 instead of n exactly corrects this (Bessel’s correction).
- The figure draws with replacement (a day can be picked twice), so the draws are independent: that is the case in which s² averages exactly σ² and the sample mean has standard deviation σ ÷ √n.
- The standard error falls with the square root of the sample size: 4 times as many values halve it. With samples of 16 days it would be 2 ÷ √16 = 0.5 minutes.
- The median ignores how far the extreme values are, so one wild value cannot drag it. The mean uses every value, so it moves; that is why typical incomes are often reported as medians.
- Orloff & Bloom, MIT 18.05 Introduction to Probability and Statistics (MIT OpenCourseWare, Spring 2022)
- NIST/SEMATECH e-Handbook of Statistical Methods: Measures of location
- NIST/SEMATECH e-Handbook of Statistical Methods: Measures of scale
- Diez, Çetinkaya-Rundel & Barr, OpenIntro Statistics (free textbook), chapters 2 and 5
- Python library: statistics (mean, median, pvariance, pstdev, variance, stdev)
- Python library: random (seed, choices)
Quiz Unlocks after the last step.