In this example, we’ll implement time series cross-validation to evaluate model’s performance.
Prerequesites This tutorial assumes basic familiarity with StatsForecast. For a minimal example visit the Quick Start
Tip You can use Colab to run this Notebook interactively![]()
pip install statsforecast
pandas
.
unique_id | ds | y | |
---|---|---|---|
0 | H1 | 1 | 605.0 |
1 | H1 | 2 | 586.0 |
2 | H1 | 3 | 586.0 |
3 | H1 | 4 | 559.0 |
4 | H1 | 5 | 511.0 |
StatsForecast
is a data frame in long
format with
three columns: unique_id
, ds
and y:
unique_id
(string, int, or category) represents an identifier
for the series.ds
(datestamp or int) column should be either an integer
indexing time or a datestamp in format YYYY-MM-DD or YYYY-MM-DD
HH:MM:SS.y
(numeric) represents the measurement we wish to forecast.unique_id == 'H1'
. However, you can use as many as you want, with no
additional changes to the code needed.
StatsForecast.plot
method.
statsforecast.models
and then we need
to instantiate a new
StatsForecast
object.
The
StatsForecast
object has the following parameters:
df
.
StatsForecast
object
has been instantiated, we can use the
cross_validation
method, which takes the following arguments:
df
: training data frame with
StatsForecast
formath
(int): represents the h steps into the future that will be
forecastedstep_size
(int): step size between each window, meaning how often
do you want to run the forecasting process.n_windows
(int): number of windows used for cross-validation,
meaning the number of forecasting processes in the past you want to
evaluate.cv_df
object is a new data frame that includes the following
columns:
unique_id
: series identifierds
: datestamp or temporal indexcutoff
: the last datestamp or temporal index for the n_windows.y
: true value"model"
: columns with the model’s name and fitted value.unique_id | ds | cutoff | y | AutoETS | |
---|---|---|---|---|---|
0 | H1 | 677 | 676 | 691.0 | 677.761053 |
1 | H1 | 678 | 676 | 618.0 | 607.817879 |
2 | H1 | 679 | 676 | 563.0 | 569.437729 |
3 | H1 | 680 | 676 | 529.0 | 537.340007 |
4 | H1 | 681 | 676 | 504.0 | 515.571123 |
y
before said period.
AutoETS
.Tip Cross validation is especially useful when comparing multiple models. Here’s an example with multiple models and time series.