## 7.2 Moving averages

The classical method of time series decomposition originated in the 1920s and was widely used until the 1950s. It still forms the basis of many time series decomposition methods, so it is important to understand how it works. The first step in a classical decomposition is to use a moving average method to estimate the trend-cycle, so we begin by discussing moving averages.

### Moving average smoothing

A moving average of order \(m\) can be written as
\[\begin{equation}
\hat{T}_{t} = \frac{1}{m} \sum_{j=-k}^k y_{t+j}, \tag{7.1}
\end{equation}\]
where \(m=2k+1\). That is, the estimate of the trend-cycle at time \(t\) is obtained by averaging values of the time series within \(k\) periods of \(t\). Observations that are nearby in time are also likely to be close in value. Therefore, the average eliminates some of the randomness in the data, leaving a smooth trend-cycle component. We call this an **\(m\)-MA**, meaning a moving average of order \(m\).

```
tsibbledata::global_economy %>%
filter(Country == "Australia") %>%
autoplot(Exports/100) +
xlab("Year") + ylab("% of GDP") +
ggtitle("Total Australian exports") +
scale_y_continuous(labels = scales::percent)
```

For example, consider Figure 7.4 which shows the volume of electricity sold to residential customers in South Australia each year from 1989 to 2008 (hot water sales have been excluded). The data are also shown in Table 7.1.

Country | Year | Exports | 5-MA |
---|---|---|---|

Australia | 1960 | 12.9945 | |

Australia | 1961 | 12.4031 | |

Australia | 1962 | 13.9430 | 13.4569 |

Australia | 1963 | 13.0059 | 13.5021 |

Australia | 1964 | 14.9382 | 13.6079 |

Australia | 1965 | 13.2202 | 13.3961 |

Australia | 1966 | 12.9324 | 13.2544 |

Australia | 1967 | 12.8837 | 12.6578 |

Australia | 1968 | 12.2977 | 12.6091 |

Australia | 1969 | 11.9549 | 12.5549 |

Australia | 1970 | 12.9770 | 12.5433 |

Australia | 1971 | 12.6613 | 12.9148 |

Australia | 1972 | 12.8258 | 13.1542 |

Australia | 1973 | 14.1550 | 13.4151 |

Australia | 1974 | 13.1520 | 13.5875 |

Australia | 1975 | 14.2815 | 13.8269 |

Australia | 1976 | 13.5231 | 13.7215 |

Australia | 1977 | 14.0229 | 13.9535 |

Australia | 1978 | 13.6282 | 14.3851 |

Australia | 1979 | 14.3120 | 14.6629 |

Australia | 1980 | 16.4395 | 14.5665 |

Australia | 1981 | 14.9121 | 14.5599 |

Australia | 1982 | 13.5410 | 14.4135 |

Australia | 1983 | 13.5947 | 14.1776 |

Australia | 1984 | 13.5802 | 14.1979 |

Australia | 1985 | 15.2600 | 14.5886 |

Australia | 1986 | 15.0137 | 15.0665 |

Australia | 1987 | 15.4943 | 15.3796 |

Australia | 1988 | 15.9841 | 15.3563 |

Australia | 1989 | 15.1459 | 15.5654 |

Australia | 1990 | 15.1436 | 15.8048 |

Australia | 1991 | 16.0590 | 16.1227 |

Australia | 1992 | 16.6914 | 16.6947 |

Australia | 1993 | 17.5737 | 17.2494 |

Australia | 1994 | 18.0059 | 17.8254 |

Australia | 1995 | 17.9169 | 18.3221 |

Australia | 1996 | 18.9392 | 18.7265 |

Australia | 1997 | 19.1748 | 18.7961 |

Australia | 1998 | 19.5956 | 19.1014 |

Australia | 1999 | 18.3540 | 19.7606 |

Australia | 2000 | 19.4435 | 20.0823 |

Australia | 2001 | 22.2352 | 19.9838 |

Australia | 2002 | 20.7831 | 19.7569 |

Australia | 2003 | 19.1030 | 19.5253 |

Australia | 2004 | 17.2198 | 19.0599 |

Australia | 2005 | 18.2853 | 18.9494 |

Australia | 2006 | 19.9081 | 19.1665 |

Australia | 2007 | 20.2306 | 20.3302 |

Australia | 2008 | 20.1887 | 20.6417 |

Australia | 2009 | 23.0385 | 20.9546 |

Australia | 2010 | 19.8425 | 21.2123 |

Australia | 2011 | 21.4728 | 21.1721 |

Australia | 2012 | 21.5190 | 20.7796 |

Australia | 2013 | 19.9877 | 20.8137 |

