The purpose of this is to go over a function to grid search SARIMA parameters and output a fitted model with those parameters. I am skipping differencing, Dickey Fuller tests, and plotting ACF and PACF. If this were a comprehensive SARIMA demo, those things would be included.


First, I start with my imports and dataset loading.


Then, I take a look at my first few rows along with the shape.

Loading output library...
Loading output library...

The next step is to make the month column the index.

Loading output library...

A quick plot visualizes what we are working with.

Loading output library...
Loading output library...

The data gets broken up into training and test, the test making up about the last year.


And here is the function. It takes two parameters, a dataset and an eval metric (aic or bic). This first part loops though all the variations of parameters (for order and seasonal_order), and collects that into a dictionary. The second part finds the best model from the dictionary and then fits a model to it. The output is a dictionary with the model, aic, bic, order tuple and seasonal_order tuple.


The called function is put into a parameter.


Here we can see the the items of the best dictionary.

Loading output library...

Finally, we put the prediction into a variable using the model from the best dictionary.


The prediction is then plotted with the actuals.

Loading output library...

To cap this off, I want to run the loop that found the best parameters, but this time, it will just print out the results. From this we can verify what our function chose.