![](https://static.wixstatic.com/media/3206646ceb6b41fa90c3f63279531957.jpg/v1/fill/w_980,h_653,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/3206646ceb6b41fa90c3f63279531957.jpg)
SARIMA stands for Seasonal Autoregressive Integrated Moving Averages. It is a simple but quite powerful model to use for analyzing time series with seasonality.
For a good and detail explanation of SARIMA theory, please refer to the official page on statsmodels.org here https://www.statsmodels.org/stable/statespace.html
In this tutorial, I will not explain the theory behind SARIMA model, but will focus on the practical and application aspects of it only.
We will load a sample dataset to examine its trend and seasonality, then we will use a grid search algorithm to find the best-fit SARIMA model, and finally we will use the best-fit model to train with the historical data and predict the future.
First of all, let import all necessary libraries into our Jupyter notebook.
![](https://static.wixstatic.com/media/6993d4_97332b82bb10402bbeb7d193415e3c9f~mv2.png/v1/fill/w_980,h_323,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/6993d4_97332b82bb10402bbeb7d193415e3c9f~mv2.png)
Let's load the sample dataset and display its head to see how our data looks.
![](https://static.wixstatic.com/media/6993d4_8fca6ab54ced4c9a9ce4ef968c246e54~mv2.png/v1/fill/w_980,h_275,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/6993d4_8fca6ab54ced4c9a9ce4ef968c246e54~mv2.png)
This data is just a sample random dataset that I use Pandas to generate to artificially create trending and seasonality in the data for the purpose of this tutorial. The data has no meaning, it's just to present to the technical application aspect of SARIMA model.
Notice that this dataset is not a Time Series dataset yet. The year and month are in separate columns. We need to combine them into datetime and set it as the dataframe index to convert this dataset into a time series data. This is typical a scenario when we get real-life data.
![](https://static.wixstatic.com/media/6993d4_b3fd125569ff411c9569427eca659c31~mv2.png/v1/fill/w_980,h_443,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_b3fd125569ff411c9569427eca659c31~mv2.png)
Now, we can see that the index is no longer a numeric index, but changed to a time series index.
Let's plot the data to see how it looks.
![](https://static.wixstatic.com/media/6993d4_d964802581f14c61996cec3b37045cd8~mv2.png/v1/fill/w_980,h_603,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_d964802581f14c61996cec3b37045cd8~mv2.png)
As we can see obviously in the visualization, the data set is a time-series data with trending over time and has clear seasonality.
We use ETS Decomposition to extract the Trend and Seasonal attributes from the data.
![](https://static.wixstatic.com/media/6993d4_baec31ed8fab4efcbd3826e46160f472~mv2.png/v1/fill/w_980,h_530,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_baec31ed8fab4efcbd3826e46160f472~mv2.png)
As we can see in the ETS decomposition plot above, the 'Trend' and 'Seasonal' components are very clear.
We can also plot ACF and PACF to examine the auto-correlation of the time series data.
![](https://static.wixstatic.com/media/6993d4_5d3bdcfd3d4e4d38890192bc60ee7501~mv2.png/v1/fill/w_980,h_423,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_5d3bdcfd3d4e4d38890192bc60ee7501~mv2.png)
![](https://static.wixstatic.com/media/6993d4_62fe82ac3b524e96bce48f17d7e8079f~mv2.png/v1/fill/w_980,h_423,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_62fe82ac3b524e96bce48f17d7e8079f~mv2.png)
From the ACF and PACF, one more time we can see obviously the data are clearly seasonal.
At this point, we know we want to use SARIMA model to analyze this dataset. However, how we know with order and seasonal order to use for SARIMA model. We can split the data into train/test set and try many different combinations of params, i.e. orders, and then calculate the errors of each combination to find the smallest error combination. That's the best-fit model.
However, the is a more practical way, called grid search, which does exactly that by automatically test all combinations in order ranges provided and find out the best model.
Now, let's use this ARIMA grid search library called pyramid-arima to find the best-fit model.
![](https://static.wixstatic.com/media/6993d4_94f010a020fb4a8d9a79eb2ea80dac48~mv2.png/v1/fill/w_980,h_729,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_94f010a020fb4a8d9a79eb2ea80dac48~mv2.png)
The grid search normally takes quite a long time to find out the best model because it has to perform brute-force search for all combinations of order to find the one with smallest error.
In our auto_arima execution above, it found the SARIMAX with order (0, 1, 1) and seasonal order (2, 0, 2, 12) is the best model.
That's amazing! We don't have to do lots of data examinations to find the right model but just execute the grid search and sit-and-wait for it to find out for us.
Now, we can use split the data into train/test sets and test prediction of this model to see how well it predicts compare to the test data.
Checking the length of the data set.
![](https://static.wixstatic.com/media/6993d4_cc1b07db5dc845d18e01af7b0deed456~mv2.png/v1/fill/w_980,h_98,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/6993d4_cc1b07db5dc845d18e01af7b0deed456~mv2.png)
Split train/test datasets.
![](https://static.wixstatic.com/media/6993d4_189c292d6ac84404a09eeda0fefc12d1~mv2.png/v1/fill/w_980,h_87,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/6993d4_189c292d6ac84404a09eeda0fefc12d1~mv2.png)
Now, we can fit the model with train dataset.
![](https://static.wixstatic.com/media/6993d4_4b6651e0a4df469fad2da13a969e656d~mv2.png/v1/fill/w_980,h_736,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_4b6651e0a4df469fad2da13a969e656d~mv2.png)
After training the model, we can test prediction to see how close the prediction with the test data we have.
![](https://static.wixstatic.com/media/6993d4_58e49cb458a54804b82861fcd4dd8de0~mv2.png/v1/fill/w_980,h_86,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/6993d4_58e49cb458a54804b82861fcd4dd8de0~mv2.png)
Let's plot to compare prediction and test data.
![](https://static.wixstatic.com/media/6993d4_2faa3e1e558d43909bfdab7ff252e37d~mv2.png/v1/fill/w_980,h_521,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_2faa3e1e558d43909bfdab7ff252e37d~mv2.png)
As we can see the prediction and the real test data are quite close.
Now, let's quantify the accurateness of the model using root mean square error.
![](https://static.wixstatic.com/media/6993d4_79aa2fb51ae34efb834cf1731b000a3d~mv2.png/v1/fill/w_980,h_129,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/6993d4_79aa2fb51ae34efb834cf1731b000a3d~mv2.png)
Now we have a good model. Let's retrain the model for the whole dataset and try to forecast into the unknown future.
![](https://static.wixstatic.com/media/6993d4_afbe7ad87e1e40299d9f6f0627684890~mv2.png/v1/fill/w_980,h_115,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/6993d4_afbe7ad87e1e40299d9f6f0627684890~mv2.png)
After having the forecast, we can plot the historical data and forecast to see how they align together.
![](https://static.wixstatic.com/media/6993d4_0f10b42760ed4806aba1e66acb5bd6a1~mv2.png/v1/fill/w_980,h_509,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6993d4_0f10b42760ed4806aba1e66acb5bd6a1~mv2.png)
We can see that the forecast data look reasonably accurate.
In conclusion, the ARIMA/SARIMA model is simple but can be powerful and effective in analyzing time series data.
This Jupyter notebook and the sample dataset can be found in my Github repository here.
Happy Forecasting!
Comments