ontime.module.processing.cross_validation#

ontime.module.processing.cross_validation(ts: TimeSeries, n_splits: int, strategy: str = 'expanding', initial_train_size: int | float | None = None, horizon: int = 1, gap: int = 0, stride: int | None = None) List[Tuple[TimeSeries, TimeSeries]]#

Split a TimeSeries into a list of (train, test) folds for time series cross-validation.

Three strategies are supported:

  • "expanding": the train set is anchored at the start of the series and grows at each fold, while the test set slides forward. Setting horizon=1 yields walk-forward validation; setting horizon to more than 1 yields multi-horizon evaluation.

  • "sliding": the train set has a fixed size and slides forward at each fold together with the test set (rolling window).

  • "blocked": the series is partitioned into n_splits + 1 contiguous, non-overlapping blocks of equal size. For each fold, one block is used as train and the next one as test, which avoids the train set growing across folds and limits leakage from far away blocks.

In every strategy, a gap can be set between the end of the train set and the start of the test set to implement purged / embargoed cross-validation, guarding against leakage due to autocorrelation.

Parameters:
  • ts – TimeSeries to split

  • n_splits – int number of folds to generate

  • strategy – str one of “expanding”, “sliding”, “blocked”

  • initial_train_size – int or float, size of the (first) train set. If a float in (0, 1), it is interpreted as a fraction of the length of ts. If None, it is computed automatically so that n_splits folds fit in the series (ignored for “blocked”).

  • horizon – int length of the test set of each fold (ignored for “blocked”, where it is equal to the block size)

  • gap – int number of points dropped between the end of the train set and the start of the test set of each fold

  • stride – int number of points between the start of consecutive test sets. Defaults to horizon (ignored for “blocked”)

Returns:

list of (train, test) TimeSeries tuples, one per fold