The world’s leading publication for data science, AI, and ML professionals.

Time Series From Scratch – Exponentially Weighted Moving Averages (EWMA) Theory and Implementation

EWMA is an improvement over simple moving averages. But is it enough for accurate forecasts?

Time Series from Scratch

Photo by Ryan Stone on Unsplash
Photo by Ryan Stone on Unsplash

The last article provided a theoretical and hands-on introduction to simple moving averages. We’ll spice things up today with its bigger brother – exponentially weighted moving averages.

Today’s article is structured identically, so it shouldn’t be challenging to follow. However, if you’re new to the concept of time series, this article isn’t a good place to start. It’s the 9th article in the series, and reading the series from the beginning is a far better approach. Links to all previous articles are at the end of this one.

The article is structured as follows:

  • Exponentially weighted moving averages – Theory and math
  • Exponentially weighted moving averages – Implementation in Pandas
  • Exponentially weighted moving averages – Forecasting
  • Conclusion

Exponentially weighted moving averages – Theory and math

Just like its dumber brother (MA), EWMA often isn’t used for forecasting. Still, EWMA is a synonym for first-order exponential smoothing – or simple exponential smoothing. The more complex members of the exponential smoothing family can work quite well in forecasting, so it’s necessary to understand EWMA first.

In a nutshell, EWMA applies weights to the values of a time series. More weight is applied to more recent data points, making them more relevant for future forecasts. That’s the most significant improvement over simple moving averages.

Here’s the most generic EWMA formula:

Image 1 - Generic EWMA formula (image by author)
Image 1 – Generic EWMA formula (image by author)

w denotes the applied weight, x is the input value, and y is the output.

How you’ll define the weight term depends on the value of the adjust parameter. When set to True, the weights are calculated by dividing with the decaying adjustment factor in beginning periods to account for imbalance in relative weightings (source). This parameter is True by default, and if left unchanged, the weights are calculated with the following formula:

Image 2 - EWMA weight calculation when adjust=True (image by author)
Image 2 – EWMA weight calculation when adjust=True (image by author)

The only new parameter here is alpha, and it denotes the value of the smoothing factor.

When adjust=False, the formula for weight calculation is much simpler:

Image 3 - EWMA weight calculation when adjust=False (image by author)
Image 3 – EWMA weight calculation when adjust=False (image by author)

There are other ways to approach the calculations, such as through the center of mass, span, and halflife parameters, but we’ll pass the smoothing factor value directly to keep things simple.

Let’s take a look at an example. x represents a sample time series without the time information, and alpha is the smoothing factor. We’ll calculate the EWMA values with and without adjustment.

Here’s the calculation without adjustment:

Image 4 - EWMA step-by-step calculation without adjustment (image by author)
Image 4 – EWMA step-by-step calculation without adjustment (image by author)

As you can see, the first data point is copied, and the rest were calculated through the simple formula discussed earlier.

Here’s the calculation with adjustment:

Image 5 - EWMA step-by-step calculation with adjustment (image by author)
Image 5 – EWMA step-by-step calculation with adjustment (image by author)

Not all values were calculated because the calculations get longer with every time step. Still, these few alone should make the picture clear.

But here’s the good news – you don’t need to worry about the alpha parameter! Pandas can calculate it automatically if you provide a value for the span parameter – which is interpreted as an n-day moving average.

If working with monthly data, you might want to select a single year (12 periods) as a span value. Then, the alpha is calculated as follows:

Image 6 - Alpha parameter calculation through span (image by author)
Image 6 – Alpha parameter calculation through span (image by author)

All in all, it’s a good way to avoid guessing the optimal smoothing parameter value.

Let’s see how to implement all of this in Python next.


Exponentially weighted moving averages – Implementation in Pandas

You’ll use the Airline passengers dataset. Here’s how to load it into Python and visualize it:

Here’s how the dataset looks like:

Image 7 - Airline passengers dataset (image by author)
Image 7 – Airline passengers dataset (image by author)

You can use the ewm() function in Pandas to calculate exponentially weighted moving averages. You can pass the smoothing value directly through alpha or make your life easier with the span parameter. Both should sound familiar by now.

The code snippet below calculates unadjusted exponentially weighted moving averages with a span value of 3, 6, and 12, and shows them visually:

Here’s how all time series look like:

Image 8 - Exponentially weighted moving averages with Pandas (image by author)
Image 8 – Exponentially weighted moving averages with Pandas (image by author)

As you can see, EWMA doesn’t suffer from the lag problem as MA. Still, it isn’t the best forecasting algorithm, as you’ll see shortly. You can’t use Pandas to forecast the future. Statsmodels is your friend.

Let’s cover the forecasting next.


Exponentially weighted moving averages – Forecasting

Here’s a short recap. EWMA provides an improvement over MA but still isn’t the best forecasting algorithm. EWMA and SES (Simple Exponential Smoothing) are synonyms, and the entire exponential smoothing family of algorithms is decent for forecasting. The thing is, you’ll need more than a single smoothing parameter (alpha) to capture both trend and seasonality.

The SimpleExpSmoothing algorithm is built into the statsmodels library. We’ll use it for forecasting. But before, we’ll split the dataset into training and testing subsets. The last two years (24 rows) are used for testing. The results are visualized after the training:

Here’s the visualization:

Image 9 - Forecasting with exponentially weighted moving averages (image by author)
Image 9 – Forecasting with exponentially weighted moving averages (image by author)

What gives? As said earlier, EWMA isn’t the best forecasting algorithm. One parameter (alpha) isn’t enough to capture both trend and seasonality. The forecasts are still more reasonable than the ones given by a simple moving average model.

You could try to make the series stationary and retrain the model, but you won’t get better results.

Double and triple exponential smoothing algorithms will provide more accurate predictions, and you’ll learn about these in the following article.


Final words

To summarize, weighting data points does provide an improvement over simple moving averages, but a single model parameter isn’t enough for accurate forecasts – especially if the data shows both trend and seasonality.

Higher-order exponential smoothing algorithms are the logical next step, and you’ll learn their ins and outs in the following article, so stay tuned.

Thanks for reading.


Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.

Join Medium with my referral link – Dario Radečić


Read the entire series

  1. Seeing the Big Picture
  2. Introduction to Time Series with Pandas
  3. White Noise and Random Walk
  4. Decomposing Time Series Data
  5. Autocorrelation and Partial Autocorrelation
  6. Stationarity Tests and Automation
  7. Train/Test Splits and Evaluation Metrics
  8. Moving Averages (MA) Theory and Implementation

Stay connected


Related Articles