OK so that's lesson number one.
The lesson is that it's actually very unconventional to try and predict the stock price in the first
place.
Instead what we really want to try and predict is the stock return.
The return is defined as follows for a given period of time.
The return is the final price minus the initial price divided by the initial price
not just for some intuition.
This is the same formula you would use when you want to calculate a sale.
So for example what does it mean when we say something is 20 percent off intuitively you know that if
something costs 100 dollars and it's 20 percent off then you'll pay 80 dollars.
But we can also calculate this another way.
Take 80 subtract one hundred and divide by one hundred.
That's minus 20 percent.
In other words the price you pay is 20 percent less than the original price.
So to summarize when financial engineers do quote unquote stock prediction they are usually looking
at some form of return not actual prices.
OK so back to the code since we're working with a pan this data frame this is somewhat convenient at
least if you know the right functions to call
as usual we want to process our data in a vector rise form meaning we do some operation on all the columns
at once.
So first we need to shift at the closing price up by 1 so that yesterday's closing price is aligned
with today's closing price.
So x of 2 is beside x of 1 as a 3 is besides 2 and so on.
Then we can subtract them all in one step so let's do DFT it had to look at the result now as you can
see the first row is in again because we don't know the previous price for that row since it's not in
our dataset.
This is not a problem for us because we are building an hour in n it's going to take the first 10 days
of data to predict that the eleventh day.
So that would be from here up to the tenth row.
So we never even use this value.
All right so the 11th row is much further away than the first row.
So if the first row has any ends as long as you're not in the target that's fine.
Next we do our return calculation.
So we say D F return is equal at the close minus the F previous close divided by the previous close.
Let's do it.
The FDR had again to look at these returns.
So again the first value is in n n.
But we don't care
next we do a plot of this time series.
Conceptually this is no different from the stock price or a sine wave.
It's still just a time series but as you're beginning to see sometimes series are harder to learn from
than others.
Next we draw a histogram so we can see the distribution of returns we can see that these are pretty
small values.
So we might want to normalize them so we're going to go through the same thing again where we assign
the returns to a variable called series as an end by one matrix.
Note that here I'm taking dot values at index 1 and up because the 0 with index is an N N as before
we create a standard scalar call fit on the first half and call it transform on the whole thing and
then flatten it back to an N length vector.
Next we're going to go through all the same steps to build our supervised learning dataset and train
our auto regressive on and model this code again isn't any different than the previous code.
It's only the data itself that's different in now we are working with the series as the return rather
than the actual stock price.
So here's the data his are and then model and here's the result of training so the loss starts out at
about 1
and then by the end of training the loss is about point 8 7 and the test loss is about one point five.
So it's pretty clearly over fitting
well we can see from this is that the model has a much harder time learning anything.
If we look at the loss per iteration it looks like the loss goes down a tiny bit but only at the cost
of the validation loss going up.
In other words it's just fitting to the noise.
Next we're going to do a one that forecast
and so here are the results.
Now it's hard to tell whether this is good or not but judging by the previous loss values it's probably
not good as an exercise.
You might want to try to run this locally so that you can zoom into the plot and check for yourself.
Next we're going to do a multi-step forecast.
So here are the results for the multi-step
so again we have the situation where the model doesn't really match up that well with the actual time
series.