Finally we're going to move on to the next example where we see how to actually build this model right.
By the way there isn't really anything wrong with the second model.
It just wasn't a good model.
This is unlike the first model where there actually were things wrong with it because it gave us misleading
results and also predicts the wrong thing.
And this final example we are going to make use of all the data open close high and low prices as well
as the volume.
So we are going to have a t by D input where t equals 10 and d equals five.
And from this we are not going to try and predict a return.
Instead we turned this into the simplest problem possible.
Just try to predict whether the price will go up or down.
In other words binary classification if you've worked in machine learning long enough you should have
some intuition about this.
Regression is in general a harder problem than classification with regression.
You have to predict a continuous value.
You can't be too high or too low.
You have to be right on the nose classification is easier especially binary classification.
You don't have to predict the exact number.
You just have to predict a label.
In this case you only have two choices up or down.
So this is what we are usually trying to predict when it comes to stock returns.
Let's get back to the code.
First we are going to start by getting all the data into no higher rates so the input data is going
to be made up of the open high low and close prices as well as the volume.
The target is going to be based on the return.
Next we are going to set an T and D.
Since our data is of shape n by t by D.
T is simple we can just choose that.
So let's stick with 10 although in the future you may want to try different values like 20 30 40 50
and so on these is also simple.
It's just the number of columns in the input data and is a little tricky.
It's the length of the series minus Big T although this should remind you of each of our time series
examples where we did the same thing.
We just never calculated an explicitly
next we're going to normalize the inputs.
This time we're going to make it a little easier on our model instead of the first half being the train
set in the second half being the test set.
Let's make the first two thirds the train set and the last third the test set so as usual we're going
to normalize our data.
This is especially important now since the volume column is very high compared to the price columns.
Next we create our train set X train is of shape entrained by t by D and Y train is of shape and train
then we do a loop where t counts up from zero up to end train and we populate X train and Y train for
x we take the input data from Little T up to a little T plus big T for Y.
We have a boolean which is true if the return at Little T plus big T is bigger than 0
next we create the test set.
As you might expect and test is an A minus N train so x test is of size and a minus and train by t by
D and Y test is a shape and a minus and train.
Next we enter a loop counting from zero up to and minus and train or equivalently and test inside the
loop.
We actually have two indices you refers to the index of X test and Y tests which of course will start
from zero but remember we are indexing our original dataset which must be offset by and train.
So t is equal to you plus and train and we use t to index the original dataset whereas we use you to
index x test and Y test
once we're done that we can make our own n.
Importantly remember that we are now doing binary classification what this means is that our model outputs
are still the same because no matter what our final layer will be a linear.
What makes it different is the lost function I lost is now the binary cross entropy so let's run this
and train our model
that we train for quite a while 300 epochs
so what do we see here.
We see that the train loss goes down a little bit but the validation loss goes up a lot.
So what this tells us is that the model is just again over fitting to the noise in the train set
so that's what it means when the train lost goes down.
But the validation loss goes up
if we look at the accuracy we see the same thing.
The train accuracy goes up but the test accuracy stays at 50 percent.
Now you might ask why doesn't the test accuracy go down.
One important thing to realize about binary classification is that zero accuracy is not the worst accuracy
if your accuracy is zero.
That means you can just reverse all your predictions and you would get one hundred percent.
That would be a perfect model.
In other words for binary classification the worst accuracy is actually 50 percent meaning it's no better
than random guessing.
That's why the test accuracy remains at 50 percent as the model over fits.
All right so that's the end of this script
let's now discuss and summarize everything we just did since it was probably a lot to take in we just
went through three examples of predicting stock returns in the first example.
We used an Alice team to predict future stock prices from past stock prices.
While this appeared to work well with the ones that forecast it didn't work so well for multi-step forecasts
we saw that the model was likely just remembering the previous value and making that prediction.
This actually makes sense in the context of our second example as well where we tried to predict stock
return.
In this example we saw that we couldn't get the last to decrease that much.
The third example we did was to consider all the data not just the previous closed prices and to turn
our model into a binary classifier we saw that even on the simplest possible task with the most data
the models still could not achieve better results than random guessing what this tells us is that there
isn't any hope of our first two models working in the first place.
Imagine if you can't even predict whether the stock price will go up or down.
How could you ever predict the numerical value of the return or the next stock price itself.
In other words if you can't even do the easy task then surely the harder task is also not going to be
solved
so be suspicious whenever you encounter an almost perfectly accurate stock price forecast.
If we can't even do binary classification then you can be sure that forecasting the actual price will
not be so simple by the way these results should not be surprising.
I asked you earlier to keep in mind the previous examples we did in this section if you recall we had
trouble getting an orange end to predict a noisy sine wave.
The underlying function in a noisy sine wave is a sine wave.
It's a deterministic signal you can imagine that if we have problems even with that then in comparison
stock prices should be a huge problem
the major problem with trying to predict stock prices from stock prices is that it's a fundamentally
flawed approach.
These approaches don't take into account data about the world itself.
Why does a stock price go up or down.
Many times it's due to some occurrence in the real world.
Imagine how ludicrous it would be to say here is some historical stock prices from this.
The future is totally deterministic.
We can predict everything that will happen now.
That is a pretty crazy line of thought.
Now consider a more realistic scenario where stock prices depend on the emotions of investors that could
be based on things like how the company is being portrayed in the media whether the company has invented
something new and so on.
Even without knowing about deep learning there should be enough to make you suspicious of any course
that claims to be able to use LSD Ms to predict stock prices using historical data.
And yes that's what a lot of people are doing out there forecasting stock prices based on a time series
alone
instead we can think about what actually might cause a stock price to go up or down.
We see examples of this all the time.
For example when Facebook is being investigated by the government that causes its stock price to go
down.
When Elon Musk tweets while intoxicated that makes his company's stock price go down.
But when someone at Space X invents a new kind of rocket technology that makes the stock price go up
imagine what you are saying.
If you try to use historical prices alone to try and predict future stock prices that's like saying
using historical stock prices you think you can predict whether Facebook will be investigated or fined
by the government.
That Elon Musk might make a stupid tweet or that some rocket scientists will invent something to believe
that you could predict such events from looking at stock prices is surely a laughable idea.
That's like saying different events that happen in the world are preordained by stock prices.
Now that is crazy.
If you really want to learn how to do good stock market predictions that could be a course all by itself.
Never mind a lifetime of study finally.
This clearly isn't to say that unless teams are not good unless teams have proven themselves time and
time again on tasks such as language modeling and machine translation We've already seen how else teams
can be used for image classification which is an application you probably hadn't even considered.
So I hope this lecture helps you understand sort of a bigger picture around deep learning and how to
choose the right resources to learn from.
There are a lot of market is out there trying to get your attention.
So try not to fall into their trap.