
Moving averages are a fundamental tool in time series analysis and they are widely employed for various purposes. Some of the most popular applications of moving averages in time series analysis include:
- Noise Reduction: Moving averages effectively filter out random fluctuations and noise in time series data. Smoothing with moving averages helps analysts focus on underlying patterns rather than short-term fluctuations, aiding them in identifying meaningful trends.
- Seasonal Decomposition: Decomposing time series data into its components often involves the use of moving averages. Seasonal decomposition techniques utilize moving averages to extract seasonal patterns from the data. Additionally, moving averages can help identify cyclic components in time series data, representing repeating patterns that are not strictly seasonal.
- Forecasting: Moving averages are a fundamental component of various forecasting models.
- Filtering Outliers: Moving averages can be used to identify and filter out outliers in time series data. Unusual data points that deviate significantly from the moving average may be considered outliers and warrant further investigation.
- Smoothing for Visualization: When plotting time series data, moving averages are often employed to create smoother, cleaner visualizations. This can make it easier to communicate trends and patterns to a broader audience.
These applications demonstrate the versatility of moving averages in Time Series Analysis. Depending on the specific goals and characteristics of the time series data, we might choose various types of moving averages and parameters. While the concept of moving averages might seem quite simple, it is easy to become overwhelmed by the available parameters and options. That’s why in this article, we will explore them in detail.
Simple vs. Exponentially Weighted Moving Averages
Moving Average (MA) and Exponentially Weighted Moving Average (EWMA) are two of the most popular types of moving averages. The key difference between them lies in how they assign weights to data points.
MA calculates the average of a fixed number of recent data points, assigning equal weight to each of them. This means that all data points in the window have the same influence on the moving average, and older data points are treated as equally significant as the most recent ones.
On the other hand, EWMA is a type of weighted moving average that assigns exponentially decreasing weights to data points as they go further back in time. As such, more recent data points receive higher weights, making them more influential in the calculation of the moving average. This approach allows EWMA to be more responsive to recent changes in the data, making it particularly useful for tracking short-term trends and reacting quickly to shifts in the time series.
Setup and data
We start by importing the libraries.
Next, we generate synthetic time series data with the following characteristics:
- A duration of one year with daily observations
- Presence of recurring patterns (seasonality) within the time series
- Absence of any underlying trend
- A notable spike occurring between June 18th and June 23rd
- The introduction of random noise as the final step

Simple Moving Average
While working with Simple Moving Averages, we typically focus on two parameters: the size of the window and whether the window is centered or not.
The difference between centered and uncentered moving averages lies in where the moving average is positioned with respect to the data points it averages.
In a centered moving average, the average is calculated for a window of data points that is centered on the data point of interest. Typically, an odd number of data points is used in the window. For example, with a centered 3-day moving average, the average is computed for each data point using the current day and the one day before and after it.
In an uncentered moving average, the average is calculated for a window of data points that moves forward in time, but the window is not centered on the data point of interest. For example, to calculate a 3-day uncentered moving average on day t, we would take the average of days t-2, t-1, and t.
In the following snippet, we calculate a 7-day moving average using both the centered and uncentered approaches.

We can clearly see the differences between the two approaches, especially around the peak in June.
For the centered MA, the peaks and valleys in the smoothed curve align more closely with the midpoint of corresponding peaks and valleys in the data.
The uncentered MA lags behind the data points, meaning it follows the data’s general trend but does not respond as quickly to short-term fluctuations. As such, peaks and valleys in the moving average curve are offset in time relative to the peaks and valleys in the data.
In the next snippet, we generate moving averages using varying window sizes. The general pattern that can be observed in the following figure is that the larger the window size, the less sensitive the moving average is to short-term fluctuations. Curves created with larger window sizes are smoother and tend to filter out short-term noise, highlighting longer-term trends.

The last thing to remember is that by default, we need X observations to create an X-period moving average. That is why the 180-day moving average only starts in the middle of the year. We can adjust that behavior using the min_periods
argument of the rolling
method.
Exponentially Weighted Moving Average
As we have already mentioned, EWMA assigns exponentially decreasing weights to data points, giving more weight to recent observations and less to older ones. This makes EWMA more responsive to recent changes in the data compared to the Simple Moving Average.
The formula for the Exponentially Weighted Moving Average (EWMA) using the smoothing parameter alpha is typically expressed using the following recursive representation:

where x_t is the value at time t, and α is the smoothing factor, which controls the weighting of the most recent observation in the moving average.
Before we delve into various methods for selecting the smoothing parameter, let’s clear up a common source of confusion: the adjusted versus unadjusted formula. This choice is determined by the adjust
argument in the ewm
method in pandas
. Below, you can find both formulas for your reference.

