In this lecture, we are going to apply auto arima to the airline passengers data center.
Our main goal in this lecture will be to improve the results we got in the last lecture, which shouldn't
be very difficult since we were just guessing.
That is, if Auto Roraima works as it should.
OK, so let's start by downloading the airline passengers data set once again.
Next, let's import Pendas Nambiar matplotlib.
Next, we're going to install the PMed Rhema Library, this is necessary because this library does not
come preinstalled in Google CoLab.
Next, we're going to load in our data set using that reads ESV.
Next, we do a DFG head, as usual, to ensure our data looks as it should.
Next, we're going to take the log of the passengers column and store this in the log passenger's column.
This should be interesting to compare, since previously we found that logging the passengers column
helps the model achieve better accuracy with the same Arima order.
Next, we're going to split our data into train and test with the last 12 data points assigned to be
the test.
Next, we're going to import the PMed, a library.
Next, we're going to call the auto Arima function, OK, so what are these arguments?
The first argument is the data that one should be obvious.
The second argument is, Trece, we're going to set this to true so that we can see which models auto
Arima tests out while it tries to find the best model.
The third argument is suppress warnings, which we are also going to set the true, as you know, stats
models has lots of warnings.
So this will make sure that those warnings are not printed out.
Finally, we said seasonal equal to true, since we want to fit a full seasonal Arima model and we set
em equal to 12 since we know that the seasonal period is 12.
OK, so let's run this.
All right, so once this is done, we call the function model that summary.
So if you've ever worked with are, you should be very comfortable with this kind of output.
All right.
So basically, this is going to tell you the orders of the best model and other information, such as
the AIC, the Bisi log likelihood and the P values of various statistical tests.
You can also see the value of each parameter in the model and whether or not there are statistically
significant.
OK, so what's our best model, it looks like our best model is an R three for the non seasonal par
and for the seasonal part, all it does is the first difference.
Remember that this is the first seasonal difference.
So instead of subtracting from one time step behind, it's subtracting from 12 times steps behind.
You might actually want to try that in code to see if the result looks stationary.
Also note that this is a much more parsimonious model than the non seasonal Arima we found earlier,
which was in a Arima 12 one zero.
This model has only three dates and one intercept.
OK, so next, let's call model that predicts.
As input will say, experience periods equals and test and return confidence equal to true so that we
can plot the confidence interval.
Next, we're going to plot our forecast along with the true values, so first we're going to create
an axis object and set the figure size.
Next, we're going to call the plot function on the test data frame.
We'll give this low label data.
Next, we're going to call the plot function on the test predictions.
We'll give this the label forecast next.
We're going to call the function of field between which will plot the confidence bounce.
As with stats models, the confidence bounce are returned as a T by two array.
So which row represents a timestamp in each column represents either the lower bound or the upper bound.
Finally, we call X legend to plot the labels.
OK, so let's run this.
All right, and we see that our forecast is pretty good.
Next, I want to plot a plot of the full model prediction, just like we did before.
This will include both the forecast and the sample predictions.
So first, we're going to need to call the function model, predict in sample.
We'll pass in start equals zero and end equals minus one to get the full training predictions.
Note that unlike stats models, we do not need to specify that we want the predictions to be accumulated.
If you have differences in your model PMed Yarema, we'll just assume that automatically.
Next, we call all the usual plot functions to make our plot.
So let's run this.
OK, so this pretty much looks as expected, both the training predictions and the forecasts look pretty
good.
One weird part about this graph is the beginning, where all we see is a flat line since we now have
a seasonal model.
It's not possible to make predictions for the first few times that this is due to the fact that we require
values from a one season behind which don't exist.
OK, so next, since we know that our model might perform better on the log data set, we're going to
run our auto arima again, but on the log passengers, otherwise, all the arguments to this function
are the same as before.
So let's run this.
All right, so now let's check model that summary.
So as you can see, this finds a slightly different model, then the Ottawa rhema on the non logged
data set.
Now we have two auto regressive coefficients and one seasonal moving average coefficient.
Again, we do seasonal differences rather than normal seasonal difference in.
All right, so again, we have a plot of the forecast against the true values, I'm not going to explain
this code since it's exactly the same as the previous code.
So qualitatively, the results still look pretty good.
Next, let's look at the full train and test predictions again, this is the same code as before.
OK, so the plot is kind of hard to see, since the first few values stretch all the way down to zero,
you could block this if you wanted, but I'm not going to bother.
Now, since both of these models look to be performing pretty well, it might be a good idea to test
them quantitatively, that is to use the IMC.
You've seen this function before, so there's no need to explain it again.
So let's run this.
All right, so this is kind of the opposite result from what we saw previously in this case, the non
diversion performs better.
Interestingly, we get an army of about 18, something which is on par with our previous Arima 12 one
zero model, the one we found by just guessing.
Now, the next thing I would like to do in the script is to test Otto Arima itself.
I want to know, can Otto Arima find the model that we found last time?
So I'm going to call Otto again on the log passengers.
This time I'm going to set a few more parameters into this function.
First, I'm gonna set Max P equal to twelve and Max Q equal to two if you look at the documentation.
These values are set quite low by default.
So if we want auto arima to even have a chance of finding the model that we found, we have to set them
higher.
Note that we also have to set a parameter called Max Order.
This value has a limit on the total sum of all the Arima orders, which promotes finding a simpler model.
Again, the default value is five, which is very small.
So in order to have a chance of finding our previous model, we need to set this higher.
I've set it to 14 since that's the sum of 12 into next.
We have an argument called stepwise.
This controls whether we use an intelligent algorithm to search the hyper parameter space or if you
use a naive grid, search the default value for this is true.
So for now, we'll leave it like this and see how it performs.
Finally, we set Seasonale equal to false, since our goal right now is to compare these results to
the results from the previous script.
OK, so let's run this.
OK, so what's interesting about this is that it doesn't even try that many values.
In fact, we never get past P equals five, but perhaps this is a better model than what we found before.
If we do a model that summary, we can see that the presumably best model is a rhema for one two.
Next, let's plot our model forecast.
OK, so we can see that this forecast is pretty bad.
Next, let's plot both the terrain and test predictions together.
Again, not so great.
Finally, let's check the arms, see?
So as you can see, it's pretty high relative to what we've seen before.
OK, so at this point what I want to do is to compare what we just did to a full grid search.
So let's go back up.
And let's set stepwise false.
And let's run this all again.
OK, so this time we can see that auto arima is actually testing out all the possible combinations of
models, not that it would not be a good idea to do this if you had a full screamo model that is both
the Nazis in a part and the seasonal part.
Again, this goes back to the concept of the curse of dimensionality.
Currently, we're only testing out two hyper parameters, which is a two dimensional grid.
If you have on average 10 values of each type of parameter, then that would be one hundred values to
test.
If you had the seasonal part to, then you would have 10 to the power for values to test.
That's 10000, which is 100 times more than one hundred.
OK, so in any case, let's run model that summary.
As you can see, the best model appears to be a rhema 12 one one.
This is very close to our manually found model, which was 12 one zero.
OK, so next, let's plot the forecast.
OK, so that looks pretty good.
So already we can see that perhaps auto Arima might not necessarily find the best model with the default
settings.
Next, let's plot the terrain and test predictions together.
Again, this looks pretty good.
Finally, let's print out the pharmacy.
OK, so we get about twenty three something.
This is actually quite interesting because this is a worst model than the model we found on our own.
As you recall, a remote 12 one zero achieves 18 something or AMZI.
Furthermore, that model has less parameters than this one.
So this model actually gets a higher penalty for having one more parameter.
So what this tells us is that finding the best AIC is not the same as minimizing the out of sample error.
They are similar ideas in concept, but one is looking for a simpler model purely to look for a simpler
model, while the other doesn't actually care about model complexity, but rather accuracy.
So these two ideas are related, but they are not the same.