Pick the smoothing factor of simple exponential smoothing by one-step error

Why it matters SES is the workhorse for forecasting thousands of low-volume series such as SKU demand, and choosing alpha by one-step error is the standard fitting method.

Simple exponential smoothing (SES) forecasts with a level that moves part of the way toward each new value. The smoothing factor alpha is chosen by minimising one-step-ahead errors.

Write ses_fit(y, alphas). For a given alpha: start with level = y[0]; for t = 1 .. n - 1 the forecast for y[t] is the current level, the error is y[t] - level, and then level = alpha * y[t] + (1 - alpha) * level. SSE is the sum of squared errors. Return (best_alpha, best_sse, next_forecast) for the alpha with the smallest SSE (first one on ties), where next_forecast is the final level, all floats.

Signaturedef ses_fit(y, alphas)

Example

y [10, 12, 11, 13, 12, 14], alphas [0.1, 0.5, 0.9] gives (0.5, 12.0, 13.0).

Understand the idea

Simple exponential smoothing keeps one number, the level, as its best guess of the series right now. When a new value arrives, the level moves part of the way toward it. alpha = 0.1 means 'move 10% of the way' (smooth, slow to react); alpha = 0.9 means 'almost jump to the new value' (fast, but noisy).

Worked example: y = 10, 12 with alpha = 0.5. Level starts at 10. The forecast for 12 is 10, so the error is 2. Then level = 0.5 * 12 + 0.5 * 10 = 11, which is the forecast for the next value.

To pick alpha honestly, each forecast must be made before seeing the value it predicts. Then add up the squared one-step errors and choose the alpha with the smallest total.

Rule: error_t = y_t - level, then level = alpha y_t + (1 - alpha) level; minimise sum of error_t^2.

Mixing up that order leaks the answer into the forecast, a classic time series leakage bug. SES with a fitted alpha is a default in many demand planning systems.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: exampleses_fit([10, 12, 11, 13, 12, 14], [0.1, 0.5, 0.9])
  • Sample 2: noisy flat seriesses_fit([5, 7, 4, 6, 5, 7, 4, 6], [0.1, 0.3, 0.6, 0.9])
  • Test 3: level shiftses_fit([1, 1, 1, 10, 10, 10, 10], [0.2, 0.5, 1.0])
  • Test 4: random walkses_fit(_y, [0.05, 0.2, 0.5, 0.8, 1.0])