ENع
←Statistics
Lesson 17 · Probability and statistics

Statistics: mean, spread and sampling

14 min10 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 17 · Interactive
01 / 10

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)
Loading figure…
or ← →
Read the lesson step by step
  1. 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)
  2. 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)
    5
  3. The 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)
  4. 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)
    0
  5. Square 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)
    32
  6. Variance 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)
  7. 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.5
  8. Divide 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.0
  9. Sample 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)
  10. 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)

Quiz Unlocks after the last step.