Time Series: Decomposition (Part I) — Trend Cycle Computation
We have already discussed trend, seasonality and cyclical patterns in a previous blog. Any time series can be decomposed into 3 components: trend-cycle, seasonality and residuals. Since it is difficult to estimate trend and cycle components separately, we combine trend and cycle into one trend-cycle component.
We extract components from a time series data so that we can understand the underlying patterns better and also the components can be used to improve the forecast.
There are two ways in which we can decompose the time series:
- Multiplicative : y(t) = T(t) * S(t) * R(t)
- Additive : y(t) = T(t) + S(t) + R(t)
where y(t) is the raw series, T(t) is the trend-cycle component at time t, S(t) is the seasonality component at time t and R(t) is the residual component at time t.
Additive decomposition is generally used when the seasonal variation is independent of the trend, whereas, the multiplicative component is used when the seasonal variation is proportional to the trend.
Let’s try to visualize scenarios where to use multiplicative vs additive decomposition.
Scenario 1: Multiplicative Decomposition
In the above plot (A10 dataset, Rob J Hyndman), we can see that seasonal variations keep on increasing proportionally to the trend. In scenarios like this, we prefer multiplicative decomposition.
Scenario 2: Additive Decomposition
In the above plot, we can see that the seasonal variations remain immune to the trend and are almost same throughout, in such situations additive decomposition is recommended.
Now, we know the difference between additive and multiplicative decomposition let’s try to find the first component, the Trend cycle component. The algorithm to compute the Trend-cycle component is the same for both the decomposition methods( Additive and Multiplicative).
Trend Cycle Computation
To extract the Trend cycle component T(t), many organisations(Ex: Statistics Canada still use some variation of the classical m-Moving Average (m-MA) method.
Let’s discuss the m-MA(m-Moving Average) algorithm,
In mathematical terms, the trend component can be written as,
where,
In simple words, we can say that trend component for a time series at time t is average of all the points within k windows from it, including that point.
Let’s try to understand this better using a10 dataset (Rob J Hyndman fpp CRAN),
Here is a dataset a10(Rob J Hyndman), having monthly demand for antidiabetic in millions in Australia for the year 1990–2008.
We will be using the following python code to find 5-MA, 7-MA, 9-MA, 11-MA and 13-MA for this dataset.
Let us try varying m for the above function and see the first few rows :
We can see that first k rows for each m Moving average component don’t have values. This is due to insufficient points for those rows to compute the Moving Average component. We can handle those values using methods like cut and normalize approach but we will not discuss it here to keep the blog simple.
Let us see the plots for the various value of m.
We can see that as m increases the trend component becomes smoother.
We have taken only odd values of m here {5, 7, 9, 11, 13}. This is helpful when m is odd for example in weekly data (m = 7), but in cases where m is even like data recorded every month(m=12), we use 2*m MA method. (also known as centred Moving Average).
Our a20 dataset, which we have been using till now is recorded at the monthly level and thus centred Moving Average is much suited here.
Basically, we find the 12-MA for the data, and then apply 2 MA on the series obtained from 12-MA method.
Let us visualize the 2*12 MA component.
We can clearly see that the 2 X 12 MA trend component is much smoother than the other m MA trend components (5, 7, 9, 11, 13).
Developers don’t need to write the code from scratch for finding the trend component. In practice, we use the inbuilt python stats module to find the trend component.
Let us use the inbuilt python stats module and compare our 2 X 12 MA component (Trend-cycle component) with the trend component obtained using the python stats module.
Its just two lines of code (one import statement followed by a function call) with the help of stats module to find the trend component. 😃
Let’s compare the trend obtained from the inbuilt python stats module to the (2 X 12 MA) trend obtained from our m_moving_average() function.
So, we can see that trend component extracted by doing the computations ourselves is similar to one obtained from the inbuilt stats module.
The 2 X m MA or centred Moving Average method is equivalent to a weighted MA of order (m+1). The above 2 X 12 MA decomposition can be expressed as a moving average of order 13. The respective weights of the 13 months are 1/24, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12, 1/12 and 1/24. So a 2 X m model has first and the last elements taking a weight of 1/2m and all the other elements having weights equal to 1/m.
In general, a weighted MA method is expressed as
where the sum of weights is 1 and weights should be symmetric i.e.
Weighted MA gives a smooth estimate of the trend-cycle component.
Now, we know how to find the trend component using m-MA and 2*m-MA method. In the next blog we will find out seasonal and the residual component using classical decomposition.
References :