What is Cross-validation?
Time series cross-validation is essential for validating machine learning models and ensuring accurate forecasts. Unlike traditional k-fold cross-validation, time series validation requires specialized rolling-window techniques that respect temporal order. This comprehensive tutorial shows you how to perform cross-validation in Python using TimeGPT, including prediction intervals, exogenous variables, and model performance evaluation. One of the primary challenges in time series forecasting is the inherent uncertainty and variability over time, making it crucial to validate the accuracy and reliability of the models employed. Cross-validation, a robust model validation technique, is particularly adapted for this task, as it provides insights into the expected performance of a model on unseen data, ensuring the forecasts are reliable and resilient before being deployed in real-world scenarios. TimeGPT incorporates thecross_validation
method, designed to streamline the validation process for time series forecasting models. This functionality enables practitioners to rigorously test their forecasting models against historical data, with support for prediction intervals and exogenous variables. This tutorial will guide you through the nuanced process of conducting cross-validation within the NixtlaClient
class, ensuring your time series forecasting models are not just well-constructed, but also validated for trustworthiness and precision.
Why Use Cross-Validation for Time Series?
Cross-validation provides several critical benefits for time series forecasting:- Prevent overfitting: Test model performance across multiple time periods
- Validate generalization: Ensure forecasts work on unseen data
- Quantify uncertainty: Generate prediction intervals for risk assessment
- Compare models: Evaluate different forecasting approaches systematically
- Optimize hyperparameters: Fine-tune model parameters with confidence
How to Perform Cross-validation with TimeGPT
Step 1: Import Packages and Initialize NixtlaClient
First, we install and import the required packages and initialize the Nixtla client. We start off by initializing an instance ofNixtlaClient
.
Step 2: Load Example Data
Use the Peyton Manning dataset as an example. The dataset can be loaded directly from Nixtla’s S3 bucket:ds
), a target column (e.g., y
), and, if necessary, an identifier column (e.g., unique_id
) for multiple time series.Step 3: Implement Rolling-Window Cross-Validation
Thecross_validation
method within the TimeGPT class is an advanced functionality crafted to perform systematic validation on time series forecasting models. This method necessitates a dataframe comprising time-ordered data and employs a rolling-window scheme to meticulously evaluate the model’s performance across different time periods, thereby ensuring the model’s reliability and stability over time. The animation below shows how TimeGPT performs cross-validation.

Rolling-window cross-validation conceptually splits your dataset into multiple training and validation sets over time.
freq
: Frequency of your data (e.g.,'D'
for daily). If not specified, it will be inferred.id_col
,time_col
,target_col
: Columns representing series ID, timestamps, and target values.n_windows
: Number of separate validation windows.step_size
: Step size between each validation window.h
: Forecast horizon (e.g., the number of days ahead to predict).
cross_validation
assesses the model’s forecasting accuracy in each window, providing a robust view of the model’s performance variability over time and potential overfitting. This detailed evaluation ensures the forecasts generated are not only accurate but also consistent across diverse temporal contexts.
cross_validation
on the Peyton Manning dataset:
TimeGPT
) aligned with historical values (y
).
unique_id | ds | cutoff | y | TimeGPT |
---|---|---|---|---|
0 | 2015-12-17 | 2015-12-16 | 7.591862 | 7.939553 |
0 | 2015-12-18 | 2015-12-16 | 7.528869 | 7.887512 |
0 | 2015-12-19 | 2015-12-16 | 7.171657 | 7.766617 |
0 | 2015-12-20 | 2015-12-16 | 7.891331 | 7.931502 |
0 | 2015-12-21 | 2015-12-16 | 8.360071 | 8.312632 |
Step 4: Plot Cross-Validation Results
Visualize forecast performance for each cutoff period. Here’s an example plotting the last 100 rows of actual data along with cross-validation forecasts for each cutoff.




