Forecasting Time Series Data

Gaurang Mehra
4 min readMay 6, 2022

--

(Part 1-: Moving Averages and Holt winters)

Series Scope- How would you forecast a variable into the future given data over the past n periods. This problem arises in business when we are trying to predict demand for a product given past data with some trend or seasonality. The series is a 3part series. We will look at the following methods, with their pitfalls

  • Part 1- Holt Winters and Moving Averages
  • Part2- ARMA/ARIMA Models
  • Part 3- SARIMA/SARIMAX models

Current Article Scope

  • Understand how the FRED Industrial production Electric and Gas utilities index varies with time. Decompose it into its trend, seasonality and error components
  • Build models to predict the value of the index into the future.
  • Compare and visualize the accuracy of the models on unseen test data.

Loading processing the data and understanding any trends and seasonality

In this step we load the data, set a date time index and rename energy index column with a more descriptive name rather than the code that the FED uses.

Code 1.1-: Data Import and Set up

Next we plot the data to infer some general trends. Code below

Code 1.2-: code to plot the energy index over time
Exhibit 1.1-: Energy Index Trend over time since 1990
  • The data is definitely showing an increasing trend over time. We need to understand the nature of this trend over time whether additive or multiplicative/exponential
  • The data seems to have regular peaks and troughs suggesting seasonality.

Lets decompose the series into its components (trend, seasonality and error) to understand clearly

Exhibit 1.2- Seasonal Decompose

A few key observations

  • Clearly there is a trend. The trend looks additive rather than multiplicative. The trend also looks like it is damped in the outer years, in other words the trend flattens out in the future.
  • There is seasonality. Its is significant relative to the scale of the data. The data is on the scale of 50 to 80 and the seasonality varies from -10 to +15. This is significant and any model we choose should have a seasonality component

Splitting the data into train and test sets and fitting to the models

We use simple exponential smoothing or a simple exponentially weighted moving average (SMA)model to make predictions on the test set. The weights applied to this moving average exponentially decay the further back you go in time. In other words older data has less weight than newer data

We will also fit the data to a Holt winters model with trend and seasonality components. As we saw in the decomposition the trend is additive and damped in the outer years. The seasonality component is also additive. This means that the peaks and troughs of the seasonality are not increasing over time but varying in a constant range

Lets now visualize the predictions on the train and the test set.

Exhibit 1.3-: Models Comparison to actuals
  • Based on the graph, looks like both the models provide a good fit on the training data.
  • On the test data it looks like the Holt Winters model with trend and seasonality follows the actuals much more closely than the SMA (Simple weighted Moving Average)
  • In fact it looks like the Simple weighted Moving Average is probably useful for predicting the next time period but quickly converges to a single value further out that you go in time. In other words it fits poorly to unseen data.

Lets see this more clearly by zooming into the test set and looking at the predictions on the test set. To make the trends clearer we have resampled the data at a quarterly level and seen how well the 2 models fit on average

Exhibit 1.4-: Zooming in on test data to see model fit
  • As we can see the Simple weighted Moving Average without trend and seasonality quickly converges to a single value of around ~100
  • The Holt Winters Exponential smoothing (EMA) does a better job of following the ups and downs in the actual data.

--

--

Gaurang Mehra

Deeply interested in Data Science, AI and using these tools to solve business problems.