TL;DR
An exponential moving average (EMA) is a running estimate of a stream of values. Each new value nudges the estimate a fixed fraction of the way toward it, so recent values count most and older values count less and less, at a constant rate. You only store one number, and each update is one multiply and one add.
Where you'll see it: in smoothed stock prices and load averages, in the momentum and second-moment terms of the Adam optimizer, and in TCP's round-trip-time estimate.
Intuition
A running average that forgets gradually
Suppose you want the typical value of a noisy signal, such as the latency of a server that you sample once a second. The plain average of everything is useless once the server's behaviour changes. A simple moving average fixes that by keeping the last readings and averaging them. It works, but it has two costs: you must store values, and a reading vanishes abruptly the moment it falls out of the window.
The EMA gets rid of both costs. Instead of a window, it keeps one running estimate and moves it toward each new reading :
The smoothing factor , a number between 0 and 1, is the fraction of the gap you close each step. A large chases the data; a small moves slowly and smooths out spikes. Written this way, the EMA is just a feedback loop. The error is the surprise in the new reading, and the update corrects the estimate by a fraction of that surprise.
- α = 0.5
- α = 0.1
The weights decay geometrically. Each step back multiplies the weight by , so a reading's influence shrinks by the same share every step. That constant ratio is what makes the EMA cheap: the exact influence of all history is baked into the single number .
How it responds to a change
The plot below shows the EMA with following a signal that jumps from 0 to 1 at time 10. The estimate does not jump with it. It closes 20% of the remaining gap each step, so it approaches 1 smoothly.
- input x
- EMA s, α = 0.2
The math
Definition
Given a sequence and a smoothing factor , the exponential moving average is defined by
Here is the reading at time step and is the estimate after seeing it. The two forms are the same: . The first step starts the estimate at the first reading.
Unrolling the recursion
Substituting the definition into itself shows what each reading contributes. Write for the decay ratio. After unrolling, the estimate is
The newest reading has weight , the one before it has weight , the one before that , and so on. The first reading keeps the leftover weight .
These weights add up to one, which is why is an average and not a scaled sum. Use the geometric series from summation notation:
using . So is a weighted average of all readings so far, with geometrically decaying weights.
Choosing α
Two common ways to pick are the span convention and the half-life.
- Span. Setting makes the EMA's centre of mass roughly that of an -point window. This is the convention most libraries use for a parameter called
span. - Half-life. The half-life is the number of steps for a reading's weight to halve. It solves :
Bias correction for a cold start
If you start with instead of the first reading, early estimates are pulled toward zero. The weights of the readings seen so far add up to only , not to 1. Dividing by that total removes the bias:
This is the correction Adam uses for its moving averages. Late in the sequence, is close to 0 and the correction vanishes.
Worked examples
Take and readings , , , . Start with .
- .
- .
- .
- Check with the unrolled form: . The weights add to 1.
Take and readings , starting at .
- .
- .
- .
- (to three decimals).
- The final reading is about 9 above the previous estimate (), but the estimate moves only . A single spike or jump changes the average gently, which is exactly the smoothing you want from noisy data.
A dashboard wants an average that behaves roughly like a 19-sample window.
- Span convention: .
- Half-life: samples. A reading loses half its weight after about 6.6 steps.
- Share of weight in the last 19 readings: .
- So about 86% of the estimate comes from the last 19 samples, and the rest from older ones that still count a little. A plain 19-sample window would give those older samples exactly zero.
Tradeoffs and failure modes
The EMA is a good default for a stream you cannot store, and a poor choice when its assumptions break.
| Situation | Problem | Better choice |
|---|---|---|
| You need an exact "last 7 days" cutoff | An EMA never drops a reading to zero; old values fade but never leave | Simple moving average over a window |
| Early estimates after startup | Starting from zero biases them toward zero | Bias correction, as in Adam |
| Timestamps are unevenly spaced | A fixed assumes equal steps | Time-aware decay for a gap and time constant |
| Outliers or spikes | Each spike adds times its size and then lingers | Clip or median-filter the input before averaging |
| You need a spread, not just a centre | An EMA of says nothing about variance | EMAs of both and , or an EWMA variance estimator |
Also, trades lag against noise: the smoother the output, the later it reacts. Pick the half-life on the timescale that matters to your question.
History
Exponential smoothing grew out of forecasting work in the late 1950s. Robert G. Brown wrote about it for inventory control and demand forecasting, and Charles C. Holt extended it to data with a trend. Both were trying to forecast from a stream of recent observations without storing long histories, which was expensive on the computers of the day. The idea spread through business forecasting and operations research over the next decades.
The name "exponentially weighted moving average" became common in quality control. The EWMA control chart, which flags a process when its smoothed measurement drifts, became a standard quality-control tool. In machine learning, the same recursion appears in momentum methods and in the moment estimates inside Adam, which was published in 2014 with the bias correction described above. It caught on because it is the cheapest way to keep a recency-weighted average of anything.
Check yourself
QuestionIn the update , what happens to the influence of a reading as gets larger?Show answer
A larger gives the newest reading more weight and older readings decay faster. The estimate reacts sooner to changes, but it also passes through more noise. A smaller smooths more and reacts more slowly.
QuestionWith , , then and . What are and ?Show answer
. Then .
QuestionWhy can an EMA be computed in constant memory, while a simple moving average over points needs stored values? Why does that not cost accuracy?Show answer
The unrolled form shows that all past readings are summarised by their geometric weights, and the recursion carries exactly that summary forward in one number . A simple moving average has a sharp window, so it must remember which values are inside it. The EMA has no window, so the one number is enough. Accuracy is not lost because the EMA is exactly a weighted average of all history; it is just a different weighting than the window.
QuestionAdam starts its first moment estimate at with . After two gradients, and , what is the bias-corrected estimate at , and why is it needed?Show answer
Unrolling gives . The total weight is , so the corrected value is . This matches the true weighted average . Without the correction, the estimate would be about 0.39, far below the gradients that actually occurred, and early updates would be too small.
Sources
- Exponential smoothing (Wikipedia): https://en.wikipedia.org/wiki/Exponential_smoothing
- Moving average (Wikipedia): https://en.wikipedia.org/wiki/Moving_average
- Hyndman and Athanasopoulos, Forecasting: Principles and Practice, ch. 8: https://otexts.com/fpp3/
- Kingma and Ba, Adam: A Method for Stochastic Optimization (2014): https://arxiv.org/abs/1412.6980