In this lecture, we are going to discuss forecasting, which will lead us into our next coning example.
This lecture will focus on how to do a correct forecast because unfortunately, a lot of resources do
this in a way that makes your results look really nice, but actually make zero sense.
So first, what does it mean to forecast in the way that we mean in this course?
It means that we have a time series and our job is to predict the next values of this Time series.
Importantly, notice I use the plural there.
We want to predict multiple values, not just one value.
The length of time that you want to predict is called the horizon, in some applications, your horizon
might be three days or five days.
For example, if you're trying to predict the demand for each product at a factory, you might want
to forecast the demand over the next three to five days.
Another pretty obvious example is the weather.
Let's say we want to predict the hourly weather over the next seven days or so.
So if we predict the weather for each hour over seven days, that's seven times 24, which is one hundred
and sixty eight hours.
So if your sequence step size is ours, that would be predicting one hundred sixty eight steps ahead.
This is important.
So remember, you don't want to just predict one step ahead as a side note.
Forecasting is a huge topic.
You could study an entire book about forecasting.
This course is not that we're just touching on specific points which are relevant to deep learning and
Arnon's.
Now, you might recognize that this section is all about own ends, recurrent neural networks.
So where are the Ahran ends?
Well, we are not there yet.
A common approach in machine learning, especially in the industry, is to do the simplest thing possible.
Don't throw an R.N. in at a problem if there is a simpler method available.
So my question for you now is what is the simplest way to do a forecast given a single one dimensional
time series?
Well, how about we go back to our approach at the very beginning of this course, linear regression,
linear regression is in fact one of the best models to use to study forecasting because it's very intuitive
and there's not that much going on in terms of formulas and manipulating equations.
At this point, you might raise an objection if our data is of shape and by t by deed.
We can't use linear regression because linear regression works on 2D rectangles of shape and by D.
And if you were thinking this, that's a great sign.
That means you're thinking of our shapes.
Which is exactly what you should be doing.
So how can we reconcile this apparent contradiction?
Well, in fact, linear regression does only work on data of shape and body.
But remember that in our example, we are considering only one dimensional time series.
So B equals one.
As you know, this is just a superfluous dimension.
We can represent the exact same data in a two dimensional array of shape and by T.
Again, this is analogous to the situation where you have an end length, one dimensional array.
Or you can have the same array stored in a two dimensional and by one object.
So how do we use linear regression in this situation?
The answer is we pretend that T is D or to put that a different way.
In this scenario, linear regression treats T as if it were D.
This is another instance of my rule.
All data is the same.
Let's think about how our data will look.
Let's suppose we have a time series of length 10.
And we want to train a model to predict the next value in the Time series using the past three time
steps.
In this case, our input matrix will be of size end by three.
And our target vector will be of size N.
Now, what is n as mentioned previously, we can calculate N as the number of length three windows that
fit into the Time series, which is length 10.
So that's ten minus three, plus one, which is eight.
But wait.
Remember that we would like to be able to predict the next value, which is the target.
So while we can fit eight windows into the Time series, we can't actually use the last window in our
training set because we don't know the next value.
Suppose our data is just x 16 to all the way up to x 10.
In this case, if our input is X eight, x nine and 10, we can't have X eleven as a target because
we don't have X eleven in our time series.
So in actuality, we only have N equals seven.
So really, it's like asking how many windows of length for fit into the time series of length.
Ten.
That's ten minus four plus one which is seven.
And this is because the fourth value is the target.
So what does a linear regression forecasting model look like at this point?
It should be pretty clear it's ex hat a time T equal to W zero plus W one times X at time, T minus
one plus W two times X at T, minus two plus W, three times X at T minus three.
Well, I'm using time indices here.
You should still be able to recognize this as linear regression, as a side note.
This is called an auto regressive or a car model.
In statistics, nomic, later, the word auto means self.
So it's a model that tries to predict the value in a time series using its own values.
OK, so using this model, how do we forecast?
Here's a common thing I see a lot.
Let's say our test set consists of the data X eleven up to x 20.
And we want to calculate the test accuracy on our test set.
So you might say take the data matrix that we see here, X say X nine, 10, x nine, x 10x eleven and
so on.
And plug that into model that predicts.
Then we'll get the predictions for X eleven X twelve x 13 and so on.
Unfortunately, this is wrong.
As a side note, please be aware that models and pi torture do not have a predict function.
We're just using this notation for convenience since it allows us to express how to make the model predictions
using one line of code.
You should recognize this notation from psyche.
Learn so you can just frame this lecture in the context of using a psyche learn model rather than pi
torch.
So why is it wrong?
Remember that we would like to use our forecasting model to predict multiple steps ahead.
What we just saw was only predicting one step ahead.
This is not good enough.
Let's say we want to predict three days ahead.
We want to use they one day to day three to predict that day four, day five and day six.
What were we doing before we used day one, day two and day three to predict day four?
That's fine.
Then we used day to day three and day four to predict day five.
That's not fine because remember, we don't know the value on day four yet.
Today is only day three.
Similarly, we can't use day three, day four, day five to predict day six.
So what do we do instead?
If we want to make predictions multiple steps into the future, we must use our own earlier predictions
as input, for example, in order to predict X five.
We use X two, X three and X hat for X hat for was calculated from X one X to an X three.
To predict, execs will use X three X hat for an X hat five.
This is the correct way to forecast.
Now, note that this doesn't mean one step ahead.
Predictions aren't useful.
It just means you have to know when to use and when not to use them.
Because we have to make predictions sequentially.
We can't do it all in one call of model to predict it like we did before.
Instead, we need to use a loop and call model, DOT predicts only on the most recently generated sequence.
And this pseudocode, I'll attempt to express the main idea, our first sequence X we initialize to
the last values of the train set from which we want to predict the first value of the test set.
We also initialize an empty list to store our predictions.
Then we enter a loop that goes for the number of steps we want to forecast inside the loop.
We call model to predict to get only the next immediate value.
We store this in our predictions list then.
And this is the important part.
We discard the all this value index.
And we can cartney the rest of X with the new prediction.
We assign that to X.
Then when we call model to predict on the next round, we are using an X that contains a both true values
and predictions for the forecasted values.
As a final note in this lecture, I want you to think about how can we apply the rule?
All machine learning interfaces are the same in order to build a more powerful predictor.
One limitation of linear regression is that the prediction can only be a linear function of its inputs.
Luckily, we already know how to build a more powerful model that works on the same kind of data and
CNN.
So by being an expert in deep learning, you can immediately apply this technique without any extra
effort.
That's a pretty powerful approach.
All of a sudden, you are able to convert your classic statistical time series forecasting model into
a modern superpower to nonlinear neural network forecaster.
And that's all just with one line of code.
Not bad.