In this lecture, we are going to apply everything we learned about Arima and apply that to stock prices.
So this goes back to the fundamental question we asked at the beginning of this section.
If we make use of time series analysis, can we predict stock prices?
After all, stock prices are a time series, just like airline passengers, just like sales data and
so forth.
I think by the end of this section, we've come to learn a few basic facts.
Firstly, that Arima is a pretty well accepted staple of time series forecasting.
Secondly, is that a more modern approach to finding the best Arima orders would be to use auto arima
rather than trying to pick the orders manually.
So this lecture is all about taking everything that we learned and applying that to stock prices.
As a side note, I want to mention that this lecture contains no new code, which you haven't seen before.
So if you feel like exercising what you've learned, you already know what we're going to do if you
want to try to do it all on your own.
That would be highly encouraged.
OK, so let's start by downloading the S&P 500 at CSFI.
Next, let's install PMed Yarema, unfortunately, because this library doesn't come with Google CoLab,
you're going to need to install it manually each time.
Next, we're going to import PMed Yarema as well as pandas, lumpia matplotlib.
Next, we're going to use Pedigree's GSV to load in our data as a data frame.
Next, we're going to check the head to remind ourselves what the data looks like.
Next, we're going to filter out Google's closed prices and assign this to a variable named GOOG.
Note that you also have the option of filling the log prices instead for this lecture.
We're going to work with the nonlawyers prices, but you're encouraged to try both on your own.
Next, we're going to apply Google's closed prices so that we know what auto Auriemma is trying to model.
Next, we're going to split our data into train and test set, we'll use 30 days as the forecast horizon.
Next, let's call the function auto arima note that we pass in seasonal equals false sense.
If we pass in a seasonal equals true, then we have to pass in the seasonal period.
And of course, it's not clear what that is.
OK, so let's run this.
Next, let's run a model summary.
OK, so we see that the best model found is in a rhema three one zero.
So does that make sense?
The difference in order makes a lot of sense, because as you've seen, returns are pretty stationary.
Although the first difference of the raw price is not exactly the return, we can imagine that they
are related to.
Be sure you might want to plot the first difference and check it yourself.
The next thing we see is that the auto regressive component has order three.
That kind of makes sense.
It says that the last three days of data might be useful in predicting the next return.
OK, so let's look at Model yet Perens.
This provides some interesting information about the model, but not much we didn't already know.
The important part of this is that it gives us a nice way to retrieve the order of the model.
As you recall, we need to know this, since it affects which days we can make predictions for.
For example, if this is equal to one, then we can make a prediction on day one, because defensing
always makes the first row and any value.
OK, so next, we're going to write a function called plot result.
This function is very similar to the ones we wrote for the previous scripts.
Basically, the point is to plot the data itself, the fit it values for the sample data, the forecast
for the out of sample data and the confidence balance.
So as input into this function, we take in a trained model the full data, the train data and the test
data.
The full data is just the data.
Before we split the data into train and test.
The first thing we do in this function is to make use of the jet programs function we just looked at.
This is a dictionary.
So first we look at the key order, which gives us the order of the fitted Arima.
Next, we grab the second component, which gives us back the difference in order.
The next we call model predict in sample to get the train predictions.
Note that the starting value is the next we call model to predict to get the out of sample predictions
along with the confidence balance.
Next, we call our plotting functions.
First we plot the full data.
This will be given the label data.
Next, we plot the trend predictions.
This will be given the label fitted.
Next, we plot the test predictions.
This will be given the label forecast.
Next, we call the function to fill between to draw the confidence bounce.
Lastly, we call a legend to show the legend.
So let's run this.
And let's test that our plot result function.
So as you can see, the fitted model looks pretty good.
Of course, you shouldn't trust a plow like this because it's very small.
Unfortunately, it's too small to see whether or not our forecast is good, which is the part we actually
care about.
So next, we're going to write a function called a plot test, the point of this function is only to
plot the test period.
By seeing it up close, we'll get a better idea of how well our model actually performs.
So in this function, we only take in at two arguments, a fit model and the test data frame.
Inside the function we call model predict to get the predictions and the confidence bounce.
Next, we plot the true test data along with the forecast and then the confidence bounce.
Actually, this is the same code from earlier, so hopefully you're comfortable with it by now.
Next, we test out our new functional a test.
So what do we see?
Well, we see that our forecast is not actually that good.
Remember, this is over 30 trading days.
One nice thing about this is that it does seem to capture that average quite well.
In addition, the true price always seems to stay within the confidence bounds for the most part.
Of course, it's worth asking whether a model like this could actually predict what will happen over
the next 30 trading days.
Elon Musk goes on Twitter and starts making crazy tweets.
I don't think models like this will be able to predict that.
OK, so as you know, one important question we have to ask at this point is, do these models perform
better than the new forecast?
Is using these models worth it?
So let's define the arm as a function which you've seen before.
Next, let's print out the pharmacy for our Arima model and for the naive forecast note that for the
naive forecast, the train prediction can simply be a single value, which is the final value of the
train series.
This is because series and LEMPIRAS broadcast during subtraction so you can subtract a scalar from an
array or vice versa.
All right.
So what do we see?
Well, we see that although we've done a lot of hard work to train this model, it is, in fact the
naive forecast that wins.
OK, so next, we're going to do the same thing for Apple stocks.
OK, so note that for Apple, there was a big dip in the price over the last period, it's worth asking
yourself, is such a drastic event predictable by NRMA model?
Remember that these models tend to follow patterns if there's a trend, it follows the trend.
If they're seasonality, it follows the seasonality.
But I think intuition, it tells us that models like these do not predict anomalous events.
For example, a stock like this rising upwards very quickly and then dropping off all of a sudden.
That's not really a pattern.
That's more like the opposite of a pattern.
OK, so next, we're going to split our data into train and test.
Next, we're going to call Otto Arima again, note that seasonal is false.
So let's run this.
OK, so here's the model summary.
Again, the fact that the difference in order is one is not surprising, what is kind of surprising
is that this model is very different from the previous model.
Here we have P equals two and two equals two.
Earlier we had a P equals three and Q equals zero.
Next, we're going to call a function of plot result.
So as you can see, even with this tiny plot, it's obvious that the true data goes way outside the
confidence bounds.
However, I think this makes sense in terms of the model we're using.
The model seems to just want to carry on the existing trend.
Philosophically, I think that's what most models would do in the context of pattern recognition and
machine learning.
That makes total sense.
OK, so next, let's call the function a plot test so we can see the forecast period of close up.
All right, and we see that, in fact, the prediction is pretty far off.
However, note that in the short term, the prediction is not that bad.
Over the first few days, the true values stay within the confidence bounce for the most part.
Now, of course, we still have to check the root mean squared error against the naive forecast.
Again, the Niyi forecast performs better than the Arima.
Next, we're going to do the same exercise, but for the IBM stock prices, again, let's start by plotting
the data.
Next, let's split the data into train and test.
Next, let's call Auto Arima.
Next, let's check model summary.
OK, so what do we see, in fact, for IBM, the best fitting model is a random walk.
What does this mean?
It means that IBM stock returns are completely unpredictable, given lagged values or lagged errors.
Next, let's call Plott result.
OK, so nothing out of the ordinary.
It does look like the confidence bounce and close the true values in the forecast.
Next, let's call playtest.
Again, we see that the confidence balance and close the true values, in fact, this prediction doesn't
look that bad, the actual price crisis through the prediction line multiple times.
So what happens when we check the root mean square error against the naive forecast?
Well, we see that they are the same since this is a random walk model with no drift.
The forecast is just the last known value and it's the same as the naive forecast.
OK, so next, let's try Starbucks again, let's start by looking at the plot.
Next, let's split the data into train and test.
Next, let's call Otto Arima.
Next, let's check the model summary.
OK, so here is another random walk, a rhema zero one zero note, however, that this is a random walk
with Dreft since the model fits with a non-zero intercept term.
Next, let's check Resul.
OK, so it's a bit hard to see what's going on during the forecast.
Next, let's check playtest.
All right, so this doesn't look too bad, it's decently accurate for the first few values and then
it overestimates the price when the price drops, although most of the time the price does stay within
the confidence bounds.
So the lesson is, don't ignore the negative side of your confidence balance.
Those values are perfectly achievable and they result in a loss of money.
So what happens when we check the root mean square error?
Well, although we detected that this was a random walk with a non-zero drift, it turns out that assuming
a random walk with zero drift would have been better, the naive forecast wins again.