Lite / Engineering Complex Systems / Week 28
Distributions, spread and the one weird run
Nineteen runs take about 48 ms. One takes 110 ms. Is your code slow, or is your summary lying to you?
Why should I care?
Every optimisation claim in this term rests on numbers that wobble. Before you can say a design is better, you need to say what 'typical' and 'how much it varies' mean for your data, and notice when one strange point is steering the whole story.
- Describe a distribution by its centre, spread and shape, and choose between mean and median.
- Compute a sample variance and standard deviation and explain the n − 1.
- Flag a possible outlier with a z-score and decide, transparently, what to do with it.
Predict
One run in twenty jumps from about 48 ms to 110 ms. Which summary moves most?
- The median
- The mean
- Both move by the same amount
- Neither: one point out of twenty can't matter
Guess first, then reveal
The mean jumps by about (110 − 48)/20 ≈ 3 ms; the median barely moves, because it only cares about the middle of the ordered list. The standard deviation balloons too, since the outlier's squared deviation dominates the sum.
Discover
Drag the orange run from 49 ms to 120 ms. Watch the mean line, the median line and the SD. Find the value where the run first gets flagged (|z| > 3), then compare 'Keep all' with 'Exclude and report'.
What the interactive shows (text version)
The interactive shows 19 run times clustered around 48 ms with an SD near 3 ms, plus one run you move from 40 to 120 ms. Moving it to 110 ms raises the mean by about 3 ms, raises the SD several-fold, leaves the median almost unchanged, and gives a z-score far above 3, so the run is flagged. Choosing 'Exclude and report' recomputes the summaries without it and reminds you to report the exclusion.
Watch (optional)
Watch for: Watch for why the variance squares each deviation, and what the interquartile range offers when data have long tails.
Range, variance and standard deviation, so that 'mean ± SD' in a results table means something to you.
“Measures of Spread: Crash Course Statistics #4” by CrashCourse (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
Centre: mean or median?
The mean uses every value, so it is efficient for well-behaved data and sensitive to extremes. The median is the middle of the sorted list: robust to a few wild values, but it throws information away. For timings, which often have a long right tail (occasional slow runs from caches, interrupts or garbage collection), report both, or report the median with the interquartile range.
Spread: variance and standard deviation
Sample variance s² = Σ(xᵢ − x̄)² / (n − 1); the standard deviation s is its square root and has the data's units. Dividing by n − 1 rather than n corrects a bias: deviations are measured from x̄, which was fitted to the same data, so they come out slightly too small. With n = 5 the correction is 25%; with n = 100 it is 1%.
Shape matters
Two data sets can share a mean and SD yet look completely different: one symmetric bell, one skewed with a long tail, one with two humps (perhaps two machines mixed together). Always plot the data (dot plot or histogram) before trusting a summary. A bimodal timing histogram usually means two different code paths, not noise.
Outliers: flag, investigate, report
A z-score z = (x − x̄)/s counts standard deviations from the mean; |z| > 3 is a common flag for roughly normal data. A flag is a question, not a verdict. Was the laptop throttling? Did a backup start? If you exclude a point, say so, give the rule you set before looking, and show the result both ways. Silently deleting inconvenient data is falsification.
Explain it another way
Intuitive
The mean is a see-saw's balance point: one heavy child far out tips it a long way. The median is the person in the middle of a queue: one late arrival at the back doesn't change who is in the middle.
Technical
Mean x̄ = Σxᵢ/n; median = middle order statistic; s = √(Σ(xᵢ − x̄)²/(n − 1)). The mean's breakdown point is 0 (one value can move it arbitrarily); the median's is 50%. Robust spread: IQR = Q3 − Q1, or the median absolute deviation.
Mathematical
Why n − 1: E[Σ(xᵢ − x̄)²] = (n − 1)σ², because Σ(xᵢ − x̄)² = Σ(xᵢ − μ)² − n(x̄ − μ)² and E[(x̄ − μ)²] = σ²/n. So dividing by n − 1 gives an unbiased estimate of σ².
Applied
Performance engineers often quote p50 (median) and p99 latency, not the mean: users feel the slow tail. Web services are tuned to cut the 99th percentile, because one slow request in a hundred is a lot of unhappy people per second.
Worked example 1
Runs (ms): 47, 49, 48, 50, 46. Find the mean and sample SD.
Mean = 240/5 = 48. Deviations −1, 1, 0, 2, −2; squares 1, 1, 0, 4, 4; sum 10. s² = 10/4 = 2.5, so s ≈ 1.58 ms.
Worked example 2
Add a sixth run of 70 ms. What happens to the mean, the median and the z-score of 70 measured against the first five?
Mean = 310/6 ≈ 51.7 ms (up 3.7). Median of 46, 47, 48, 49, 50, 70 = (48 + 49)/2 = 48.5 (up 0.5). z = (70 − 48)/1.58 ≈ 13.9: definitely flag it and find out what happened.
Common mix-ups
The mean is always the best summary.
✓ For skewed data or data with rare extremes the median (with IQR) often represents a typical value better; report what fits the question.An outlier is an error and should be removed.
✓ An outlier is a flag to investigate. Remove only with a pre-stated rule, and report it.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Time any small program of yours 20 times. Plot a dot plot, compute mean, median and SD, and flag anything with |z| > 3. Write two sentences on what caused the spread and which summary you would quote.
Reflect
Think of a number you have seen quoted as an average (a commute, a battery life, a download speed). Would the median tell a different story?
Write your answer on paper or in your own notes. Lite never stores what you write.