TactivergeLite

Lite / Intelligent Robotic Systems / 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 · Full interactive version

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.

Predict

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

  1. The mean
  2. The median
  3. The standard deviation
  4. The maximum
Guess first, then reveal

The median: it only cares about the middle of the sorted list, so one extreme value moves it hardly at all. The mean jumps by about 35 cm and the standard deviation explodes.

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?

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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

Intuitive

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.

Technical

statistics.mean, statistics.median and statistics.stdev (sample) or statistics.pstdev (population). The median needs a sort, so it is O(n log n) with a simple implementation; mean and standard deviation are O(n).

Mathematical

For [2, 4, 4, 4, 5, 5, 7, 9]: mean 5, deviations sum of squares 32, sample variance 32/7, s ≈ 2.138; population σ = 2.

Applied

Robot sensor pipelines often use a median filter over the last few readings to reject single-sample spikes before averaging, exactly because the median ignores isolated outliers.

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 4. Which report line is most honest and useful?
Practice 5. Why does statistics.stdev divide by n − 1?

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).

Reflect

When would you report the median instead of the mean in your own projects?

Write your answer on paper or in your own notes. Lite never stores what you write.

Go further

← Charts that lie without a single false numberProject: plan a path that counts the cost →