Skip to content
Tactiverge

School/○ Innovator/Week 8

Mean, median and spread: summarising a log honestly

A range sensor logs 9 readings near 48 cm and one glitch at 400 cm. Which single number should go in the report?

About 35 min · 3 goals · Checked by automated tests

1.Why should I care?

Before you plot a log you usually summarise it. The mean, median and standard deviation each tell you something different, and one glitch can wreck the wrong choice. An honest summary gives a centre, a spread and n, and says how outliers were handled.

  • ◇ Compute the mean, median and sample standard deviation of a data set by hand and with the statistics module.
  • ◇ Explain how an outlier affects the mean, the median and the standard deviation.
  • ◇ Choose and report a summary with its spread and the number of readings.

2.Predict

Readings: nine values near 48 cm and one glitch of 400 cm. Which summary is barely affected by the glitch?

Guess first. Being wrong here is how the surprise works.

3.Discover

In the Code Lab, compute statistics.mean, statistics.median and statistics.stdev for the worked example's readings, with and without the 400 cm glitch. Then replace the glitch with 49 and recompute. Which summary changed most each time?

Can't use the interactive? Read a description instead

The Code Lab activity can be done from the worked numbers: nine clean readings have mean 48.09 cm, median 48.1 cm and sample standard deviation 0.29 cm. Adding a 400 cm glitch makes the mean 83.28 cm and the standard deviation 111.28 cm, while the median only moves to 48.15 cm.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A log of battery current (A) reads xs = [20, 26, 26, 17, 21, 23, 19, 26]. What is the median, statistics.median(xs)? Give 2 decimal places.

5.Understand

Centre: mean or median?

The mean uses every value, so it is sensitive to outliers. The median is the middle value of the sorted data (or the average of the two middle values), so it resists spikes. When they differ a lot, look at the data: something is skewing it.

Spread: standard deviation

The standard deviation is a typical distance of readings from the mean. Roughly two-thirds of normally distributed readings lie within one standard deviation of the mean. Report it with the mean, and say whether it is the sample (n − 1) or population (n) version.

Outliers: decide, then disclose

Never silently delete inconvenient readings. Decide a rule before looking at the results (for example, reject readings outside the sensor's physical range), apply it, and report how many were removed and why.

Explain it another way

If one billionaire walks into a café, the mean wealth of the customers rockets; the median barely moves. Sensor glitches are the billionaires of your log.

Derivation: Sample standard deviation, step by step
  1. Mean: x̄ = (x₁ + x₂ + … + xₙ)/n.

  2. Deviations: dᵢ = xᵢ − x̄. They always add up to zero, so we square them to measure their size.

  3. Sum of squares: S = Σ dᵢ².

  4. Sample variance: s² = S/(n − 1). Dividing by n − 1 rather than n corrects for using x̄, which is itself estimated from the same data and sits closer to the readings than the true mean does.

  5. Sample standard deviation: s = √(S/(n − 1)), in the same units as the readings. Python's statistics.stdev computes exactly this; statistics.pstdev divides by n.

Worked example 1

Summarise a range log with and without a glitch.

import statistics as st

clean = [48.2, 47.9, 48.5, 48.1, 47.6, 48.3, 48.0, 48.4, 47.8]
with_glitch = clean + [400.0]

for name, xs in [("clean", clean), ("with glitch", with_glitch)]:
    print(name, len(xs), round(st.mean(xs), 2), round(st.median(xs), 2), round(st.stdev(xs), 2))
# clean 9 48.09 48.1 0.29
# with glitch 10 83.28 48.15 111.28

valid = [x for x in with_glitch if 2 <= x <= 300]      # sensor's rated range: 2 to 300 cm
print(len(with_glitch) - len(valid), "reading(s) outside the rated range removed")

Common mix-ups

  • The mean is always the best summary.
    ✓ With outliers or skewed data, the median often represents a typical reading better. Report both if they differ.
  • Removing outliers is cheating.
    ✓ Removing them by a rule decided in advance and disclosed is good practice. Removing them silently, or because they're inconvenient, is not.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A log of motor temperature (°C) reads xs = [68, 68, 76, 77, 66]. What is the sample standard deviation, statistics.stdev(xs)? Give 2 decimal places.

Practice 3 · a fresh version every time

A log of ultrasonic range (cm) reads xs = [49, 49, 47, 44, 42, 51]. What is the sample standard deviation, statistics.stdev(xs)? Give 2 decimal places.

Practice 4

Which report line is most honest and useful?

Practice 5

Why does statistics.stdev divide by n − 1?

7.Apply it

Write summarise(readings, low, high) that applies a rated-range rule, then returns n kept, n removed, mean, median and sample standard deviation. Test it on a list with no outliers, one with two glitches and an empty list (which should raise a clear error).

8.Reflect

9.Go further

Concepts: Describing data · Measurement error and uncertainty · Honest data visualisation