Forecasting with Holt's Linear Trend

medium
Unit 13 · Statistics & Time Series0/3 to milestone

Simple exponential smoothing can't follow a trend: forecasts lag behind growing sales. Holt's method tracks both a level ℓ\ell and a trend bb:

ℓt=αyt+(1−α)(ℓt−1+bt−1),bt=β(ℓt−ℓt−1)+(1−β) bt−1\ell_t = \alpha y_t + (1-\alpha)(\ell_{t-1} + b_{t-1}), \qquad b_t = \beta(\ell_t - \ell_{t-1}) + (1-\beta)\,b_{t-1}

Initialise with ℓ0=y0\ell_0 = y_0 and b0=y1−y0b_0 = y_1 - y_0, then update for t=1…n−1t = 1 \dots n-1. The forecast hh steps after the last observation is ℓn−1+h⋅bn−1\ell_{n-1} + h \cdot b_{n-1}.

Implement holt_forecast(y, alpha, beta, horizon) returning a list of horizon forecasts. Raise ValueError if y has fewer than 2 points. Then implement mape(actual, forecast), the mean absolute percentage error as a percentage, skipping points where the actual value is 0.

#time-series#forecasting2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Forecasting with Holt's Linear Trend · TechGradify