Holt's Method: Smoothing Both Level and Trend

Time SeriesMedium+15 XP+15

Holt's Method: Smoothing Both Level and Trend

A single exponential moving average has a structural flaw: on a series with a steady trend it always lags behind. By the time it catches up to where the series was, the series has moved on.

Holt's method (double exponential smoothing) fixes it by tracking two things instead of one — the current level and the current trend — and updating both at every step.

ℓt=αxt+(1−α)(ℓt−1+bt−1)\ell_t = \alpha x_t + (1 - \alpha)(\ell_{t-1} + b_{t-1})ℓt​=αxt​+(1−α)(ℓt−1​+bt−1​) bt=β(ℓt−ℓt−1)+(1−β)bt−1b_t = \beta (\ell_t - \ell_{t-1}) + (1 - \beta) b_{t-1}bt​=β(ℓt​−ℓt−1​)+(1−β)bt−1​

Read the first line as the EMA recursion with one change: instead of mixing toward the old level, it mixes toward the old level plus the trend — the level's own forecast of where it was heading. The second line is the same recursion applied to the trend, which is estimated by how much the level just moved.

Task: write holt(series, alpha, beta) returning the level at every time step, each rounded to 4 decimal places.

Initialisation: level = series[0] and trend = series[1] - series[0].

  • The series has at least two values.
  • The output has one entry per input value, starting with series[0] itself.
  • Update the level first, then use both the new and old level to update the trend. Doing it the other way round gives different numbers.
  • Carry unrounded values forward; round only what you append.

The pay-off is visible on a straight line: feed in 10, 20, 30, 40 and Holt reproduces it exactly, because it has learned the trend of +10 and keeps applying it. A single EMA on the same input trails behind at every step.

Examples

series = [10, 20, 30, 40]
alpha = 0.5
beta = 0.5
returns [10.0, 20.0, 30.0, 40.0]
series = [100, 90, 85, 70]
alpha = 0.3
beta = 0.1
returns [100.0, 90.0, 81.5, 71.155]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run