An example visualization of predicted vs. actual values in the Peyton Manning dataset.
Step 5: Generate Prediction Intervals for Model Uncertainty
It is also possible to generate prediction intervals during cross-validation. To do so, we simply use thelevel
argument.
unique_id | ds | cutoff | y | TimeGPT | TimeGPT-hi-80 | TimeGPT-hi-90 | TimeGPT-lo-80 | TimeGPT-lo-90 | |
---|---|---|---|---|---|---|---|---|---|
0 | 0 | 2015-12-17 | 2015-12-16 | 7.591862 | 7.939553 | 8.201465 | 8.314956 | 7.677642 | 7.564151 |
1 | 0 | 2015-12-18 | 2015-12-16 | 7.528869 | 7.887512 | 8.175414 | 8.207470 | 7.599609 | 7.567553 |
2 | 0 | 2015-12-19 | 2015-12-16 | 7.171657 | 7.766617 | 8.267363 | 8.386674 | 7.265871 | 7.146560 |
3 | 0 | 2015-12-20 | 2015-12-16 | 7.891331 | 7.931502 | 8.205929 | 8.369983 | 7.657075 | 7.493020 |
4 | 0 | 2015-12-21 | 2015-12-16 | 8.360071 | 8.312632 | 9.184893 | 9.625794 | 7.440371 | 6.999469 |





An example visualization of predicted vs. actual values in the Peyton Manning dataset with prediction intervals.
Step 6: Enhance Forecasts with Exogenous Variables
Time Features
It is possible to include exogenous variables when performing cross-validation. Here we use thedate_features
parameter to create labels for each month. These features are then used by the model to make predictions during cross-validation.
unique_id | ds | cutoff | y | TimeGPT | TimeGPT-hi-80 | TimeGPT-hi-90 | TimeGPT-lo-80 | TimeGPT-lo-90 | |
---|---|---|---|---|---|---|---|---|---|
0 | 0 | 2015-12-17 | 2015-12-16 | 7.591862 | 8.426320 | 8.721996 | 8.824101 | 8.130644 | 8.028540 |
1 | 0 | 2015-12-18 | 2015-12-16 | 7.528869 | 8.049962 | 8.452083 | 8.658603 | 7.647842 | 7.441321 |
2 | 0 | 2015-12-19 | 2015-12-16 | 7.171657 | 7.509098 | 7.984788 | 8.138017 | 7.033409 | 6.880180 |
3 | 0 | 2015-12-20 | 2015-12-16 | 7.891331 | 7.739536 | 8.306914 | 8.641355 | 7.172158 | 6.837718 |
4 | 0 | 2015-12-21 | 2015-12-16 | 8.360071 | 8.027471 | 8.722828 | 9.152306 | 7.332113 | 6.902636 |





An example visualization of predicted vs. actual values in the Peyton Manning dataset with time features.
Dynamic Features
Additionally you can pass dynamic exogenous variables to better inform TimeGPT about the data. You just simply have to add the exogenous regressors after the target column.TimeGPT
considering this information


An example visualization of predicted vs. actual values in the electricity dataset with dynamic exogenous variables.
Step 7: Long-Horizon Forecasting with TimeGPT
Also, you can generate cross validation for different instances ofTimeGPT
using the model
argument. Here we use the base model and the model for long-horizon forecasting.


An example visualization of predicted vs. actual values in the electricity dataset with dynamic exogenous variables and long horizon forecasting.
Frequently Asked Questions
What is time series cross-validation? Time series cross-validation is a model validation technique that uses rolling windows to evaluate forecasting accuracy while preserving temporal order, ensuring reliable predictions on unseen data. How is time series cross-validation different from k-fold cross-validation? Unlike k-fold cross-validation which randomly shuffles data, time series cross-validation maintains temporal order using techniques like walk-forward validation and expanding windows to prevent data leakage. What are the key parameters for cross-validation in TimeGPT? Key parameters includeh
(forecast horizon), n_windows
(number of validation windows), step_size
(window increment), and level
(prediction interval confidence levels).
How do you evaluate cross-validation results?
Evaluate results by comparing forecasted values against actual values across multiple time windows, analyzing prediction intervals, and calculating metrics like MAE, RMSE, and MAPE.
Conclusion
You’ve mastered time series cross-validation with TimeGPT, including rolling-window validation, prediction intervals, exogenous variables, and long-horizon forecasting. These model validation techniques ensure your forecasts are accurate, reliable, and production-ready.Next Steps in Model Validation
- Explore evaluation metrics to quantify forecast accuracy
- Learn about fine-tuning TimeGPT for domain-specific data
- Apply cross-validation to multiple time series