Australia | 2014 | 21.0758 | 20.3697 |

Australia | 2015 | 20.0130 | 20.3200 |

Australia | 2016 | 19.2530 | |

Australia | 2017 | 21.2704 |

In the last column of this table, a moving average of order 5 is shown, providing an estimate of the trend-cycle. The first value in this column is the average of the first five observations (1960–1964); the second value in the 5-MA column is the average of the values for 1961–1965; and so on. Each value in the 5-MA column is the average of the observations in the five year window centred on the corresponding year. In the notation of Equation (7.1), column 5-MA contains the values of \(\hat{T}_{t}\) with \(k=2\) and \(m=2k+1=5\). This is easily computed using

```
aus_exports <- tsibbledata::global_economy %>%
filter(Country == "Australia") %>%
transmute(
Exports = Exports/100,
`5-MA` = slide_dbl(Exports, mean, .size = 5, .align = "center")
)
```

There are no values for either the first two years or the last two years, because we do not have two observations on either side. Later we will use more sophisticated methods of trend-cycle estimation which do allow estimates near the endpoints.

To see what the trend-cycle estimate looks like, we plot it along with the original data in Figure 7.5.

```
aus_exports %>%
ggplot(aes(x = Year)) +
geom_line(aes(y = Exports, colour = "Data")) +
geom_line(aes(y = `5-MA`, colour = "5-MA")) +
xlab("Year") + ylab("Exports (% of GDP)") +
ggtitle("Total Australian exports") +
scale_y_continuous(labels = scales::percent) +
scale_colour_manual(values=c("Data"="grey50","5-MA"="red"),
breaks=c("Data","5-MA"))
```

Notice that the trend-cycle (in red) is smoother than the original data and captures the main movement of the time series without all of the minor fluctuations. The order of the moving average determines the smoothness of the trend-cycle estimate. In general, a larger order means a smoother curve. Figure 7.6 shows the effect of changing the order of the moving average for the Australian exports data.

Simple moving averages such as these are usually of an odd order (e.g., 3, 5, 7, etc.). This is so they are symmetric: in a moving average of order \(m=2k+1\), the middle observation, and \(k\) observations on either side, are averaged. But if \(m\) was even, it would no longer be symmetric.

### Moving averages of moving averages

It is possible to apply a moving average to a moving average. One reason for doing this is to make an even-order moving average symmetric.

For example, we might take a moving average of order 4, and then apply another moving average of order 2 to the results. In the following table, this has been done for the first few years of the Australian quarterly beer production data.

```
beer <- aus_production %>%
filter(year(Quarter) >= 1992) %>%
select(Quarter, Beer)
beer_ma <- beer %>%
mutate(
`4-MA` = slide_dbl(Beer, mean, .size = 4, .align = "cl"),
`2x4-MA` = slide_dbl(`4-MA`, mean, .size = 2, .align = "cl")
)
```

Quarter | Beer | 4-MA | 2x4-MA |
---|---|---|---|

1992 Q1 | 443.00 | NA | NA |

1992 Q2 | 410.00 | 451.25 | 450.00 |

1992 Q3 | 420.00 | 448.75 | 450.12 |

1992 Q4 | 532.00 | 451.50 | 450.25 |

1993 Q1 | 433.00 | 449.00 | 446.50 |

1993 Q2 | 421.00 | 444.00 | 446.00 |

1993 Q3 | 410.00 | 448.00 | 443.00 |

1993 Q4 | 512.00 | 438.00 | 439.62 |

1994 Q1 | 449.00 | 441.25 | 443.62 |

1994 Q2 | 381.00 | 446.00 | 443.12 |

1994 Q3 | 423.00 | 440.25 | 443.62 |

1994 Q4 | 531.00 | 447.00 | 446.12 |

1995 Q1 | 426.00 | 445.25 | 443.88 |

1995 Q2 | 408.00 | 442.50 | 440.38 |

1995 Q3 | 416.00 | 438.25 | 437.00 |

1995 Q4 | 520.00 | 435.75 | 433.50 |

1996 Q1 | 409.00 | 431.25 | 429.62 |

1996 Q2 | 398.00 | 428.00 | 430.88 |

1996 Q3 | 398.00 | 433.75 | 433.75 |

1996 Q4 | 507.00 | 433.75 | 434.75 |

The notation “\(2\times4\)-MA” in the last column means a 4-MA followed by a 2-MA. The values in the last column are obtained by taking a moving average of order 2 of the values in the previous column. For example, the first two values in the 4-MA column are 451.25=(443+410+420+532)/4 and 448.75=(410+420+532+433)/4. The first value in the 2x4-MA column is the average of these two: 450.12=(451.25+448.75)/2.

