When working with data — whether it’s sensor readings, financial time series, or other measurements — we often encounter noise. Noise is random fluctuations that obscure the true signal we’re trying to analyze. To extract meaningful information, we use smoothing and filtering techniques. One of the simplest and most effective methods is the exponential moving average (EMA). This article explains its fundamentals, how it works, and its practical applications.
Smoothing is a technique used to reduce noise in data while preserving the underlying trend or pattern. Imagine you’re tracking the temperature outside with a sensor. Due to environmental factors or sensor imperfections, the readings might fluctuate wildly from second to second. Smoothing helps you see the general temperature trend without getting distracted by these short-term variations.
The exponential moving average is particularly useful because it gives more weight to recent data points, making it responsive to changes while still smoothing out noise.
Before diving into the EMA, let’s consider a simpler method: the simple moving average (SMA). The SMA calculates the average of a fixed number of past data points.
A filter, in its simplest form, is a mathematical algorithm for this “smoothing.” The algorithm takes measured values of something as input and produces smoothed values as output. There are a vast number of such mechanisms, each with its own operating principle and area of application. For example, you can calculate the arithmetic mean over the last few measurements. In this case, the adjustable parameter of the filter is the so-called local window — the number of recent measurements used in the averaging to obtain the current value. For example, four:
If you have a time series $ x_1, x_2, \dots, x_n $, the SMA over a window of $ 4 $ points at time $ t $ is:
$$ X_t = \frac{x_t + x_{t-1} + x_{t-2} + x_{t-3}}{4} $$
Generalized formula for a local window $ n $:
$$ X_t = \frac{\sum_{i=0}^{n-1} x_{t-i}}{n} $$
While the SMA is easy to compute, it has a drawback: all points in the window are weighted equally, and older data has the same influence as newer data. This makes the SMA slow to react to sudden changes in the trend.
The exponential moving average (EMA) addresses this limitation by assigning exponentially decreasing weights to older data points. This means recent values have a stronger influence on the average, allowing the EMA to adapt more quickly to changes.
The EMA is defined recursively. For a time series $ x_t $, the EMA at time $ t $, denoted $ X_t $, is calculated as:
$$ X_t = \alpha \cdot x_t + (1 - \alpha) \cdot X_{t-1} $$
Where:
The smoothing factor $ \alpha $ determines how much weight is given to the current observation versus the historical average. A higher $ \alpha $ (closer to 1) makes the EMA more responsive to new data, while a lower $ \alpha $ (closer to 0) makes it smoother and less reactive.
To start the recursion, you need an initial value for $ X_0 $. A common choice is to set $ X_0 = x_1 $ (the first data point) or use the SMA over the first few points.
The smoothing factor $ \alpha $ can be related to the window size $ n $ of an SMA using the formula:
$$ \alpha = \frac{2}{n + 1} $$
For example, if you want the EMA to behave like an SMA with a 10-point window, you’d set $ \alpha = \frac{2}{10 + 1} = \frac{2}{11} \approx 0.18 $.
The EMA is widely used in:
It’s time to get hands-on with EMA filtering. In the program below, the red line represents the actual value. The white dots are the measured values of the actual quantity, which include some “error.” The green line shows the result of the filter’s work. The adjustable parameter “alpha” can be changed using the vertical slider on the right.
When alpha is close to 1, filtering is almost nonexistent: the result is mostly influenced by the latest measurement. As alpha decreases, you can observe the green line becoming smoother. However, as smoothness increases, the green line starts lagging behind the red line. This is called a phase shift. The stronger the filtering, the greater the phase shift.
There’s always a need to find a balance: a result that’s smooth enough while keeping the phase shift small enough. But if the measurement error is too high, achieving the desired compromise becomes impossible. You can easily see this by setting error = 1: the error becomes comparable to significant changes in the actual value.
A similar property is observed in mechanical damping. Although this process is much more complex due to natural frequencies and the physical properties of dampers, an analogy with the exponential filter can be drawn: the softer the damper (the smaller the alpha), the more vibrations are suppressed, but at the same time, significant changes in the system might be missed.