Time series forecasting : SARIMA

CHAHINEZ AREZKI
6 min readSep 20, 2020

--

Creating a seasonal ARIMA model using Python and Statsmodel. The process is shown below :

  1. Visualize the data
  2. Make the time series stationary if it is not
  3. Plot ACF/PACF charts and find optimal parameters
  4. Build the ARIMA Model
  5. Make prediction

1. Import and visualize the data

We start by importing the time series. After that, it is necessary to visualize the data to understand what nature is the time series and that would define the type of model we should use. Questions that we are going to respond to are: Is there an overall trend in the data that we should be aware of? Does the data show any seasonal trends? This is important when deciding which type of model to use.

Time serie

We visualized the data by using plot() function, and we can see there is both: an upward trend in the data and there is seasonality to it. The graph below rise the assumption that is not stationary because of the trend, seasonality, and that the mean increases over time, and the variance will not be constant either.

Another tool to visualize the data is the seasonal_decompose function in statsmodel. With this, the trend and seasonality become even more obvious. The results as shown below:

From the graphs above, we can conclude the period of seasonality which is 12 months, and as we already mentioned we have a positive trend.

There are two ways to check the stationarity of a time series. The first is by looking at the data. By visualizing the data it should be easy to identify a changing mean or variation in the data. For a more accurate assessment, there is the Dickey-Fuller test.

|Results of Dickey-Fuller Test:
Test Statistic -0.899593
p-value 0.788106
#Lags Used 14.000000
Number of Observations Used 141.000000
Critical Value (1%) -3.477601
Critical Value (5%) -2.882266
Critical Value (10%) -2.577822
dtype: float64

The p-value is not at all close to zero. So, we can conclude that the null hypothesis (a unit root is present) is accepted with strong confidence and this makes the time series non-stationary.

2. Stationarize the data

What does it mean for data to be stationary? Stationary means mean, variance, and covariance are constant over periods.

So, now we need to transform the data to make it more stationary. The first thing we want to do is take the first difference of the data. This should help to eliminate the overall trend from the data.

|Results of Dickey-Fuller Test:
Test Statistic -0.899593
p-value 0.788106
#Lags Used 14.000000
Number of Observations Used 141.000000
Critical Value (1%) -3.477601
Critical Value (5%) -2.882266
Critical Value (10%) -2.577822
dtype: float64

We see that the new p-value decreased, so we can say we are in the right way to make the time series stationary. While this helped to improve the stationarity of the data it is not there yet. Our next step is to take a seasonal difference to remove the seasonality of the data and see how that impacts the stationarity of the data.

|Results of Dickey-Fuller Test:
Test Statistic -1.984795
p-value 0.293279
#Lags Used 14.000000
Number of Observations Used 129.000000
Critical Value (1%) -3.482088
Critical Value (5%) -2.884219
Critical Value (10%) -2.578864
dtype: float64

Compared to the original data there is an improvement, but we are not there yet. The next step is to take the first difference of the seasonal difference

|p-value = 5.085877052016867e-07

As you can see by the p-value, it is less than 10e-3. The null hypothesis is now rejected with strong confidence. The time series is then stationary. So, we conclude that taking the seasonal first difference has now made our data stationary. Since we have transformed the data series, we are ready to apply any suitable model.

3. Plot the ACF and PACF charts and find the optimal parameters

The next step is to determine the tuning parameters of the model by looking at the autocorrelation and partial autocorrelation graphs.

Now we analyze the AutoCorrelation and Partial AutoCorrelation Functions. As we know, ACF helps us to identify the MA order, so from our ACF we can conclude that the q parameter will have a value equal to q=1 because it cuts off after the first lag. And since PACF helps us define the AR order, from the plot above we conclude that the p parameter will have a value p=2, because it cuts off after the second lag.

As we mention before, it is obvious that the time series has seasonality (S), so whatever the model we choose, we know that we have to incorporate seasonality. We know that there might be a MA component because the value of something at some period depends on other value periods before and this gives us the conclusion to have MA and AR model. We saw that there is an upward/positive trend over time that we needed to account for so the integrated part is necessary too (I). By this short analysis, we know that may be the best choice is to use SARIMA (seasonal ARIMA), model.

Now, we continue to define parameters, because SARIMA needs more parameters: from ACF and PACF we have the value of p=2 and q=1. We already applied the first difference to reduce the trend, so the value of d=1. So, the first part of the parameters is (2,1,1). Now, we analyze the second part, where we already mentioned that seasonality is each 12 months, so m=12. The three other parameters (P, D, Q) are analogs of (p, d, q) that we already defined, except for the fact that they are for seasonal components. This means that for d=1, we took the first difference and we used the backshift operator by one period, and now the D value D=1 will backshift the entire time series also by 12 time periods, since we have m=12 and it applies to P, D, and Q. The values P and Q, which are the orders of the seasonal AR and MA, will inherit the values of p and q.

Note: We could have used an automatic parameters generator (auto_arima function), which applies different values of operators and by choosing the best value for AIC, it defines the correct parameters, but we preferred to make our analysis.

Now, we are ready to build our model 😃

4. Build Model

We split the data in train and test and build the model with the parameters we defined. Now we know we define the parameters for the model ((2,1,1)x(2,1,1,12)) and fit it to our time series.

5. Make Predictions

Now that we have a model built and we have fit it, so the time series is trained. This means that can make forecasts. First, we use the model to forecast for periods that we already have data for, so we can understand how accurate are the forecasts.

The plot below shows the observed values in blue and the predicted values in orange. As we can see from the plot, the orange line is quite close to the blue one.

--

--