The centre
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.
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.
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.
The spread
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.
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.
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.
Sampling
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.
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.
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.
In code
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.
Try it in the Python prompt
;; step 1/10 · Start with data >>> x = [2, 4, 4, 4, 5, 5, 7, 9] >>> len(x), min(x), max(x) (8, 2, 9)
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).