Paperlive
HOME / BLOG / TIME SERIES CROSS VALIDATION: EXPANDING & SLIDING WINDOWS GUIDE

Time Series Cross Validation: Expanding & Sliding Windows Guide

Time Series Cross Validation: Expanding & Sliding Windows Guide

Time Series Cross Validation: Methods, Pitfalls, and Best Practices for Forecasting

Quick Answer: Time series cross validation evaluates forecasting models by always training on past data and testing on future data. Instead of shuffling rows like standard k-fold, it uses expanding or sliding windows that respect chronological order. This prevents look-ahead bias and produces performance estimates you can actually trust in production.

Forecasting models are easy to overrate. Shuffle your data, run ordinary k-fold, and you may see an impressive error score that collapses in the first week of deployment. The reason is simple: the model was allowed to learn from the future. Time series cross validation fixes this by recreating the way forecasts are actually made. This guide covers the main methods, how to size your windows, and the subtle mistakes that inflate results.

Key Takeaways

  • Standard k-fold cross validation leaks future information when data is ordered in time.
  • Time series cross validation always tests on data that comes after the training period.
  • Expanding window and sliding window are the two core strategies.
  • A gap between training and test sets prevents leakage from lag features.
  • Your test window should match the forecast horizon you use in production.

Why Standard K-Fold Fails for Time Series

K-fold cross validation assumes observations are independent. Time series data is not. Today's sales depend on yesterday's sales, last week's promotion, and the season. When you randomly split such data, the training set contains points from after the test period. The model effectively peeks at the future.

The problem gets worse with engineered features. Lag values, rolling averages, and exponentially weighted means all carry information across neighboring time steps. Random splits turn those features into leakage channels. The result is an optimistic error estimate and, more dangerously, the wrong model being selected. This is exactly why time series cross validation exists.

What Is Time Series Cross Validation?

Time series cross validation, also known as rolling-origin evaluation or backtesting, creates several train-test splits along the timeline. Each split has a forecast origin:

  • The model is trained on everything before that origin.
  • It is evaluated on the period immediately after it.
  • The origin then moves forward, and the process repeats.

Averaging errors across all splits gives a far more reliable estimate than a single holdout period, because it tests the model across different seasons, trends, and events. Forecasting expert Rob Hyndman helped popularize this approach as "evaluation on a rolling forecasting origin."

The Main Time Series Cross Validation Methods

1. Expanding Window

The training set starts small and grows with each fold, while the test window stays the same size. Fold one trains on months 1 to 12 and tests on month 13. Fold two trains on months 1 to 13 and tests on month 14, and so on.

Use it when older data remains relevant and more history improves the model.

2. Sliding (Rolling) Window

The training set has a fixed size and moves forward. Fold one trains on months 1 to 12, fold two on months 2 to 13.

Use it when the data changes over time and old patterns may hurt the model, as in fast-moving markets or products with short life cycles.

3. Walk-Forward Validation

This is a stricter version in which the model is retrained at every step and forecasts one period at a time, just as it would in production. It is the most realistic option but also the most computationally expensive.

4. Blocked Cross Validation

The series is divided into non-overlapping blocks, each with its own training and test segment. Margins between blocks reduce dependence. It is useful when you have long series and want independent evaluations.

5. Purged and Embargoed K-Fold

Marcos López de Prado popularized this method in Advances in Financial Machine Learning (2018). It does two things:

  • Purging removes training samples whose labels overlap with the test period.
  • Embargo drops a buffer after each test fold.

It is standard practice in quantitative finance, where labels often span multiple days.

How to Choose Window Sizes and Folds

Good time series cross validation depends on sensible configuration:

  • Test window size: match your real forecast horizon. If you forecast 14 days ahead, test on 14-day windows.
  • Initial training size: include at least two full seasonal cycles when seasonality matters, such as two years of data for yearly patterns.
  • Number of folds: three to ten is typical. More folds give more stable estimates but cost more compute.
  • Step size: this is how far the origin moves each time. Moving by the horizon length avoids overlapping test sets.

Use a Gap to Prevent Hidden Leakage

Even with correct ordering, leakage can occur at the boundary. If a feature uses a 7-day rolling average, the first test days share information with the last training days. Adding a gap equal to your longest lookback or your forecast lead time removes this overlap.

A gap also reflects real operations. If your data pipeline delivers yesterday's numbers with a two-day delay, your validation should include that delay too.