The purpose of the adjustment is to address any imbalance in the relative weightings by dividing by the decaying smoothing factor during the initial periods.
Mathematical detail: **** In the adjusted formula, the denominator represents a geometric series. Therefore, under the assumption of an infinitely long time series, both formulas yield the same result, that is, the unadjusted value.
To illustrate the practical difference, we create two EWMA lines using identical smoothing parameters. The distinction lies in the adjustment applied to the formula. Moreover, we narrow our focus to the first month since that’s where the distinction is most noticeable, albeit quite minor.

As the next step, let’s dive a bit deeper into the relationship between the value of alpha and how quickly the weight decreases in calculating the moving average. To calculate the subsequent weights, we use the formula coming from the adjusted variant of EWMA.

In the plot, we observe that a smaller alpha results in a slower decay of the weights. When alpha is at its extreme value of 0.99, the EWMA essentially clings to the most recent data point and neglects the older ones.
Next, let’s visualize the EWMA curves using three different alpha values: 0.1, 0.4, and 0.7.

We can observe the previously identified pattern in action: the higher the alpha, the more responsive the curve becomes to recent data points. This behavior occurs because higher alpha values place greater emphasis on recent observations, causing the curve to closely follow data fluctuations. Conversely, lower alpha values give more weight to historical observations, resulting in a smoother curve that lags behind the data’s fluctuations.
Another layer of complexity in using Exponentially Weighted Moving Averages comes into play when choosing the parameter responsible for exponential decay. While we’ve already discussed the alpha parameter, it’s not the only option. Below, we describe the remaining three parameters while offering some intuition for selecting the right value:
- Half-life (
halflife
) -The half-life is essentially the time required for the decaying quantity to be reduced to half of its initial value. A smaller half-life makes recent observations more influential, thus making the EWMA more responsive to short-term changes. Conversely, a larger half-life places more weight on older data, leading to a smoother and slower-changing EWMA. - Span (
span
) – The span signifies the effective window size or the number of periods considered in the EWMA calculation. In other words, it indicates how many past observations contribute to the smoothing process. A larger span includes more historical data, resulting in a smoother and slower-changing EWMA. Conversely, a smaller span focuses on recent data, making it more responsive to short-term variations. - Center of Mass (
com
) – Similar to span, the center of mass parameter defines a relative position within the span. It indicates where the majority of the weighting function’s mass lies. A center of mass of 0.5 corresponds to the midpoint of the span, giving equal weight to observations on both sides. Adjusting the center of mass allows us to bias the weighting toward either recent or older data points. A smallercom
shifts the center of mass closer to the present, emphasizing recent data and increasing sensitivity to short-term fluctuations. On the other hand, a largercom
shifts the center of mass toward the past, resulting in a smoother and slower-changing EWMA.
The following formula illustrates the relationship between these three alternative parameters and the smoothing factor alpha.

To better understand the concept, let’s consider a scenario where we are dealing with quarterly data. In our Exponentially Weighted Moving Average, we want to incorporate the data from the past year. To achieve this, we opt for a span of 4, which corresponds to an alpha value of 0.4. Below, you’ll find a table with some illustrative values.
Lastly, let’s take a look at the half-life parameter. We have already mentioned that it corresponds to the number of time periods in which a quantity will be reduced to half of its initial value. Let’s consider a half-life of 10, which corresponds to alpha = 0.066967. If we calculate the following weights using that alpha, we can clearly see that the weight for the 10th observation is equal to 0.5, as we expected.

Wrapping up
- Moving averages serve several purposes in time series analysis, such as noise reduction, seasonal decomposition, forecasting, outlier filtering, and creating smoother visualizations.
- The simple moving average assigns equal weight to observations from both the distant and recent past.
- In contrast, the exponentially weighted moving average reacts more quickly to recent data by giving it higher importance. We can fine-tune the level of smoothing using one of the following parameters: smoothing factor (alpha), half-life, span, or center of mass.
You can find the code used in this post here. As always, any constructive feedback is more than welcome. You can reach out to me on LinkedIn, Twitter or in the comments.
Liked the article? Become a Medium member to continue learning by reading without limits. If you use this link to become a member, you will support me at no extra cost to you. Thanks in advance and see you around!
You might also be interested in one of the following:
A Comprehensive Guide on Interaction Terms in Time Series Forecasting
Unlocking the Power of Interaction Terms in Linear Regression
Metrics Layer: A Single Source of Truth for All KPI Definitions
Further reading
- https://gregorygundersen.com/blog/2022/05/17/exponential-decay/
- https://gregorygundersen.com/blog/2022/06/04/moving-averages/
- https://pandas.pydata.org/docs/user_guide/window.html#window-exponentially-weighted
All images, unless noted otherwise, are by the author (and potentially some AI).