In this lecture, we are going to apply the whole winter's exponential smoothing model in code.
This is our final model for this group of models, the culmination of our study of exponential smoothing.
This lecture is going to walk you through a prepared CoLab 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 always, you can check the lectures, how to code by yourself and how to practice for a more in-depth
discussion.
If there's anything in this lecture you didn't understand or you think I missed a step or didn't explain
why we were doing something, please use the Q&A to inquire.
As usual, you can look at the title of the notebook to determine what notebook we are currently looking
at.
Let's start by importing the exponential smoothing class.
Next, we're going to create an instance of our model.
This time we're not going to bother fitting a model to the entire data set, but rather we're going
to skip right away to the train to split.
So the first variation we're going to try is the additive trend and additive seasonality, we set the
seasonal periods, the 12, since we know that the data cycles yearly and the frequency of the data
is monthly.
Next, we call the fit function.
So let's run this.
Next, we assign the fitted values to the whole Winters' column of our original data frame for the train
rose.
Next, we call the forecast function for and test the steps in a sign the results to our original data
frame for the test rose.
Next, we plot the whole winter's column along with the original data set.
So this is very encouraging, unlike the simple exponential smoothing model and the whole linear trend
model.
This model fits very well for both a train and test notice, importantly, that the predictions are
not lagging behind the Time series.
So hopefully now you are convinced that it is not correct to shift it backwards by one step.
And in fact, the lagging is only due to model mis specification.
Remember that you have to be consistent.
If you shift back for one model, you have to shift back for all of them.
And for this model, that would be very silly because then the predictions would be ahead of the data,
which doesn't make any sense.
Now that we know our model fits decently well, it would be a good idea to calculate some metrics.
Although there are many metrics which are used in time series analysis, the root mean squared error,
we'll be just fine for us.
This metric makes more sense, in my opinion, than something like the absolute error, because these
models actually minimize the squared error directly.
Therefore, it would make less sense to check the absolute error because we didn't optimize for that
in the first place.
If you're going to check the absolute error, then you may as well make the absolute error.
Your lost function in the root mean square.
There is just the square root of the square there, which puts it on the same scale as the original
data.
So minimizing the mean squared error is equivalent to minimizing the root mean squared error.
So here's how we calculate it.
We take why the predictions and t the targets, although it doesn't really matter which is which and
then we subtract them.
This does an element y subtraction.
Next we square the result.
This is also an element y's operation.
Next we call the mean.
So now we have the mean squared error.
Finally we take the square root so we get the root mean squared error or.
Now, just out of curiosity, I would also like to calculate the mean absolute error.
This calculation is a little simpler.
We just take the difference between why and t take the absolute value and then take the mean, hence
mean absolute error.
Next, we check the RNC on both the train and test set, as you recall, the train predictions are stored
in the fitted values attribute and the test predictions are made using the forecast function.
As you can see, I've already switched around, which is the prediction and which is the target, since
it doesn't really matter, which is why and which is t you will get the same answer either way.
All right, so the train pharmacy is about 11 something and the test pharmacy is about 17.
When we checked the mean absolute air, we get about nine something for train and 13 something for test.
Next, we're going to try the whole Winters' model again, but with different parameters.
Recall that it seems that the amplitude of the cycle increases with the level of the Time series.
This suggests that a multiplicative model may fit better.
So for this model, we're going to have an additive trend and a multiplicative seasonality.
Again, we call it a sign the trend predictions and the test predictions to the data frame and then
plot the result.
So we're just doing it all in one step, since you already know how each of these steps works.
So from my perspective, at least, this model appears to fit better than the purely additive model.
But let's check our accuracy metrics.
So when we check the pharmacy, we get about nine something for the train set and about 15 something
for the tests.
This gives us a better trainer and a better test there compared to the purely additive model.
Therefore, we would prefer this model.
If we check the mean absolute air, we see the same thing, both the trainer and the tester have improved.
Next, let's try Hault Winters again, but with the multiplicative trend and the multiplicative seasonality,
this kind of makes sense because the trend in the Time series is not exactly a straight line.
The growth actually seems to be accelerating over time.
Again, we call it a sign the predictions to our data frame and plot the result.
It's difficult to tell whether this is a better or worse fate, so let's check our accuracy metrics.
All right, so here we see an interesting pattern, the train arm is about nine something and the test
arm AC is about twenty five something.
So the trainer is better, but the test error is much worse.
We see the same thing for the mean absolute error, the train error has gone down, but the test there
has gone up to about 20 something.
Therefore, while this Time series may look like a multiplicative model is a better fit, it actually
overfit to the training data and we get a worse result.