Smoothing with moving medians
See how moving medians calm a jumpy time series plot in Year 12 VCE General Mathematics. Weekly visitor numbers at a Ballarat attraction can leap about, and one huge day, such as a festival or a voucher sale, can drag an average a long way. Picking the middle value of each group of neighbouring points avoids that problem.
You will practise 3-median and 5-median smoothing straight from a graph: choosing the middle point of each group, plotting it at the middle period and joining the smoothed points to reveal the trend. You will compare medians with means when an outlier is present, before measuring seasonal patterns with seasonal indices.
Theory
Moving medians smooth a time series plot by picking the middle of each group of neighbouring points. This Year 12 General Mathematics guide for students in Victoria shows how to do 3-median and 5-median smoothing straight from a plot, why medians are not thrown off by outliers, and how the smoothed series reveals the trend.
When a time series plot has large ups and downs, the long-term trend can be hard to see. Moving median smoothing replaces each point with the middle value of that point and its neighbours, and it can be done straight from the plot, without any calculation.
In 3-median smoothing, look at a point and the point on each side; the smoothed value is the one in the middle when the three are put in size order (on the plot, the one in the middle vertically). 5-median smoothing uses two points on each side and takes the third value in size order. Moving median smoothing always uses an odd number of points, so each group has a middle time period where the smoothed value is placed.
A median depends only on which value is in the middle, not on how extreme the others are, so a one-off outlier has little or no effect on median smoothing. The first and last periods have no smoothed value, because a full group does not fit there.
For a \(k\)-median, with \(k\) odd, the smoothed value at period \(t\) is
For a 3-median it is the 2nd of the three in size order; for a 5-median, the 3rd of the five. With \(n\) points in the series:
| Smoothing | Points each side | No smoothed value for | Example: \(n = 12\) |
|---|---|---|---|
| 3-median | \(1\) | first \(1\) and last \(1\) period | \(10\) values (periods \(2\) to \(11\)) |
| 5-median | \(2\) | first \(2\) and last \(2\) periods | \(8\) values (periods \(3\) to \(10\)) |
3-median smoothing from a plot
- Start at the second point. Look at it and the points on either side.
- Choose the middle one vertically (neither the highest nor the lowest). Read its value from the grid.
- Mark the smoothed value at the middle period, at that height.
- Move along one point and repeat, up to the second-last point. Join the smoothed points in order.
5-median smoothing
- Start at the third point; use it and two points on each side.
- The smoothed value is the third of the five in size order, placed at the middle period.
- Describe the trend the smoothed series shows. Use median smoothing when the data contain an outlier, and more points for a smoother result.
Take the day and the days either side, and choose the middle value in size order.
| \(\text{period 3 and neighbours}\) | \(:\) | \(12,\ 9,\ 14\) |
| \(\text{in size order}\) | \(:\) | \(9,\ 12,\ 14\) |
| \(\text{3-median (period 3)}\) | \(=\) | \(12\) |
| \(\text{period 5 and neighbours}\) | \(:\) | \(14,\ 11,\ 11\) |
| \(\text{in size order}\) | \(:\) | \(11,\ 11,\ 14\) |
| \(\text{3-median (period 5)}\) | \(=\) | \(11\) |
(i) \(12\). (ii) Two of the points are level at \(11\), so the middle value is \(11\). (iii) Day \(1\) has no point before it, so there is no group of three centred on it.
Work along the plot from month \(2\) to month \(8\), each time choosing the middle of three points.
| \(\text{month 2}\) | \(:\) | \(44,\ 36,\ 42 \to 42\) |
| \(\text{month 3}\) | \(:\) | \(36,\ 42,\ 40 \to 40\) |
| \(\text{month 4}\) | \(:\) | \(42,\ 40,\ 34 \to 40\) |
| \(\text{month 5}\) | \(:\) | \(40,\ 34,\ 38 \to 38\) |
| \(\text{month 6}\) | \(:\) | \(34,\ 38,\ 30 \to 34\) |
| \(\text{month 7}\) | \(:\) | \(38,\ 30,\ 34 \to 34\) |
| \(\text{month 8}\) | \(:\) | \(30,\ 34,\ 28 \to 30\) |
The smoothed values are \(42,\ 40,\ 40,\ 38,\ 34,\ 34,\ 30\). They never rise, while the original data zig-zag, so the smoothed series shows a clear decreasing trend.
Use the week and two weeks on each side, and take the third value in size order.
| \(\text{period 3 and neighbours}\) | \(:\) | \(50,\ 65,\ 55,\ 70,\ 45\) |
| \(\text{in size order}\) | \(:\) | \(45,\ 50,\ 55,\ 65,\ 70\) |
| \(\text{5-median (period 3)}\) | \(=\) | \(55\) |
| \(\text{period 8 and neighbours}\) | \(:\) | \(60,\ 75,\ 65,\ 80,\ 70\) |
| \(\text{in size order}\) | \(:\) | \(60,\ 65,\ 70,\ 75,\ 80\) |
| \(\text{5-median (period 8)}\) | \(=\) | \(70\) |
| \(\text{number}\) | \(=\) | \(11 - 5 + 1\) |
| \(\text{number}\) | \(=\) | \(7\) |
(i) \(55\) tens. (ii) \(70\) tens. (iii) \(7\) values, for weeks \(3\) to \(9\): weeks \(1\), \(2\), \(10\) and \(11\) do not have two neighbours on each side.
| Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Bookings | 27 | 29 | 26 | 71 | 30 | 28 | 31 |
Both use days \(3\), \(4\) and \(5\).
| \(\text{period 4 and neighbours}\) | \(:\) | \(26,\ 71,\ 30\) |
| \(\text{in size order}\) | \(:\) | \(26,\ 30,\ 71\) |
| \(\text{3-median (period 4)}\) | \(=\) | \(30\) |
| \(\text{3-mean (day 4)}\) | \(=\) | \(\dfrac{26 + 71 + 30}{3}\) |
| \(\text{3-mean (day 4)}\) | \(=\) | \(\dfrac{127}{3}\) |
| \(\text{3-mean (day 4)}\) | \(\approx\) | \(42.3\) |
(i) \(30\). (ii) \(42.3\). (iii) The other days have \(26\) to \(31\) bookings. The outlier (\(71\)) pulls the mean up, but the median ignores how extreme it is, so the 3-median (\(30\)) better shows the usual level.
Common pitfalls
Frequently asked questions
How do you do moving median smoothing on a graph?
For each point, look at it and its neighbours (one on each side for a 3-median, two on each side for a 5-median). Pick the one in the middle vertically and mark that value at the middle time period. Move along one point and repeat, then join the smoothed points.
Why does moving median smoothing use an odd number of points?
With an odd number of points there is a middle time period, so the median can be placed exactly on one of the original periods and read straight from the graph.
When is median smoothing better than mean smoothing?
When the data contain an outlier. A mean is pulled towards an extreme value, but a median is only the middle value, so a single one-off spike or dip has little or no effect on it.
Is 5-median smoothing smoother than 3-median smoothing?
Usually yes. Each smoothed value depends on more neighbouring points, so single high or low points have less effect. The cost is fewer smoothed values at each end.
How many smoothed values do I get with median smoothing?
With n points and k-median smoothing you get n - k + 1 values. For 9 points, 3-median smoothing gives 7 values and 5-median smoothing gives 5.