Hyperparameter Tuning with Nested Time Series Cross Validation

Tuning hyperparameters and estimating performance on the same folds produces biased results. Nested time series cross validation solves this with two loops:

  • An outer loop moves the forecast origin for the final evaluation.
  • An inner loop selects hyperparameters, using only data before that origin.

It is slower, but it gives honest numbers for model comparison.

Handling Multiple Time Series

In retail or energy forecasting, you may have thousands of series. When applying time series cross validation to many series, split by time, not by series, so every series shares the same training cutoff. This matches production, where a global model forecasts all series from the same date.

Report errors both overall and for key segments, since averages can hide poor performance on important products.

Choosing the Right Error Metrics

  • MAE: easy to interpret in original units.
  • RMSE: penalizes large errors more heavily.
  • MAPE: intuitive, but breaks when actual values are zero or near zero.
  • WAPE: a volume-weighted alternative preferred in retail.
  • MASE: scale-free and comparable across series. Hyndman and Koehler introduced it in 2006.

Within each fold of your time series cross validation, record the error separately. Do not report just the average, because large variation between folds signals instability.

Tools That Support Time Series Cross Validation

  • scikit-learn TimeSeriesSplit: supports expanding windows, fixed test sizes, a gap parameter, and a maximum training size for sliding windows.
  • sktime: offers ExpandingWindowSplitter and SlidingWindowSplitter with forecasting-specific evaluation.
  • Nixtla StatsForecast and MLForecast: built-in cross validation functions designed for many series at scale.
  • Prophet: includes a cross validation utility with initial, period, and horizon settings.
  • Darts: provides backtesting through historical forecasts.

Common Mistakes to Avoid

Even careful time series cross validation can be undermined by these errors:

  • Scaling or normalizing with the full dataset before splitting.
  • Computing rolling features across the train-test boundary without a gap.
  • Using a test window shorter than the real forecast horizon.
  • Relying on a single holdout period that happens to be unusually calm.
  • Ignoring structural breaks, such as a pandemic period, when interpreting fold results.

Frequently Asked Questions

Can I use k-fold cross validation for time series at all?

Only in limited cases. Research by Bergmeir and colleagues showed it can work for purely autoregressive models with uncorrelated residuals. For most practical projects, time series cross validation is the safer choice.

How many folds should I use in time series cross validation?

Three to ten folds usually balance reliability and compute cost. Make sure each fold covers meaningful seasonal variation.

Is walk-forward validation the same as time series cross validation?

Walk-forward validation is a specific, strict form of time series cross validation in which the model is retrained at each step.

Should I retrain the model in every fold?

Yes, if that reflects production. If you retrain monthly in production, retrain monthly in validation.

Final Thoughts

Time series cross validation is the difference between a forecast that looks good on paper and one that holds up in the real world. To get it right:

  • Respect chronological order.
  • Match your test window to your horizon.
  • Add gaps where features overlap.
  • Tune with nested folds.

When your time series cross validation mirrors production, your offline metrics finally become a reliable guide.

Ready to become a DevOps engineer?
Industry-certified DevOps training online · 100% job-opportunity guarantee
Get Curriculum →

Get the curriculum

DevOps Course Online

100% secure · no spam · callback in 30 min

Recommended Course

Online DevOps course
BESTSELLER
AWSAzureGCPGen AI
Online DevOps Course
Job-Ready Program

Learn from Microsoft-certified experts with real projects, internship certification and dedicated placement support to help you land your next DevOps role.

6 Months
Duration
5–45 LPA
Opportunity range
300+ Hrs
Live sessions
IIT Patna
Certification
Next batch closing soon — limited seats
Live classes · 23 Nov
View full curriculum
© 2025 PAPERLIVE, All rights reserved

Disclaimer: PaperLive Learning offers skill-based training and professional upskilling courses only. Our programs are not postgraduate (PG) programs, diploma courses, or formal academic qualifications and do not award degrees or diplomas.

We strive to ensure that the information and pricing displayed on our website are accurate. However, course fees, content, duration, and promotional offers may change without prior notice. Applicable pricing at the time of enrollment will be communicated clearly.

Career outcomes depend on individual skills, effort, experience, performance, and market conditions. Placement statistics, salary figures, and success stories are for informational purposes only and do not guarantee similar results.

References to third-party platforms, certifications, and tools are for educational purposes only and do not imply endorsement or affiliation unless explicitly stated.