When a 2-MA follows a moving average of an even order (such as 4), it is called a “centred moving average of order 4”. This is because the results are now symmetric. To see that this is the case, we can write the \(2\times4\)-MA as follows: \[\begin{align*} \hat{T}_{t} &= \frac{1}{2}\Big[ \frac{1}{4} (y_{t-2}+y_{t-1}+y_{t}+y_{t+1}) + \frac{1}{4} (y_{t-1}+y_{t}+y_{t+1}+y_{t+2})\Big] \\ &= \frac{1}{8}y_{t-2}+\frac14y_{t-1} + \frac14y_{t}+\frac14y_{t+1}+\frac18y_{t+2}. \end{align*}\] It is now a weighted average of observations that is symmetric.

Other combinations of moving averages are also possible. For example, a \(3\times3\)-MA is often used, and consists of a moving average of order 3 followed by another moving average of order 3. In general, an even order MA should be followed by an even order MA to make it symmetric. Similarly, an odd order MA should be followed by an odd order MA.

### Estimating the trend-cycle with seasonal data

The most common use of centred moving averages is for estimating the trend-cycle from seasonal data. Consider the \(2\times4\)-MA: \[ \hat{T}_{t} = \frac{1}{8}y_{t-2} + \frac14y_{t-1} + \frac14y_{t} + \frac14y_{t+1} + \frac18y_{t+2}. \] When applied to quarterly data, each quarter of the year is given equal weight as the first and last terms apply to the same quarter in consecutive years. Consequently, the seasonal variation will be averaged out and the resulting values of \(\hat{T}_t\) will have little or no seasonal variation remaining. A similar effect would be obtained using a \(2\times 8\)-MA or a \(2\times 12\)-MA to quarterly data.

In general, a \(2\times m\)-MA is equivalent to a weighted moving average of order \(m+1\) where all observations take the weight \(1/m\), except for the first and last terms which take weights \(1/(2m)\). So, if the seasonal period is even and of order \(m\), we use a \(2\times m\)-MA to estimate the trend-cycle. If the seasonal period is odd and of order \(m\), we use a \(m\)-MA to estimate the trend-cycle. For example, a \(2\times 12\)-MA can be used to estimate the trend-cycle of monthly data and a 7-MA can be used to estimate the trend-cycle of daily data with a weekly seasonality.

Other choices for the order of the MA will usually result in trend-cycle estimates being contaminated by the seasonality in the data.

### Example: Electrical equipment manufacturing

```
vic_print_media <- tsibbledata::aus_retail %>%
filter(Industry == "Newspaper and book retailing", State == "Victoria")
vic_print_ma <- vic_print_media %>%
mutate(
`12-MA` = slide_dbl(Turnover, mean, .size = 12, .align = "cl"),
`2x12-MA` = slide_dbl(`12-MA`, mean, .size = 2, .align = "cl")
)
vic_print_ma %>%
ggplot(aes(x = Month)) +
geom_line(aes(y = Turnover, colour = "Data")) +
geom_line(aes(y = `2x12-MA`, colour = "2x12-MA")) +
xlab("Year") + ylab("Turnover (million $AUD)") +
ggtitle("Newspaper and book retail turnover (Victoria, Australia)") +
scale_colour_manual(values=c("Data"="grey","2x12-MA"="red"),
breaks=c("Data","2x12-MA"))
```

Figure 7.7 shows a \(2\times12\)-MA applied to the electrical equipment orders index. Notice that the smooth line shows no seasonality; it is almost the same as the trend-cycle shown in Figure 7.1, which was estimated using a much more sophisticated method than a moving average. Any other choice for the order of the moving average (except for 24, 36, etc.) would have resulted in a smooth line that showed some seasonal fluctuations.

### Weighted moving averages

Combinations of moving averages result in weighted moving averages. For example, the \(2\times4\)-MA discussed above is equivalent to a weighted 5-MA with weights given by \(\left[\frac{1}{8},\frac{1}{4},\frac{1}{4},\frac{1}{4},\frac{1}{8}\right]\). In general, a weighted \(m\)-MA can be written as \[ \hat{T}_t = \sum_{j=-k}^k a_j y_{t+j}, \] where \(k=(m-1)/2\), and the weights are given by \(\left[a_{-k},\dots,a_k\right]\). It is important that the weights all sum to one and that they are symmetric so that \(a_j = a_{-j}\). The simple \(m\)-MA is a special case where all of the weights are equal to \(1/m\).

A major advantage of weighted moving averages is that they yield a smoother estimate of the trend-cycle. Instead of observations entering and leaving the calculation at full weight, their weights slowly increase and then slowly decrease, resulting in a smoother curve.