Forecast a trending series with Holt's linear smoothing
A plain exponential moving average always lags behind a series that keeps rising, like daily active users or token spend. Holt's method tracks two things, a level and a trend, so its forecasts keep climbing.
Write holt_forecast(y, alpha, beta, h). y is a 1D array with at least 2 values. Initialise level l = y[0] and trend b = y[1] - y[0]. Then for t = 1, ..., len(y) - 1:
- l_new = alpha * y[t] + (1 - alpha) * (l + b)
- b = beta * (l_new - l) + (1 - beta) * b
- l = l_new
Return a NumPy array of h forecasts: l + k * b for k = 1, ..., h.
def holt_forecast(y, alpha, beta, h)Example
y = [1, 2, 3, 4] (a perfect line) gives level 4 and trend 1 for any alpha and beta, so h = 2 returns [5.0, 6.0].
Understand the idea
A plain moving average always trails a series that keeps rising. Holt's method keeps two numbers: a level (where the series is now) and a trend (how much it grows per step). Each new point nudges both, and the forecast extends the line from the latest level using the trend. Worked example: y = [2, 4, 5], alpha = 0.5, beta = 0.5. Start l = 2, b = 4 - 2 = 2. At t = 1: l_new = 0.5 * 4 + 0.5 * (2 + 2) = 4, b = 0.5 * (4 - 2) + 0.5 * 2 = 2, l = 4. At t = 2: l_new = 0.5 * 5 + 0.5 * (4 + 2) = 5.5, b = 0.5 * (5.5 - 4) + 0.5 * 2 = 1.75, l = 5.5. One step ahead forecast = 5.5 + 1.75 = 7.25. Rule: level = alpha * y_t + (1 - alpha) * (old level + old trend), trend = beta * (level change) + (1 - beta) * old trend, forecast k steps ahead = level + k * trend. Used for quick capacity, user growth and spend forecasts.