Sub topic 3
Time-Ordered Data: Forward-Chaining Splits
Assume you know supervised learning, overfitting, common metrics, NumPy, and scikit-learn’s fit and predict API.
Time-ordered data: forward-chaining splits
Randomly assigning time-series rows to folds can leak future information into training. Forward-chaining cross-validation keeps time order: each test window occurs after its training data.
import numpy as np
from sklearn.model_selection import TimeSeriesSplit
X = np.arange(12).reshape(6, 2)
splitter = TimeSeriesSplit(n_splits=3)
for fold, (train, test) in enumerate(splitter.split(X), start=1):
print(f"fold {fold}: train={train}, test={test}")The training set usually grows from one split to the next. This matches a process that trains on historical observations and predicts later observations.
Tuning hyperparameters with cross-validation
A hyperparameter is a setting chosen before training, such as a regularization strength. GridSearchCV evaluates candidate settings with cross-validation, then refits the best one on the supplied training data. For time series, pass a TimeSeriesSplit object as cv.
Keep a final chronological test period untouched until all choices are complete.
Leave-one-out and leave-p-out
Leave-one-out cross-validation (LOOCV) uses one observation as the test set and all other observations as training data, repeating this for every observation. Leave-p-out holds out every possible group of p observations. These methods can be expensive and their test scores can vary greatly, especially with dependent or time-ordered data. They are rarely suitable for ordinary forecasting.
Repeated cross-validation
Repeated cross-validation runs a random partitioning method several times with different seeds. It estimates how sensitive results are to the split. Repetition is useful for independent observations, but it does not repair a wrong split strategy. Repeating shuffled folds on time-series data still leaks temporal structure.
Nested cross-validation for unbiased model selection
Nested cross-validation uses an inner loop to select hyperparameters and an outer loop to estimate performance. The outer test fold remains unseen during tuning. This reduces optimistic bias when many model settings are compared.
Limitations and common pitfalls
- Folds are not independent when training windows overlap.
- A distribution shift can make old scores poor predictors of future performance.
- The forecast horizon, gaps, and retraining schedule should match production.
- Report the metric, split design, window sizes, and fold variability.
- Keep a final untouched test period when possible.
Exercise
Why should a time-series test window come after its training window?
Show answer
Because the split must imitate deployment, where the model uses past data to predict the future.