#262Holt-Winters Exponential SmoothingMedium

Holt-Winters Exponential Smoothing

Background

Holt's linear smoothing (the level + trend variant of exponential smoothing) is the canonical lightweight forecaster for series with a roughly-linear trend and no seasonality. It maintains two state variables — level LtL_t and trend TtT_t — each as a weighted average of the new observation and the previous extrapolation. Used as a baseline forecaster in production capacity planning, ETA prediction, and time-series anomaly detection.

Problem statement

Implement holt(series, alpha, beta, horizon). Initialise L=y0L = y_0, T=y1y0T = y_1 - y_0. For t=1,,T1t = 1, \dots, T-1:

Lt=αyt+(1α)(Lt1+Tt1),Tt=β(LtLt1)+(1β)Tt1L_t = \alpha y_t + (1 - \alpha)(L_{t-1} + T_{t-1}),\qquad T_t = \beta (L_t - L_{t-1}) + (1 - \beta) T_{t-1}

Forecast hh steps ahead is LT+hTTL_T + h \cdot T_T.

Input

  • series — array-like of floats, the observed time series (2\ge 2 values).
  • alpha — smoothing factor for the level, in (0,1)(0, 1).
  • beta — smoothing factor for the trend, in (0,1)(0, 1).
  • horizonint >= 1, number of future steps to forecast.

Output

Returns a tuple (level, trend, forecast):

  • level — final LTL_T (Python float).
  • trend — final TTT_T (Python float).
  • forecastnp.ndarray of length horizon.

Examples

Example 1 — constant series

Input:  series=[5.0, 5.0, ..., 5.0], alpha=0.5, beta=0.3, horizon=3
Output: level=5.0, trend≈0.0, forecast=[5.0, 5.0, 5.0]

Example 2 — linear series recovers the slope

Input:  series=[0, 1, 2, ..., 19], alpha=0.5, beta=0.5, horizon=3
Output: trend ≈ 1.0

Constraints

  • series must have 2\ge 2 observations.
  • alpha and beta must lie in (0,1)(0, 1).
  • horizon >= 1.

Notes

  • Adding seasonality. Holt-Winters' triple-exponential variant adds a per-season component StS_t; this implementation is the level+trend version.
  • Picking α,β\alpha, \beta. Production setups grid-search over a held-out window or use AIC/BIC; defaults of α=0.5,β=0.3\alpha=0.5, \beta=0.3 are reasonable for monthly data.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: constant series forecast
  • Sample: two-point series final level and trend
  • Example: linear ramp forecast