Sub topic 12
Forecasting several steps ahead: recursive vs direct
This chapter assumes you can build lag and rolling features, fit a HistGradientBoostingRegressor, and validate chronologically, as covered in earlier chapters. A model trained on lag features predicts one step ahead. To forecast a horizon of several steps, you need a strategy. The two common ones are recursive and direct.
The problem
At time t, lag_1 is known. At t+2, lag_1 is the value at t+1, which you have not observed yet. Any feature that depends on unobserved values cannot be computed from real data. The strategies differ in how they handle this.
Recursive strategy
You train one model for one step ahead. To forecast further, you predict step 1, append that prediction to the history, and use it as a lag for step 2. Repeat until the horizon is reached. The recursive_forecast function from this tutorial's code examples does exactly this.
preds = recursive_forecast(model, history, horizon=14, feature_cols=feature_cols)Here history must be a Series with a DatetimeIndex whose freq is set, for example via asfreq('D').
Strengths:
- One model to train and maintain.
- Works for any horizon without retraining.
Weaknesses:
- Errors accumulate. A wrong prediction at step 1 becomes an input at step 2.
- The model was trained on real lags, but at inference it sees its own predictions. Predictions are usually smoother than real data, so this mismatch can bias forecasts.
Direct strategy
You train a separate model for each horizon h. Each model learns to predict y[t+h] using only information available at time t. The target is shifted forward instead of the features being filled in.
import pandas as pd
from sklearn.ensemble import HistGradientBoostingRegressor
def fit_direct(data: pd.DataFrame, feature_cols: list, horizon: int):
models = {}
for h in range(1, horizon + 1):
target = data['y'].shift(-h) # value h steps in the future
mask = target.notna()
m = HistGradientBoostingRegressor(max_iter=300, learning_rate=0.05, random_state=0)
m.fit(data.loc[mask, feature_cols], target[mask])
models[h] = m
return models
def predict_direct(models: dict, last_row: pd.DataFrame):
return [float(m.predict(last_row)[0]) for _, m in sorted(models.items())]Every model uses features computed at the forecast origin t. No predictions are fed back, so errors do not compound through the inputs.
Strengths:
- No error accumulation through the lags.
- Training and inference use the same kind of inputs.
Weaknesses:
horizonmodels to train and store.- Each model is fitted independently, so the forecasts across steps may not form a coherent path.
- Calendar features need care. For the model at horizon
h, the day of week of the target date is(origin + h), which you can compute in advance and add as a feature.
Comparison
| Aspect | Recursive | Direct |
|---|---|---|
| Models | 1 | One per step |
| Error accumulation | Yes, through lags | No |
| Train/inference mismatch | Yes | No |
| Cost | Low | Grows with horizon |
| Path coherence | Natural | Not guaranteed |
Choosing between them
Start with recursive. It is simple and often good enough for short horizons. Switch to direct if you see errors grow quickly with the horizon, or if the horizon is long. Evaluate both with walk-forward validation, and score each horizon separately, since an average over all steps hides where a method breaks down.
Exercise
You need a 28-day daily forecast and your recursive forecasts drift badly after day 10. Name one cause and one alternative strategy.
Show answer
Prediction errors are fed back as lags and compound, and the model never saw its own smoothed predictions during training. The direct strategy avoids this by training one model per horizon on features known at the forecast origin.