In this video, we are going to look at Arima in code for the first few Arima lectures will continue
looking at the airline passengers data to build your intuition on how a remote works.
And then to cap off this section, we will return to looking at stocks.
So in this lecture, we will focus solely on airline passengers.
Since using this data set, it will be much easier to understand the behavior of a rhema under different
party and new values with stocks.
This is kind of difficult because you can't really tell if there's a problem due to bad settings or
if it's simply due to the inherent randomness in stock prices themselves.
So hopefully that makes sense for you.
So we'll start by updating stats models, as you recall, at the current time, the version of stat's
models on CoLab is different from the latest version.
This notebook happens to contain code that will work differently depending on which version you have.
Note that if you've seen a similar lecture from me before, there are a few details that are different
in this version of the script.
Of course, this sort of thing changes on pretty much a week by week basis.
But I know that you, as a good data scientist, are not intimidated by these little details.
OK, so the next thing we're going to do is download the airline passengers CSFI.
Next, we import Pendas nonpaying matplotlib.
Next, we're going to load in our CSFI using predatorial CSFI.
Next, we do a different head just to remind you of what's in our data frame.
Next, we do a dot plot to remind you what our data set looks like.
Next, we're going to calculate the first difference in our data.
Next, we do the plot so you can see what the first difference of the Time series looks like.
As you can see, it's not quite stationary because there is some seasonality and the variation is increasing
over time.
Next, we're going to compute the log of the passengers column by calling airport log on the passengers
column.
Next, we plot the log passengers column.
Notice something interesting about this, which is that the values don't seem to be growing as rapidly
as they were in the non logged time series.
Furthermore, the amplitude of the cycles looks more constant than it was before.
Next, we're going to import the Arima class from stat's models.
Next, we're going to set the frequency of our data from index to months.
And we're also going to split our data into train and test using the last 12 months of our data set,
as the test said.
The next step is to create a trainer and test reacts in order to index our data frame either in the
train or test rose.
Recall that this is necessary for when we want to store our predictions in the data frame.
Next, we're going to test in air one model.
This is a model in which the current value in the Time series depends linearly on just the previous
value, as you recall.
This is equivalent to a rhema one zero zero.
Next, we call a treatment outfit, as with hot winters, this returns and a remar result object.
Unlike psychic learn, this does not return a model.
Next, we call a rhema result predict to obtain the training predictions will place this in a new column
in our data frame called R-1.
Next will plot the passengers column along with the air one column to see how well our model fits with
the data.
So as you can see, we get this kind of delayed behavior, which should remind you of what happened
with whole winters, as you recall, this is how hot winters also behaved when the model was specified.
In these cases, it seems like the best your model can do is to just copy the previous value or close
to it.
Next, we're going to grab our out of sample predictions by calling a rhema result, get forecasts for
NASA steps.
OK, so unlike other prediction functions you may have seen, this does not return numerical data directly.
Instead, this returns an object of type prediction results.
You're encouraged to check the stats, models, documentation.
If you want to learn exactly what attributes and methods are available in this course, you'll be shown
the relevant parts.
So if you want to access the actual data corresponding to the prediction, we simply call the attribute
predicted mean.
Note that we can also obtain data like confidence intervals, but we won't look at that just yet.
For now, let's just plot the air one column again against the passengers data.
As you can see, this model is not very good, in fact, it doesn't even capture the fact that the data
is trending upwards.
Instead, the forecast is going in completely the wrong direction.
So even a naive forecast would be better than this.
The next step is to explore the prediction results a little bit further.
Let's first check to see what kind of object it really is.
As you recall, we can accomplish this by using the tape function in Python.
OK, so we see that this is a prediction results rapper, so this is a common paradigm in object oriented
programming, but basically since this is a rapper, you can assume that any attribute or method that
belongs to a prediction results object probably also belongs to this rapper object, at least if you
want to be lazy.
Otherwise, check the documentation in the case where you encounter any discrepancies.
The next step is to learn how to obtain our confidence intervals in order to do this, we'll call the
function and confident.
OK, so as you can see what we get as a data frame, which contains two columns, the lower bound in
the upper bounds for each point in time.
Notice the convention for the column names.
It's basically the string, lower or upper, prepend it to the original column name.
Next, we're going to write a function that will plot the fitted values of an arena model along with
the forecast and confidence intervals.
This will be helpful for the rest of this script so that we don't have to keep writing this code over
and over again.
So this function, it takes in one argument, which is NRMA result object inside the function.
The first thing we do is set the size of the player, which will be 15 columns by five rows.
Next, we call applied function passing in the passenger's column of the data frame.
This will plot the true data.
Next, we're going to obtain the train predictions by calling a result that fitted values.
Then we will plot the train predictions by calling the plot function again, passing in the train index
along with the train predictions we just obtained.
We'll make this plot green and we'll give it the label fitted.
Next, we will plot the forecast to start, we need to call ResultSet forecast for NTSA steps.
This gives us backup prediction results objects from there.
We need to call the method in order to get a data frame containing the confidence intervals.
The next step is to assign these confidence interval columns to variables called lower and upper.
The next step is to grab our forecast to obtain the actual predicted values.
Again, this uses the attribute predicted mean.
Next, we will call the plot function again, this time passing in the test index along with the forecast
values.
We'll give this the label forecast next.
In order to plot the confidence balance, we'll use the function at fill between the arguments to this
will be the test index.
Once again, the lower and upper values, the color red and alpha equals zero point three to set the
transparency.
Lastly, we call a legend so that the legend will show up in our PLI.
All right, so in this next block, we're going to test the function we just created passing in the
Arima result objects we got back earlier.
OK, so this is our Awana forecast again, not very good, but we have many options to try next.
Next, we're going to try something which I think should be pretty natural for most machine learning
people, if our model is bad, why not simply add more inputs?
And so in this next block, we're going to use an Artan model.
Luckily, there's not much more work to do beyond what we've already done.
We start by creating another Arima object with the order at 10 00.
Next, we call a remote outfit and then we pass the Arima results into our plotting function.
So let's run this.
OK, and we see that this does, in fact, do a much better job.
The first thing we notice is that it does not have the Lagan characteristic that the previous R-1 model
had.
This suggests that the model is actually learning to anticipate the pattern rather than just copying
the last value.
If we look at the forecast, we see that the model really does learn that the signal is periodic.
Unfortunately, it seems to underestimate the true value significantly.
Next, we're going to test and may one model, as you recall, this is equivalent to a rhema zero zero
one as before.
We can just repeat the same code from earlier.
So let's run this.
OK, so this looks pretty bad.
It's clear that the May one model does not work.
In fact, it's even worse than R1.
As you recall, the moving average models expected forecast is always a constant value.
So intuitively, we know that this is probably not the right choice.
All right, so next, we're going to investigate using the log passengers.
Let's begin by taking the first difference of the log passengers.
Again, we use the function.
Next, we plot the first difference of the log passengers.
As you can see, the behavior is indeed what we expected.
Unlike the first difference of the non logged passengers, these values do not seem to grow that much
over time.
All right, so next, before we do anything with the log passengers, I want to fit a new model on the
non logs passengers.
We know that defensing is good because it at least seems to remove the trend.
So for this model, I'm going to use a rhema eight one one I've chosen P equals eight since it seems
like regression on more pass values helps.
I've chosen D equals one since as we saw, this removes the trend from the Time series and I've chosen
Q equals one for good measure just so that we have a full Arima model with all the components.
Now in order to plot the results from this Arima, we're going to need to create a new function.
Y the reason why is because we've differenced our data, as you recall, when we difference our data.
This removes one row from our data set, specifically the first row.
Each point in the different time series is the current value minus the last value.
But for the first row there is no last value and therefore that value can't exist.
You'll see that if you try to use the previous function that we wrote for plotting the results, you
will get an error.
You were strongly encouraged to try it yourself and to see what the error actually is.
OK, so this function is called plot fit and forecast int.
It now takes in a three arguments instead of just one.
First, it takes in the Arima results object, same as before.
Second, it takes in a value for D.
D will represent the first row in the Difference Time series that actually exists.
Third, we have an argument called Call, which will tell us which column of the data frame we fit,
the model line that will either be passengers or log passengers.
Of course, we have to choose the appropriate one in order to plot the correct data.
With the model predictions inside the function, we first plot the data.
This looks the same as before, except now we index the data frame using the call argument rather than
a fixed column name.
Next, we grab our training predictions using the predict function.
I've used the predict function so that we can be explicit about our indices, but also because it's
necessary in order to get the values back in terms of the original time series rather than the difference
the Time series.
Notice that for the start argument I've passed in train that index index at D before this value would
have just been zero instead of D.
Next, we call the plot function for the predictions again, notice that for the index, I'm only passing
in the values from the death row onward.
Next, we plot the forecast, this part is the same as before we call the forecast function for n test
timestamps, then we call the plot function to plot the forecast.
Then we call the fill between function to plot the confidence interval.
Lastly, we call the legend function to show the legend.
Next, we're going to call our function, which will plot the results.
OK, so as you can see, this does pretty well better than a purely auto regressive model, the train
fit is pretty good and the forecast captures the seasonality, although it still seems to underestimate
the peak.
Next, we fit in a Rhema model with the same speed and values, except this time we use the logged data
set.
As usual, we call fit and then we plot the results.
All right, so as you can see, our model still does decently well, it captures the seasonality, but
it still underestimates the peak.
It seems to do a bit worse than the non log data.
So for the next step, I wondered what would happen if I simply added more auto regressive terms, this
time with equals 12 and two equals zero.
The reason I chose 12 here is because that's the length of the period.
So it might make sense to use data from all 12 months over the past year in order to make a prediction.
OK, so this is the same code as before, except now the Arima order is 12 one zero.
OK, so let's run this.
OK, so what do we see?
Well, it seems like this is our best model so far.
Now, just out of curiosity, I wanted to fit another model on the log passenger's data set with the
same Arima parameters.
OK, so, again, it looks pretty good, but it's hard to tell which one is better.
Now, in order to really know how well these models have performed, it would be useful to look at a
metric such as the root mean squared error.
So next, we're going to define such a function.
This will take in two arguments.
The Arima results object and a flag to tell us whether the model was fitted on the log passengers or
the non log passengers inside the function we call results forecast to get the forecast.
Next, we check is logged.
If is logged is true, then we exponentiation forecast to put the data back into the original scale.
Finally, we assign the passengers column of the test data frame to a variable called T.
Then we assign the forecast to a variable called Y.
Then we apply our usual formula for the root mean squared error and then we return this value.
All right, so in the next block, we compare the root mean squared error for the last four models we
created.
These are Remate one one on the raw data set a eight one one on the log data set, RMI 12 one zero on
the raw data set in agreement 12, one zero on the log data set.
OK, so let's run this.
So what do we see?
Firstly, our suspicions were correct, logging the data was a bit worse, at least for me, maybe one
one.
Next, we see that adding more lagged values to the auto regressive part of the model does really help.
A rhema 12 one zero achieves the best accuracy.
The best combination appears to be a rhema 12 one zero with logging.