Search

Search papers and lessons

Exponential Moving Average

Level 2 · 10 min read

An exponential moving average is a running average where the newest value gets a fixed share and older values fade geometrically, so each update costs one multiply-add and no history is stored.

On this page

  • Colour = topic mix
  • Optimizers
  • Discrete math
  • Sign in to see your progress

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 NN readings and averaging them. It works, but it has two costs: you must store NN 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 ss and moves it toward each new reading xx:

s←s+α (x−s).s \leftarrow s + \alpha\,(x - s).

The smoothing factor α\alpha, a number between 0 and 1, is the fraction of the gap you close each step. A large α\alpha chases the data; a small α\alpha moves slowly and smooths out spikes. Written this way, the EMA is just a feedback loop. The error x−sx - s is the surprise in the new reading, and the update corrects the estimate by a fraction of that surprise.

0510152000.10.20.30.40.5k (steps ago)weight
  • α = 0.5
  • α = 0.1
The weight a reading gets k steps after it arrived is α(1 − α)^k. Larger α forgets faster. The dots mark the newest reading, k = 0.

The weights decay geometrically. Each step back multiplies the weight by 1−α1 - \alpha, 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 ss.

How it responds to a change

The plot below shows the EMA with α=0.2\alpha = 0.2 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.

05101500.20.40.60.81time tvalue
  • input x
  • EMA s, α = 0.2
The input steps up at t = 10. The EMA closes 20% of the remaining gap each step, so the gap shrinks by a factor of 0.8 per step and it takes about 11 steps to get within 10% of the new level.

The math

Definition

Definition: Exponential moving average

Given a sequence x1,x2,…x_1, x_2, \dots and a smoothing factor α∈(0,1]\alpha \in (0, 1], the exponential moving average sts_t is defined by

s1=x1,st=α xt+(1−α) st−1(t≥2).s_1 = x_1, \qquad s_t = \alpha\, x_t + (1 - \alpha)\, s_{t-1} \quad (t \ge 2).

Here xtx_t is the reading at time step tt and sts_t is the estimate after seeing it. The two forms are the same: st=st−1+α(xt−st−1)s_t = s_{t-1} + \alpha(x_t - s_{t-1}). The first step starts the estimate at the first reading.

Unrolling the recursion

Substituting the definition into itself shows what each reading contributes. Write r=1−αr = 1 - \alpha for the decay ratio. After unrolling, the estimate is

st=∑k=0t−2α rk xt−k  +  r t−1 x1.s_t = \sum_{k=0}^{t-2} \alpha\, r^{k}\, x_{t-k} \;+\; r^{\,t-1}\, x_1 .

The newest reading xtx_t has weight α\alpha, the one before it has weight αr\alpha r, the one before that αr2\alpha r^2, and so on. The first reading keeps the leftover weight rt−1r^{t-1}.

These weights add up to one, which is why sts_t is an average and not a scaled sum. Use the geometric series from summation notation:

∑k=0t−2α rk+r t−1=α⋅1−r t−11−r+r t−1=(1−r t−1)+r t−1=1,\sum_{k=0}^{t-2} \alpha\, r^{k} + r^{\,t-1} = \alpha \cdot \frac{1 - r^{\,t-1}}{1 - r} + r^{\,t-1} = (1 - r^{\,t-1}) + r^{\,t-1} = 1,

using 1−r=α1 - r = \alpha. So sts_t is a weighted average of all readings so far, with geometrically decaying weights.

Choosing α

Two common ways to pick α\alpha are the span convention and the half-life.

  • Span. Setting α=2N+1\alpha = \frac{2}{N + 1} makes the EMA's centre of mass roughly that of an NN-point window. This is the convention most libraries use for a parameter called span.
  • Half-life. The half-life hh is the number of steps for a reading's weight to halve. It solves rh=12r^{h} = \frac{1}{2}:
h=ln⁡12ln⁡(1−α)=−ln⁡2ln⁡(1−α).h = \frac{\ln \tfrac{1}{2}}{\ln(1 - \alpha)} = -\frac{\ln 2}{\ln(1 - \alpha)}.

Bias correction for a cold start

If you start with s0=0s_0 = 0 instead of the first reading, early estimates are pulled toward zero. The weights of the readings seen so far add up to only 1−rt1 - r^{t}, not to 1. Dividing by that total removes the bias:

s^t=st1−(1−α)t.\hat{s}_t = \frac{s_t}{1 - (1 - \alpha)^{t}}.

This is the correction Adam uses for its moving averages. Late in the sequence, (1−α)t(1 - \alpha)^t is close to 0 and the correction vanishes.

Worked examples

Example
A tiny run by hand

