Home Knowledge Base Time Series Cross-Validation

Time Series Cross-Validation is a specialized evaluation strategy for temporal data that respects chronological order — training only on past data and testing on future data in each fold, because standard K-Fold cross-validation shuffles data randomly and would use "future" information to predict "the past" (a devastating form of data leakage that produces impossibly optimistic performance estimates for stock prices, sales forecasts, weather predictions, and any time-dependent prediction task).

What Is Time Series Cross-Validation?

Standard K-Fold vs Time Series Split

AspectStandard K-FoldTime Series Split
Data orderShuffled randomlyChronological order preserved
Future in training?Yes ⚠️ (data leakage)Never ✓
Training windowRandom subsetPast data only (expanding or sliding)
Test windowRandom subsetNext future period only

How Time Series Split Works (Expanding Window)

FoldTraining DataTest DataGap
1Jan - MarAprNone
2Jan - AprMayNone
3Jan - MayJunNone
4Jan - JunJulNone
5Jan - JulAugNone

Training window expands each fold. The model always predicts the next unseen period.

Sliding Window Variant

FoldTraining DataTest DataWindow Size
1Jan - MarApr3 months
2Feb - AprMay3 months
3Mar - MayJun3 months
4Apr - JunJul3 months

Fixed-size training window — older data drops off, simulating concept drift.

Python Implementation

from sklearn.model_selection import TimeSeriesSplit

tscv = TimeSeriesSplit(n_splits=5, gap=0)
for train_idx, test_idx in tscv.split(X):
    X_train, X_test = X[train_idx], X[test_idx]
    y_train, y_test = y[train_idx], y[test_idx]

# With gap (e.g., 7-day gap to prevent immediate correlation leakage)
tscv = TimeSeriesSplit(n_splits=5, gap=7)

The Gap Parameter

GapPurposeUse Case
gap=0Test immediately after training periodStandard forecasting
gap=77-day buffer between train and testAvoid autocorrelation from consecutive days
gap=3030-day bufferMonthly forecasting with weekly seasonality

Common Applications

DomainTime UnitTypical Setup
Stock PredictionDailyTrain on 2 years, test on next month
Sales ForecastingWeeklyTrain on 52 weeks, test on next 4 weeks
WeatherHourlyTrain on 6 months, test on next week
Demand PlanningDailyExpanding window, 1-week test horizon

Time Series Cross-Validation is the only correct evaluation strategy for temporal data — respecting the chronological ordering that standard K-Fold violates, preventing the future-to-past data leakage that produces unrealistically optimistic performance estimates, and simulating the real deployment scenario where models must always predict from historically available data.

time series splittemporalorder

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.