This is business analytics using forecasting. And I'm Galit Shmueli. In the last video, we looked at how to use a moving average for visualizing a time series.
Now we're going to look at how to use a moving average for forecasting. Recall that the moving average method is based on a very simple idea. The idea is to forecast future values by using an average of several past points.
We talked about using it for visualization. And in this video, we're talking about forecasting. This is a very simple and popular forecasting method.
And again, we're going to focus on a width of a window as the key concept. Remember that we talked about two types of windows, a centered window and a trailing window. These two windows differ by the location that they are placed at.
For visualization, we use the centered moving average, which uses data from before and after the time period of interest. For forecasting, we'll use a trailing window, which only uses data before the period that we're forecasting. Let's see how to compute a trailing moving average.
First, we choose our window width. The window width will depend on different patterns in the series and on how much we want to smooth. Then, for a moving average at time t, we're going to place our window on time points t and back.
Lastly, we're going to compute the average of values in the window. So our formula is simply the average of the values in the window divided by the window width. In short, to forecast the time at t plus k given data until time t, we're going to use a trailing moving average that ends at time t.
And this is the formula that we see right here. Notice that it doesn't matter how far into the future you're going to forecast. Your number is going to be the same number.
While you can easily do moving average computations manually, it's useful to use software in order to avoid errors and also to get performance metrics for the training and validation periods. Here's how you would perform moving average forecasting with XLMiner or with R. If you're using XLMiner, go to the Time Series area, to the Smoothing function, and choose Moving Average.
If you're using R, then use the library(zoo), and you can find here code that uses the function rollmean. This will give you a trailing moving average for the period that you're interested in. Let's return to the example of the quarterly soft drink sales that we saw earlier.
We're going to start by partitioning our series and suppose that our goal is to generate forecasts for the next four quarters. So our validation period is going to be four months in this example. In XLMinor, we use the data partitioning option in the Time Series functions.
Then we will specify the last four rows as a validation set. Or, if you're using R, here is simple code to achieve the partitioning. The first line defines the time series object soda.
ts. The next two lines define the exact training and validation periods. Now we're ready to start creating forecasts.
We'll use only the training period and generate forecasts for the validation period. Here's the result of generating forecasts with a moving average. Focusing on the goal of forecasting sales in the next four quarters, we use the moving average with window four and recall that our validation set has four quarters.
This setup will allow us to evaluate performance for a horizon of four quarters ahead. The first moving average value is for the fifth quarter-- quarter one of 1987. The moving average forecast of $2,167 million US is the average of the four most recent sales figures.
In the same way, we can forecast sales for the next quarter by shifting our window one quarter ahead, and so on and so forth until the last quarter in our training period. To generate forecasts for the validation period, we can only use data until quarter one of 1989, which is the end of the training period. Therefore our forecast for each of the validation periods is the same number.
It's the average of the last four quarters in the training period. Since we have forecasts, we can now compute forecast errors. These are the differences between the sales values and their forecasts.
The forecast errors will help us evaluate the performance of the moving average forecaster. We can compute metrics such as MAPE, MAD, and MSE for the training period and separately for the validation period. We learn, for example, that the mean percentage error in both training and validation periods is around 8 or 8 and 1/2%.
But we can get a much better understanding by looking at the performance charts. Here we have the actual and forecast chart, and below it, the forecast errors chart. The gray band indicates the validation period.
And we can see the fixed forecast in this validation period. See the horizontal red line. We observe that the moving average is completely missing out on the seasonal fluctuations.
And it seems to lag behind in terms of the trend, under-forecasting when the series is increasing. This lagging behind occurs because we are using the most recent values, which tells us where the level was, but it doesn't tell us where the level is going. If the trend is increasing, then we're not accounting for future increases.
In short, the moving average here is not capturing seasonality, and even the trend is not properly captured. Let's look at another example. This time, a monthly series of ridership on Amtrak trains.
Our validation period here was 12 months. And like the previous example, it's fixed. It's not a role forward application.
Here we're going to use a window of width 12 months. We compute the moving average by averaging the most recent 12 values of ridership. For example, our first forecast is 1,809 thousands of passengers.
And the associated forecast error is negative 194. 7, indicating an over forecast of almost 200,000 passengers. From the performance charts, we again see that trend and seasonality are not captured.
We can also look at the standard performance metrics, like MAPE, MAD, and MSE. But notice how much more informative the performance charts are. So here too we conclude that the moving average forecaster is missing two main patterns in the data, seasonality and trend.
When we use the forecasting method, we always want to ask ourself, what is the assumption behind this model? And when we're using a moving average, our assumption is what we call stationarity. What it means is that we're assuming that the future is very similar to the past that we're seeing-- not necessarily the same numbers, but in a probabilistic sense.
Since we're extrapolating from the past to the future, we're making this assumption when we're using a moving average. What happens if this assumption is violated? How will the moving average work?
Is it going to help us if we actually have a longer history with more data? The answer is going to be no. If the assumption is violated, we really don't know how well this forecaster is going to work.
So, just remember that this is a hidden assumption when we're using a moving average. Let's come back to the notion of a window width. And this is the parameter that the user can set.
The window width is the amount of smoothing that we're going to apply. To figure out what it means, think of the two extremes. One extreme is the longest window possible.
How long would that be? Well, we have the whole training period, so the longest window would be the length of the training period. If we do that, and we take an average of all the points in our training period, it's just a simple average.
And that's what we call over-smoothing. The other extreme is taking the shortest possible window. And that would be a window of width 1.
In that case, you actually get the same original series. So you haven't performed any smoothing. If we're going to use this for forecasting, then the moving average is essentially equal to the naive forecast.
This is what we call under-smoothing. So, choosing a window width is somewhat of a balance between under and over-smoothing. You might also try different window widths that will give you different forecasts.
You can compare them and choose a window width that seems to perform better. The bottom line with a moving average forecaster. The moving average takes an average of the most recent points.
A moving average can be used for two things. It can be used for visualization and it can be used for forecasting. For visualization, we use a centered window.
And the moving average chart will highlight the level and the trend by suppressing error and seasonality. We can also use the moving average for forecasting, in which case we use a trailing window. Remember that it does not capture seasonality or trend, so it's better to use it only on series that do not have that component.
And lastly, when we talk about window width, we have to think about over-smoothing versus under-smoothing. Ask yourself whether there are patterns in the data that you're trying to suppress. And if not, then try a few different types of widths.
With this, we conclude the discussion of the moving average. We're going to talk next about an operation called differencing.