Skip to content
tezvyn:

Time Series Cross-Validation

Source: otexts.comMediumHow cards are made

Time Series Cross-Validation

Unlike standard cross-validation that shuffles data, time series cross-validation respects the arrow of time. It evaluates a forecasting model by training only on past data to predict a future point, then rolling forward. The footgun is using random k-folds.

The mental model

Standard cross-validation assumes data points are independent, so it shuffles them randomly into folds. This breaks for time series data, which has a strict chronological order. You can't use knowledge from Tuesday to predict Monday. Time series cross-validation respects this "arrow of time" by ensuring the model is only ever trained on past data to predict future data, mimicking how it would operate in the real world.

How it works

This procedure is often called "evaluation on a rolling forecasting origin." Instead of random folds, you create a series of sequential splits. The process is as follows: first, you select an initial block of data for the first training set. Second, you train your model and use it to forecast the very next data point, which serves as your first test set (of size one). Third, you calculate the forecast error. Fourth, you expand the training set to include the point you just tested on. You then repeat this process, rolling the "origin" forward one step at a time, forecasting the next point, and adding it to the training set. The final evaluation metric is the average of the forecast errors across all these steps. To ensure reliable forecasts, the earliest observations are typically not used as test sets, as the initial training data would be too sparse.

When to use it

Use time series cross-validation whenever you are evaluating a forecasting model. This is the correct way to estimate a model's out-of-sample performance on any data with a temporal component, such as daily sales figures, hourly server loads, or minute-by-minute stock prices. It provides a much more realistic estimate of how the model will perform on new, unseen data compared to naive train-test splits or standard k-fold CV.

When not to use it

This method is specifically for time-ordered data. For problems where the data points are independent and have no inherent sequence, such as classifying images or predicting customer attributes from a static dataset, standard k-fold cross-validation is more appropriate and computationally efficient. Using time series CV on non-temporal data provides no benefit and adds unnecessary complexity.

One canonical example

Imagine you have 12 months of user activity data and want to forecast month 13. To evaluate your model, you might start with an initial training set of the first 6 months. Fold 1: Train on months 1-6, predict month 7. Fold 2: Train on months 1-7, predict month 8. Fold 3: Train on months 1-8, predict month 9. ...and so on, up to Fold 6, which trains on months 1-11 to predict month 12. You would then average the forecast errors from these 6 folds to get a robust performance estimate before using all 12 months of data to train your final model.

Interview question

Which cross-validation approach is most appropriate for evaluating a model that predicts daily sales figures for the upcoming week?

  • a.Standard k-fold cross-validation, as it efficiently uses all available data.
  • b.A single train-test split, using the first 80% of data for training and the last 20% for testing.
  • c.Stratified k-fold cross-validation, to maintain the proportion of high and low sales days.
  • d.Time series cross-validation, to mimic real-world forecasting by training on past data.Correct
Why?

Time series cross-validation is designed for temporal data like daily sales, ensuring the model is always trained on past data to predict future data, mimicking real-world forecasting. Standard k-fold cross-validation would incorrectly shuffle the data, allowing the model to train on future information.

Just read this? Test yourself on what you have been reading.

Read the original → otexts.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on time series — each one lists the topics its interview covers.

See open roles