Course contentsShow
← Machine Learning and Deep Learning
Lesson 2396 of 3,53852. Time Series and ForecastingFree lesson

Time Series Cross-Validation

Implementing expanding or sliding window CV; preventing data leakage while respecting temporal dependencies.

Time Series Cross-Validation

What You'll Learn

You'll learn how to properly validate time series models using expanding or sliding window strategies that respect temporal order and prevent data leakage.

Why Standard Cross-Validation Fails for Time Series

In standard k-fold CV, you randomly shuffle data into folds—but time series data has an inherent order. Using future data to predict the past creates data leakage, giving you unrealistically optimistic performance estimates that won't hold in production.

Think of it like studying for an exam: you can't learn from questions that haven't been asked yet!

Expanding Window Cross-Validation

The expanding window approach mimics real-world forecasting:

  1. Start with an initial training set (e.g., first 100 observations)
  2. Forecast the next period(s) and evaluate
  3. Add those periods to your training set
  4. Repeat, growing your training window each iteration

This respects temporal dependencies—you always train on the past and test on the future.

Sliding Window Cross-Validation

The sliding window maintains a fixed-size training window:

  1. Use observations 1-100 to predict 101-110
  2. Slide forward: use 11-110 to predict 111-120
  3. Continue sliding through your data

This is useful when recent data is more relevant than distant history, or when computational resources are limited.

Preventing Data Leakage

Critical rules:

  • Never use test data for feature engineering (rolling stats, scaling parameters)
  • Compute transformations only on training data, then apply to test
  • Respect forecast horizon—if predicting 7 days ahead, your test set must be 7+ days in the future

Key Takeaway: Time series cross-validation uses expanding or sliding windows to ensure you only learn from the past when predicting the future, maintaining temporal integrity and preventing optimistic bias in your validation metrics.