School/□ Systems Engineer/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?
About 40 min · 3 goals · Checked by automated tests
1.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.
2.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'.
Loading the interactive…
Can't use the interactive? Read a description instead
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.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Your benchmark times have a long right tail from occasional background tasks. Which centre is least affected by those rare slow runs?
4.Predict
One run in twenty jumps from about 48 ms to 110 ms. Which summary moves most?
Guess first. Being wrong here is how the surprise works.
5.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
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.
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.
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 σ².
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.
6.Watch
→ 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.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
Five timing runs (ms): 33, 28, 28, 33, 34. What is the sample standard deviation s (divide by n − 1)? (3 decimal places.)
Practice 3 · a fresh version every time
Past runs of a benchmark have mean 67 ms and standard deviation 4 ms. A new run takes 74.6 ms. What is its z-score? (2 decimal places; negative if it is below the mean.)
Practice 4
A run is flagged with z = 4.2. What is the most defensible action?
Practice 5
Why does the sample standard deviation divide by n − 1?
8.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.
9.Reflect
Concepts: Describing data · Measurement error and uncertainty