quantlab.model.split#

Splits of a timestamp axis: purged segments, in/out-of-sample ranges and walk-forward folds.

A label at bar t reads bars up to t + L, L being its lookahead. A segment followed by another therefore loses its last L bars, so no label fitted on it reads a bar of the next segment. The model’s train/validation/test splits, the backtest’s in-sample/out-of-sample split (in_sample_window, split_ranges) and the walk-forward folds all go through purge_segments.

A walk-forward cross-validation trains on a window of bars and tests on the bars right after it, then steps forward by one test segment. Fold i tests on the test_periods bars from position i * test_periods + train_periods on, and its training window ends right before them. The window starts at position i * test_periods (sliding) or at the first bar (expanding), so both modes test on the same bars. There are max(1, (T - train_periods) // test_periods) folds, T being the number of bars, and a fold whose test segment runs past the end is skipped. test_periods defaults to train_periods // 5. The training window loses its last purge_bars bars before it is fitted; each Fold carries both windows, the one configured (which a model is given and purges itself) and the one actually fitted.

The module imports no quantlab layer: the caller computes purge_bars from its labels and cuts the axis to its own date range. Model and ensemble cross-validation both plan their folds here.

Examples#

>>> import numpy as np
>>> bars = np.arange("2024-01-01", "2024-01-21", dtype="datetime64[D]")
>>> folds = walk_forward_folds(bars, 10, test_periods=5, purge_bars=2)
>>> len(folds)
2
>>> folds[1]
Fold(index=1, train_window=('2024-01-06', '2024-01-15'), fitted_train_window=('2024-01-06', '2024-01-13'), test_window=('2024-01-16', '2024-01-20'))

Classes

Fold

One walk-forward fold; every window is (start, end), both ends inclusive.

Functions

in_sample_window

Return the first and last bar a fitted model has seen.

purge_segments

Return each segment's usable timestamps after purging.

split_ranges

Cut a backtest's bars into in-sample and out-of-sample runs.

walk_forward_folds

Return the walk-forward folds of a timestamp axis.