Time-based splits (a.k.a. Temporal Cross-Validation, Rolling Window Validation)
What are Time-Based Splits?
- Time-based splits respect the chronological order of data.
- Unlike random splits, you never train on future data to predict the past.
- This is crucial for time series forecasting and any task where data has a temporal component (financial prediction, demand forecasting, sensor logs, etc.).
Key principle:
$\text{Training data time range } < \text{Validation data time range}$
Why not use K-Fold?
- Standard CV (random or stratified) shuffles data → mixes past & future.
- This leads to data leakage: the model “sees” the future during training.
- Example: If you’re forecasting sales for July, you can’t train on August data.
Common Time-Based CV Strategies
1. Expanding Window (Growing Training Set)
- Training set grows over time, test set is the next fixed chunk.
- Example:
- Fold 1: Train [Jan–Mar], Test [Apr]
- Fold 2: Train [Jan–Apr], Test [May]
- Fold 3: Train [Jan–May], Test [Jun]
2. Sliding Window (Rolling Window)
- Training set is a fixed-length moving window.
- Example (window = 3 months):
- Fold 1: Train [Jan–Mar], Test [Apr]
- Fold 2: Train [Feb–Apr], Test [May]
- Fold 3: Train [Mar–May], Test [Jun]
3. Blocked Splits (Single Holdout)
- Simple chronological split: train on first N%, test on last (1–N)%.
- Often used for initial baseline.
Example in Python (sklearn TimeSeriesSplit)
import numpy as np
from sklearn.model_selection import TimeSeriesSplit
X = np.arange(10).reshape(-1, 1)
y = np.arange(10)
tscv = TimeSeriesSplit(n_splits=3)
for train_idx, test_idx in tscv.split(X):
print("Train:", train_idx, "Test:", test_idx)
Output (example):
Train: [0 1 2 3] Test: [4 5]
Train: [0 1 2 3 4 5] Test: [6 7]
Train: [0 1 2 3 4 5 6 7] Test: [8 9]
Here:
- Training expands with each fold.
- Tests are strictly after the training window.
When to Use
Use time-based splits when:
- Predicting future from past (time series forecasting, stock predictions).
- Data has seasonality or temporal autocorrelation.
- Avoiding data leakage is critical.
Not necessary when:
- Data points are IID (independent and identically distributed).
- Labels don’t depend on time order.
Summary
- Time-based splits ensure temporal order is respected.
- Prevents training on future data.
- Implemented as expanding window, sliding window, or blocked holdout.