Take α=0.5\alpha = 0.5 and readings x1=10x_1 = 10, x2=12x_2 = 12, x3=8x_3 = 8, x4=14x_4 = 14. Start with s1=x1=10s_1 = x_1 = 10.

  1. s2=0.5×12+0.5×10=11s_2 = 0.5 \times 12 + 0.5 \times 10 = 11.
  2. s3=0.5×8+0.5×11=9.5s_3 = 0.5 \times 8 + 0.5 \times 11 = 9.5.
  3. s4=0.5×14+0.5×9.5=11.75s_4 = 0.5 \times 14 + 0.5 \times 9.5 = 11.75.
  4. Check with the unrolled form: 0.5×14+0.25×8+0.125×12+0.125×10=7+2+1.5+1.25=11.750.5 \times 14 + 0.25 \times 8 + 0.125 \times 12 + 0.125 \times 10 = 7 + 2 + 1.5 + 1.25 = 11.75. The weights 0.5,0.25,0.125,0.1250.5, 0.25, 0.125, 0.125 add to 1.
Example
A jump in a noisy signal

Take α=0.2\alpha = 0.2 and readings 100,102,98,105,110100, 102, 98, 105, 110, starting at s1=100s_1 = 100.

  1. s2=0.2×102+0.8×100=100.4s_2 = 0.2 \times 102 + 0.8 \times 100 = 100.4.
  2. s3=0.2×98+0.8×100.4=99.92s_3 = 0.2 \times 98 + 0.8 \times 100.4 = 99.92.
  3. s4=0.2×105+0.8×99.92=100.936s_4 = 0.2 \times 105 + 0.8 \times 99.92 = 100.936.
  4. s5=0.2×110+0.8×100.936=102.749s_5 = 0.2 \times 110 + 0.8 \times 100.936 = 102.749 (to three decimals).
  5. The final reading is about 9 above the previous estimate (110−100.936=9.064110 - 100.936 = 9.064), but the estimate moves only 0.2×9.064≈1.810.2 \times 9.064 \approx 1.81. A single spike or jump changes the average gently, which is exactly the smoothing you want from noisy data.
Example
Turning a window into a half-life

A dashboard wants an average that behaves roughly like a 19-sample window.

  1. Span convention: α=219+1=0.1\alpha = \frac{2}{19 + 1} = 0.1.
  2. Half-life: h=−ln⁡2ln⁡0.9=0.69310.1054≈6.6h = -\frac{\ln 2}{\ln 0.9} = \frac{0.6931}{0.1054} \approx 6.6 samples. A reading loses half its weight after about 6.6 steps.
  3. Share of weight in the last 19 readings: 1−0.919=1−0.1351≈0.8651 - 0.9^{19} = 1 - 0.1351 \approx 0.865.
  4. 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.

SituationProblemBetter choice
You need an exact "last 7 days" cutoffAn EMA never drops a reading to zero; old values fade but never leaveSimple moving average over a window
Early estimates after startupStarting from zero biases them toward zeroBias correction, as in Adam
Timestamps are unevenly spacedA fixed α\alpha assumes equal stepsTime-aware decay α=1−e−Δt/τ\alpha = 1 - e^{-\Delta t / \tau} for a gap Δt\Delta t and time constant τ\tau
Outliers or spikesEach spike adds α\alpha times its size and then lingersClip or median-filter the input before averaging
You need a spread, not just a centreAn EMA of xx says nothing about varianceEMAs of both xx and x2x^2, or an EWMA variance estimator

Also, α\alpha trades lag against noise: the smoother the output, the later it reacts. Pick the half-life hh 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 s←s+α(x−s)s \leftarrow s + \alpha(x - s), what happens to the influence of a reading as α\alpha gets larger?Show answer

A larger α\alpha 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 α\alpha smooths more and reacts more slowly.

QuestionWith α=0.25\alpha = 0.25, s1=8s_1 = 8, then x2=12x_2 = 12 and x3=4x_3 = 4. What are s2s_2 and s3s_3?Show answer

s2=0.25×12+0.75×8=3+6=9s_2 = 0.25 \times 12 + 0.75 \times 8 = 3 + 6 = 9. Then s3=0.25×4+0.75×9=1+6.75=7.75s_3 = 0.25 \times 4 + 0.75 \times 9 = 1 + 6.75 = 7.75.

QuestionWhy can an EMA be computed in constant memory, while a simple moving average over NN points needs NN 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 ss. 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 m0=0m_0 = 0 with β1=0.9\beta_1 = 0.9. After two gradients, g1=1g_1 = 1 and g2=3g_2 = 3, what is the bias-corrected estimate at t=2t = 2, and why is it needed?Show answer

Unrolling gives m2=0.9×0.1×g1+0.1×g2=0.09+0.3=0.39m_2 = 0.9 \times 0.1 \times g_1 + 0.1 \times g_2 = 0.09 + 0.3 = 0.39. The total weight is 1−0.92=0.191 - 0.9^2 = 0.19, so the corrected value is m^2=0.39/0.19≈2.05\hat{m}_2 = 0.39 / 0.19 \approx 2.05. This matches the true weighted average (0.9×1+1×3)/1.9≈2.05(0.9 \times 1 + 1 \times 3) / 1.9 \approx 2.05. 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