In this lecture we are going to do what I think is a lot of students favorite exercise how to predict
stock returns.
We are going to use the Alice theme for this.
The most powerful are an end unit we've encountered so far.
I hope that as you go through this lecture you will consider everything you've learned so far.
Throughout this section this lecture is going to walk you through a prepared call lab notebook.
Although a very good exercise which I always recommend is once you know how this is done to try and
recreate it yourself with as few references as possible as usual you can look at the title of the notebook
to determine what notebook we are currently looking at.
First I want to answer the question What is the motivation behind this example.
Why are we doing it.
Well the answer may surprise you.
There are a lot of resources out there that will claim to be able to predict stock prices using LSD
Ms.
Now you must ask yourself how many of these people have actually tried their techniques in the actual
stock market using their own money.
I can tell you how many 0.
Unfortunately the methodology that a lot of these resources take is extremely flawed.
You might have even joined the chorus because you thought it was going to teach you how to predict stock
prices using our own ends.
I mean it's understandable if you are a beginner all you know is that deep learning is this new field
and it's very popular because people have started to realize how powerful it is.
And so you start to believe that because deep learning is so powerful it must be good at making predictions
with all sorts of data including stocks.
You see deep learning and you think this will be a game changer for making predictions in the stock
market.
All you need to do is use this magical Ellis to em and you'll automatically extrapolate future stock
prices using historical stock prices.
Unfortunately you have fallen for a marketing trap.
So what this lecture is really about is fixing incorrect thinking taught to you by all the market is
out there pretending to be data scientists.
It's not easy to differentiate between a marketer and a data scientist.
I can make a course on deep learning but so can a marketer.
Sometimes there may be only subtle differences in what we teach because marketers can just hire five
dollar an hour freelancers online who themselves are also not actual data scientists.
The Internet is as egocentric.
People write blogs not because they are interested in or knowledgeable about the topic but because they
want you to click on their links and visit their Web site and view their advertisements.
What ends up happening is everybody starts copying each other.
This has network effects if one guy who has a popular blog ends up writing incorrect code.
Guess what happens.
Everybody who is a marketer or wants to do a CEO is going to copy that guy's incorrect code.
It's going to spread everywhere on the Internet which due to a CEO will hide the true answer from you.
I've seen this in several places throughout my career already.
This example not included one example of this is with vague pre processing and another example is the
formula that Reddit uses in their ranking algorithm.
Unfortunately when people stop thinking for themselves and resort to simply copying others because they
are popular it is easy to make mistakes.
So this lecture is all about undoing those mistakes.
Now back to the code.
The first step here is to use pennies to grab the data from my github.
Yes lucky you.
I am not going to make you download this data yourself.
This data contains daily stock prices for Starbucks from about February 2013 to February 2018.
So let's do a DFT head and see what's in this data frame.
Now first of all if you are not an expert in finance you might be surprised that this data when we talk
about stock prices we usually think of these stock price.
The stock price is a number and each day it's a different number.
So you might think of this as a single variable time series.
And yet here we have multiple columns.
So what are these columns.
The first one is the date.
That's pretty obvious.
Second we have four different numerical columns open high low and close.
Now all of these are stock prices.
So the way the stock market works is it starts early in the morning and it stays open for the workday
and then it ends and stock prices are tracked at a very granular rate.
So it's not just a daily thing.
It's pretty much real time.
So at any split second you can find out the exact stock price.
So to put it in simple terms the open price is the price at the start of the trading day.
The closed price is the price at the end of the trading day.
The high price is the maximum price for that day and the low price is the minimum price for that day.
Finally the volume column is the number of shares of the stock that were traded that day.
If this is new to you this is probably very confusing.
But luckily we always remember my rule.
All data is the same.
The machine learning algorithm doesn't really care what these things are.
They are just a table of numbers.
All we have to do as machine learning students is make sure those numbers are in the right range for
the neural network as you can see the price values are in the tens and the volume values are in the
millions.
So this is an example of where different columns of our data have different scales.
In any case we can see some of the beginning values in 2013.
If we do DFT head and we can see some of the ending values in 2018 if we do the left tale so it ends
on February 7th 2018 all right so that's nice for you.
If you've invested in Starbucks your stock approximately doubled in the past five years
next.
Let's begin our work by demonstrating how some resources out there do this the wrong way.
By the way I'm not naming any names but I know that a lot of you taking this course have encountered
a course where they do an example just like this
so we would like a single variable time series to start with.
So let's just focus on the closing price.
So we're going to do dot values to get this as a num pi array and then we are going to reshape it to
an end by one matrix.
We need to do this because the next step is to apply the standard scalar from psychic learn so that
our data is standardized.
Note that I'm calling the fit function on the first half of the series only.
This is because I don't want to include test data in the training pipeline.
I call fit transform however on the entire dataset.
Also note that because we're going to be taking windows of this time series you would have to do a few
calculations to figure out where exactly the boundary between the train set and the test set is.
I kind of hand wave this and said let's just stop in the middle which is probably good enough but it's
probably off by a little bit the mean and variance won't change that much if we add a few values or
take away a few values so once we're done we flatten the series to get back in any length vector since
that's what the code we were working with before expects to see
so next we have the same code which turns our time series into a supervised learning dataset.
Basically it's going to take windows of length 10 and the target will be the next value.
So conceptually this is no different from our sine wave time series.
The only difference is the data itself.
It is no longer a sine wave but a real data from a real stock.
But again all data is the same.
The algorithms we are using don't care about that.
The code is still exactly the same
next.
Right off the bat we are going to use an Alice TDM network.
Now you could attempt to use a linear model or a simple or an N but we are going to skip that in the
spirit of following what these other resources are doing as an exercise.
You might want to try those approaches and see how well they work
so after defining the model I'm going to instantiate it and I'm going to move the model to the GP you
want to create the laws and optimizer then I'm going to define a training function
then we're going to do the usual train to split and we're going to move the data to the GP you and finally
we train the model all right so let's scroll down and loss is the loss here is the loss per iteration.
So we see that the loss does decrease which is promising.
That means our Ellis team network can indeed predict somewhere close to the next value in the time series
and now we have our one step forecast again this looks pretty darn good if you are just learning deep
learning and you encounter something like this.
You probably thinking to yourself Man I just hit the jackpot.
But then you have to ask yourself.
There must be other people out there in the world who are experts in deep learning.
Are they making it millions or billions off this approach.
You can ask yourself this question even without knowing deep learning.
This is just logical reasoning.
Thus we can assume that in reality something tricky is happening here.
Remember that once that forecasts are not really that useful.
What we would really like to do is forecast for multiple times steps even just two or three.
So let's see what happens.
So here's our multi-step forecast
and that's it for the second half of the dataset.
All right.
So now we can see what's happening when we do a multi-step forecast.
All we see is a pretty straight line.
What does that mean.
It means the model isn't really predicting the next value in the time series.
It's just copying the previous value.
And that actually makes a lot of sense for a time series.
Recall I said earlier that four images in time series signals this data is highly correlated.
If you're looking at a red car and you find a red pixel what are the neighboring pixel values.
Probably also red.
And if you're looking at a time series this time series is probably a smooth function over time.
And we can see that that's exactly the model we've created.
This is a smooth orange line it's not going to jump around from say minus one to plus two and back down
to minus three and so forth.
Now one thing that is nice about this last time I've run this example is that it did capture the trend
that is going to go up but other than that it didn't really do anything useful so it turns out that
this model is not really that great.
In fact it's not really doing much.