The Moving Average as a Filter

A daily series, an average over M consecutive days, and what that average does to every cycle the series could hold. Try the real reports first, then change M by one day and watch the weekly pattern come back.

Loading interactive simulation...

Lesson

The theory โ€” The Moving Average as a Filter

An M-point moving average replaces each value of a series by the mean of M consecutive values. It is a convolution with a box of M weights 1/M, so by the convolution theorem its effect on a cycle of f cycles per sample is a multiplication by the box's Fourier transform, the gain H(f) = sin(ฯ€fM)/(M sin ฯ€f). H is zero at f = 1/M, 2/M, โ€ฆ, negative between the first and second zero, and it is real when each average is placed at the middle of its window; placed at the window's end, the same values carry a delay of (M โˆ’ 1)/2 samples.

What each symbol means

M
the number of days in each average
f
the frequency of a cycle, in cycles per day, from 0 to ยฝ
H(f)
the gain: what the average multiplies a cycle of frequency f by; negative means inverted
Assumes
One value per day with no gaps, so that the window always spans M calendar days. The cancellation needs the reported values themselves to repeat every 7 days. A reporting delay that is always the same keeps them weekly and changes nothing; a missing day, a holiday, or a delay that varies from week to week breaks it.

A 7-day average deletes the weekly cycle, whatever its shape 🖖

Press Germany, 7 days. These reports sag at the start of each week and pile up later: Mondays average 60.1% of the week around them, Thursdays 130.7%. In the 7-day average the weekday swing is 0.8 points. Every window of seven consecutive days holds exactly one Monday, one Tuesday and so on to Sunday, so any pattern that repeats weekly adds the same amount to every window, whatever shape it has. On the lower chart, the gain sin(7ฯ€f)/(7 sin ฯ€f) is exactly zero at 1/7, 2/7 and 3/7 cycles a day, the weekly cycle and the only two harmonics a series of daily values can carry. That 0.8 is not zero because the real pattern does not repeat exactly; press The weekly pattern alone and it is 0.0. Holidays break the repeat outright. Christmas Day reads 0 and the next day 39,988, the most of any day in the series, so on 22 December the centred average drops to 21,025, its window ending on the zero, and a day later, with the 39,988 inside, it is back at 22,266. Now change M by one day. Eight days hold one weekday twice and keep an eighth of the swing, 8.8 points of 70.7 in the synthetic week; six days leave one weekday out and keep a sixth, 11.8. Fourteen and twenty-one delete it again. A trailing average reports the same numbers on a later day, so it deletes the weekly cycle just as well.

Some waves come out of the average upside down 🖖

Press A 5-day wave: a cosine with a period of 5 days, through the same 7-day average. Smoothing is supposed to shrink a fast wiggle, and the dashed ghost is that expectation, the wave smaller but upright. The average comes out at โˆ’0.2311 of the wave: smaller, and inverted, with its peaks on the days the input has troughs. The reason fits in a window. Any five consecutive days hold one whole cycle, which sums to zero, so a 7-day window is left with its two end days, 3 days either side of the middle. Three days is six tenths of a cycle, and the two days that far either side of any point add up to 2 cos(6ฯ€/5) = โˆ’1.618 times the wave's value there. Divided by 7, that is โˆ’0.2311. On the lower chart the gain is negative for every period between 3.5 and 7 days, most negative at โˆ’0.2330 near 4.86 days, positive again between 2.33 and 3.5. Press Day on, day off: the fastest wiggle a daily series can hold gets through at โˆ’0.1429, a seventh of its size. Steven W. Smith's DSP textbook calls the moving average "an exceptionally good smoothing filter" and "an exceptionally bad low-pass filter" in one sentence, and these lobes are why.

A trailing average is the centred one, three days late 🖖

Press Trailing, as dashboards do. A dashboard cannot average days that have not happened, so it puts each 7-day average on the last day of its window. That is the same seven numbers and the same mean, reported 3 days after the window's middle; the dashed line is the centred average, and the solid line is that curve slid right. The highest 7-day average in these reports, 25,851 a day, belongs to the week of 18 to 24 December. Centred, it sits on 21 December. Trailing, it sits on the 24th. Neither placement is free. The centred average is on time and stops 3 days before the data does, because its last windows are not complete yet. The trailing average reaches today and describes a week whose middle was 3 days ago; on a straight-line trend it is behind by exactly 3 days of change. The gain is untouched, so the weekly cycle still vanishes: only the timing moves, by the same 3 days at every frequency. For an M-day window the delay is (M โˆ’ 1)/2 days, so a 14-day trailing average is 6.5 days behind.

Problem solved in full

  1. Average the average 4 steps

    A 7-day average turns a 5-day wiggle upside down. A colleague suggests taking the 7-day average of the 7-day average. What happens to the 5-day wave now, to the weekly cycle, and what does the second pass cost? Press A 5-day wave first: the page gives the gain of one pass.

    Open this problem in the tool

    1. One pass, from the page:

    2. A second pass multiplies the wave by the same gain again, and a negative times a negative is positive:

    3. The two passes together are one average over 13 days with triangular weights, the box convolved with itself:

    4. Its half-power point moves out, from Hยฒ = ยฝ for one pass to Hโด = ยฝ for two:

    Answer

    The 5-day wave comes out upright, at 0.0534 of its size, and the weekly cycle is still deleted, since 0 ร— 0 = 0. The price is a smoother that also softens slower cycles: the period whose power is halved moves from 15.66 days to 21.74, and a centred version needs 6 days on each side instead of 3, so it stops 6 days before the data does. Hyndman and Athanasopoulos call this kind of thing a moving average of a moving average, a 3ร—3-MA in their example; here it is a 7ร—7-MA.

Learning path

The Fourier transform in practice

References (3)