Sub topic 3
Validating Forecasts and Extending the Model
This chapter assumes you know Python, pandas, regression, decision trees, and the fit/predict interface. Time-series validation must preserve time order because future observations cannot be used to predict the past.
Chronological and Walk-Forward Validation
A chronological split puts earlier rows in the training set and later rows in the test set. A random split can place future patterns in training data and produce an unrealistically good score.
Walk-forward validation repeats this process. Each fold trains on an expanding or rolling history and validates on the next block. A gap can separate training and validation rows when features or operations need extra time.
from sklearn.model_selection import TimeSeriesSplit
from sklearn.metrics import mean_absolute_error
splitter = TimeSeriesSplit(n_splits=5, test_size=30, gap=7)
for train_idx, valid_idx in splitter.split(data):
train = data.iloc[train_idx]
valid = data.iloc[valid_idx]
model.fit(train[features], train["y"])
prediction = model.predict(valid[features])
print(mean_absolute_error(valid["y"], prediction))Several Steps Ahead
Recursive forecasting predicts one step, adds that prediction to history, and uses it for the next step. It reuses one model but can accumulate errors. Direct forecasting trains a separate model for each horizon, such as one model for tomorrow and another for seven days ahead. It avoids error propagation but needs more models and training data.
Tuning Without Future Information
Tune hyperparameters only inside the training period. Use walk-forward validation to compare settings such as tree depth, learning rate, and number of iterations. Keep the final test period untouched until model selection is complete.
> callout warning > Repeatedly checking the final test score turns the test set into a tuning set. Report it only after all choices are fixed.
Inspecting Features
Tree-based feature importance can show whether lag, rolling, or calendar columns influence predictions. Permutation importance is often more informative: shuffle one feature in validation data and measure how much the error increases. Correlated features can share importance, so treat rankings as evidence, not proof of causation.
Extensions
External regressors add useful information such as promotions or weather. Use a variable only when its future value is known or separately forecast. A global model trains on many related series by adding a series identifier and shared features. It can learn common patterns while retaining series-specific levels.
For uncertainty, quantile loss trains a model to predict a chosen percentile. For example, the 0.1 and 0.9 quantiles form an approximate prediction interval around a median forecast.
Exercise
Why should a final test period remain untouched during hyperparameter tuning?
Show answer
Repeated use lets model choices adapt to that period, so its score no longer estimates performance on unseen future data.