Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
  1. Home
  2. Blog
  3. Cross-Validation for Time Series Data
AI & Machine Learning

Cross-Validation for Time Series Data

Shuffling rows lets a model learn from the future to predict the past. What to do instead, and how to size the folds so the result means something.

Updated 2 min readBy SpiderHunts Technologies

Free estimateNo obligation

Get a free estimate

Tell us what you need. A senior engineer reads every enquiry.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →

Quick answer — TL;DR

K-fold cross-validation assumes rows are independent, which time-ordered data is not. Use forward-chaining splits where every validation fold comes after its training data, and keep a final untouched period for the last honest check.

The assumption that fails

Ordinary k-fold cross-validation shuffles the data and rotates which portion is held out. It assumes each row is independent of the others, which is reasonable for, say, a set of images and false for anything with a time order.

With time-ordered data, shuffling puts January and March in training while February is held out. The model sees what happened on both sides of the gap, which is information no real prediction would have.

Forward chaining instead

The correct structure trains on a period and validates on the period immediately after, repeatedly, moving forward.

FoldTrain onValidate on
1Months 1-12Months 13-15
2Months 1-15Months 16-18
3Months 1-18Months 19-21
4Months 1-21Months 22-24

Each fold reflects a realistic situation: everything known up to a point, predicting what comes next. The scores can be averaged, but the spread across folds is at least as informative as the mean.

Sliding or expanding window

The table above uses an expanding window - training data grows each fold. The alternative is a sliding window of fixed length, dropping the oldest data as it moves.

  • Expanding suits stable relationships where more history helps.
  • Sliding suits businesses that change, where old data actively misleads.
  • Testing both answers a genuine question: does old data still help? If sliding wins, that is evidence your business has changed.

That comparison is worth running regardless of which you adopt, because it tells you something about the business rather than only about the model.

Gaps, and why you may need one

If predictions are made some distance ahead of when they are used, the validation split should reflect that. Forecasting six weeks out means the last six weeks before the validation period would not have been available.

Leaving that gap makes the test harder and more honest. Skipping it produces a score achievable only if data arrives instantly, which it does not.

Keep one period genuinely untouched

Cross-validation folds get used repeatedly while tuning, and tuning against them gradually leaks their information into the model's design. By the twentieth configuration, the score is optimistic.

Hold back a final period that is not looked at until you believe the work is finished, and check once. If that result disappoints, the honest response is to accept it rather than to keep adjusting until the number improves.

A test set you have optimised against is not a test set any more.

FAQ

Frequently asked questions

The questions readers ask us after this guide.

Still have a question?

Ask us directly — a senior engineer will get back to you.

Ask about your project

Can I ever shuffle time-ordered data?

Only where the ordering genuinely does not matter to the prediction. If you are forecasting anything, it matters.

How many folds should I use?

Enough to cover different conditions, with each validation period long enough to be meaningful. Three to five is common for monthly business data.

Should the folds be equal length?

Validation periods usually yes, for comparability. Training periods vary naturally with an expanding window.

What if I only have two years of data?

Use fewer folds and accept wider uncertainty. Manufacturing extra folds from short history produces confident-looking numbers with nothing behind them.

Keep reading

More on AI & Machine Learning

Start here

Want machine learning project details from us?

Tell us what you are trying to predict and roughly what data you hold. We will come back with an honest view on whether machine learning is the right tool, what the work would involve and a realistic cost range. If a spreadsheet would do the job, we will say so.

  1. You tell us what you needTwo minutes on the form, or a message on WhatsApp.
  2. A senior engineer reviews itAnd comes back with questions, a realistic range and an honest view on fit.
  3. Free 30-minute scoping callWe talk through scope, options and a realistic estimate — with no obligation.
Free estimateNo obligation

Talk to someone who builds this

Send a short brief and we will come back with an honest view and a realistic range.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →