Get startedGet started for free

Seasonal time series

1. Seasonal time series

You've mastered the art of ARIMA models, which we can use to predict a great many types of time series. In this chapter you will learn about seasonal time series and how we can extend the ARIMA model to predict seasonal data.

2. Seasonal data

A seasonal time series has predictable patterns that repeat regularly. Although we call this feature seasonality, it can repeat after any length of time. These seasonal cycles might repeat every year like sales of suncream, or every week like number of visitors to a park, or every day like number of users on a website at any hour.

3. Seasonal decomposition

This is monthly US candy production, which has a regular cycle that repeats every year. We can think of this, or any time series, as being made of 3 parts. The trend, the seasonal component, and the residual.

4. Seasonal decomposition

The full time series is these three parts added together.

5. Seasonal decomposition using statsmodels

We can use the statsmodels seasonal-underscore-decompose function to separate out any time series into these three components. We first import the function from statsmodels-dot-tsa-dot-seasonal We use the function by passing in the series. We also need to set the period parameter, which is the number of data points in each repeated cycle. Here our cycle repeats every 12 steps. This function returns a decompose-results object.

6. Seasonal decomposition using statsmodels

We can use the plot method of this object to plot the components.

7. Finding seasonal period using ACF

In order to decompose the data, we need to know how often the cycles repeat. Often you'll be able to guess this, but we can also use the ACF to identify the period. This is the ACF plot for the candy production time series. The ACF shows a periodic correlation pattern. To find the period we look for a lag greater than one, which is a peak in the ACF plot. Here, there is a peak at 12 lags and so this means that the seasonal component repeats every 12 time steps.

8. Identifying seasonal data using ACF

Sometimes it can be hard to tell by eye whether a time series is seasonal or not. This is where the ACF is particularly useful. This is the monthly number of vehicles registered in the US. We could take this time series and plot the ACF directly, but since the time series is non-stationary, the ACF plot will be clearer if we detrend it first.

9. Detrending time series

We have detrended time series before by taking the difference. However, this time we are only trying to find the period of the time series, and the ACF plot will be clearer if we just subtract the rolling mean. We calculate the rolling mean using the DataFrame's dot-rolling method where we pass the window size and also use the dot-mean method. Any large window size, N, will work for this. We subtract this from the original time series and drop the NA values. The time series is now stationary and it will be easier to interpret the ACF plot.

10. Identifying seasonal data using ACF

We plot the ACF of the detrended data and we can clearly see that there is a seasonal period of 12 steps.

11. ARIMA models and seasonal data

Since the data is seasonal we will always have correlated residuals left if we try to fit an ARIMA model to it. This means that we aren't using all of the information in the data, and so we aren't making the best predictions possible. We'll come back to this in the next lesson, but for now...

12. Let's practice!

... let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.