Chapter 2 · Build and stress

Statistical and ML forecasters

Choose a forecaster and a multi-step strategy together; the estimator alone does not define the forecast.

65–90 min4 key conceptsReviewed 26 Aug 2026
01 · Production proposition

Regional demand shifts through promotions, holidays, and drift.

This chapter moves from match assumptions to the data to compare whole systems. We will make the idea concrete in a small code example before placing it back inside a production system with available before staffing decisions.

Decision

Backtest preprocessing, refitting time, inference latency, fallback behavior, and per-horizon error, not just the estimator object.

Evidence

Verify the behavior illustrated in direct_forecast.py, then measure decision utility, slice reliability, and system latency.

Failure consequence

Training uses realized future weather and random folds, producing a forecast impossible to reproduce at origin time. An unsafe release must degrade to a named baseline or the last known-good version.

02 · Guided chapter

Build the idea one layer at a time.

Forecasting models differ less by prestige than by the structure they assume. Statistical models describe temporal dependence directly; machine-learning models learn flexible mappings from engineered histories and covariates.

Exponential smoothing and ARIMA are strong when a series has stable level, trend, seasonality, and autocorrelation. Tree ensembles can pool many related series and exploit nonlinear effects, but only after the history has been converted into origin-safe features. A global model can share strength across sparse regions if identity and scale are handled deliberately.

Multi-step strategy matters. Recursive forecasting feeds predictions back as inputs and can accumulate error; direct forecasting trains a separate output for each horizon; multi-output models predict the path jointly. Compare them under the same rolling origins, baseline, feature availability, and compute budget.

Keep in view
  • direct vs recursive
  • quantiles
  • coverage
  • reconciliation
WORKED PATH

Follow the reasoning before touching the code.

  1. 01

    Match assumptions to the data

    Use plots and residual autocorrelation to decide whether a compact statistical model explains the series before adding a flexible learner.

  2. 02

    Choose a horizon strategy

    Select recursive for simplicity, direct for horizon-specific behavior, or joint output for cross-horizon structure—and record how training targets are formed.

  3. 03

    Compare whole systems

    Backtest preprocessing, refitting time, inference latency, fallback behavior, and per-horizon error, not just the estimator object.

pythondirect_forecast.py
import numpy as np
from sklearn.linear_model import Ridge

series = np.array([4, 5, 7, 8, 10, 11, 13, 14], dtype=float)
X = np.array([[series[i]] for i in range(5)])
models = []
for horizon in (1, 2, 3):
    y = np.array([series[i + horizon] for i in range(5)])
    models.append(Ridge(alpha=0.1).fit(X, y))
print([round(model.predict([[14]])[0], 2) for model in models])

Fit one direct linear model for each future horizon.

READ THE EXAMPLE

What to notice

  • Each horizon learns its own mapping, so an error at horizon one is not fed into horizons two and three.
  • Direct models require separate aligned targets and more parameters; their convenience does not remove the need for a seasonal-naive comparison.
YOUR TURN Pause and predict before revealing the answer.

What changes if one one-step model is called three times and each prediction becomes the next input?

Need a hint?

Trace whether the second call receives an observed value or a model output.

Check your reasoning

That becomes recursive forecasting: it uses one model but exposes later horizons to accumulated earlier prediction error.

Bring forward

Lessons 1, 2, 3 in this course.

03 · Formal treatment

Connect the chapter to the course’s quantitative spine.

Choose a forecaster and a multi-step strategy together; the estimator alone does not define the forecast. The quantile forecast objective below is the shared course-level lens that helps quantify the surrounding system. Name each symbol, check its units or shape, and use only values that were available at the production cutoff.

Formal treatment
q^τ=arg min⁡q∑max⁡ ⁣(τ(y−q),(τ−1)(y−q))\hat{q}_\tau = \operatorname*{arg\,min}_q \sum \max\!\left(\tau(y-q),(\tau-1)(y-q)\right)

Quantile forecast objective

Symbol, shape or unit contract
SymbolMeaning / shape / unit
τtarget quantile
q̂_τpredicted quantile
yobserved value
Open derivation and numerical substitution

Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.

  1. Write the named inputs: τ, q̂_τ, y.
  2. Substitute one small, hand-checkable batch before vectorizing.
  3. Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c07(inputs)
assert is_finite(value)
04 · Deterministic lab

Change one assumption and make the tradeoff visible.

Use the worked chapter as your prediction, then change one input in the lab and explain why the output moves. The lab runs predefined TypeScript and never executes learner code, so the focus stays on the concept rather than setup.

Simplified simulation

Rolling-window backtest

Change history length and inspect bias, error, and interval coverage.

weeks
Primary12.1 MAE
Secondary83.2%
DiagnosisBalanced

Assumption: Seasonal demand with one simulated regime change at week 30.

Open nonvisual data table
ItemComputed stateInterpretation
h+112.5 MAE87.8%
h+613.6 MAE86.7%
h+1215.0 MAE85.4%
h+1816.3 MAE84.0%
h+2417.6 MAE82.7%
05 · Production implications

Trace the complete operating path.

Carry the chapter’s central idea into the system: Choose a forecaster and a multi-step strategy together; the estimator alone does not define the forecast. The path below shows where that reasoning needs an artifact, a test, an owner, or a fallback.

  1. 01

    Turn “Match assumptions to the data” into a written invariant and a failing test.

  2. 02

    Promote direct_forecast.py from illustration to a versioned implementation with representative fixtures.

  3. 03

    Use “Compare whole systems” as the release checkpoint: Backtest preprocessing, refitting time, inference latency, fallback behavior, and per-horizon error, not just the estimator object.

  4. 04

    Serve or materialize behind explicit available before staffing decisions.

  5. 05

    Join telemetry to mature outcomes and retain a rollback path.

Observability

Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.

Cost

Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.

Failure modes

Training uses realized future weather and random folds, producing a forecast impossible to reproduce at origin time. Add a detector, owner, mitigation, and stop condition for this class of failure.

Alternatives

Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.

06 · Check understanding

Explain the contract, not just the vocabulary.

Browser-graded checkpointPass ≥ 80%
01Why specify forecast lead time?
02Why use rolling origins?
03What should a calibrated 90% interval do?
07 · Apply in production

Regional demand forecast

Build a 24-hour pipeline with naive, statistical, and boosted baselines plus interval forecasts.

  • Forecast contract
  • Rolling-origin evaluation
  • Horizon and coverage report
  • Drift/retrain/fallback policy
Open assignment and rubric
08 · Sources & next depth

Read primary material with a purpose.

09 · Production resolution

Return to the opening failure.

Monitor horizon-level error, bias, interval coverage, freshness, reconciliation, and a seasonal-naive fallback.

For this lesson, the release evidence is the worked code example, a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce rolling validation, statistical baselines, boosted forecasts, and uncertainty bands.