skfolio.model_selection.CombinatorialPurgedCV#

class skfolio.model_selection.CombinatorialPurgedCV(n_folds=10, n_test_folds=8, purged_size=0, embargo_size=0)[source]#

Combinatorial Purged Cross-Validation.

Provides train/test indices to split time series data samples based on Combinatorial Purged Cross-Validation [1].

Compared to KFold, which splits the data into k folds with 1 fold for the test set and k - 1 folds for the training set, CombinatorialPurgedCV uses k - p folds for the training set with p > 1 being the number of test folds.

KFold can recombine one single testing path while CombinatorialPurgedCV can recombine multiple testing paths from the combinations of the train/test sets.

To avoid data leakage, purging and embargoing can be performed.

Purging consist of removing from the training set all observations whose labels overlapped in time with those labels included in the testing set.

Embargoing consist of removing from the training set all observations that immediately follow an observation in the testing set, since financial features often incorporate series that exhibit serial correlation (like ARMA processes).

Parameters:
n_foldsint, default=10

Number of folds. Must be at least 3.

n_test_foldsint, default=8

Number of test folds. Must be at least 2. For only one test fold, use sklearn.model_validation.KFold.

purged_sizeint, default=0

Number of observations to exclude from the start of each train set that are after a test set and the number of observations to exclude from the end of each training set that are before a test set.

embargo_sizeint, default=0

Number of observations to exclude from the start of each training set that are after a test set.

Attributes:
index_train_test_ndarray of shape (n_observations, n_splits)

References

[1]

“Advances in Financial Machine Learning”, Marcos López de Prado (2018)

Examples

>>> import numpy as np
>>> from skfolio.model_selection import CombinatorialPurgedCV
>>> X = np.random.randn(12, 2)
>>> cv = CombinatorialPurgedCV(n_folds=3, n_test_folds=2)
>>> for i, (train_index, tests) in enumerate(cv.split(X)):
...     print(f"Split {i}:")
...     print(f"  Train: index={train_index}")
...     for j, test_index in enumerate(tests):
...         print(f"  Test {j}:  index={test_index}")
Split 0:
  Train: index=[ 8  9 10 11]
  Test 0:  index=[0 1 2 3]
  Test 1:  index=[4 5 6 7]
Split 1:
  Train: index=[4 5 6 7]
  Test 0:  index=[0 1 2 3]
  Test 1:  index=[ 8  9 10 11]
Split 2:
  Train: index=[0 1 2 3]
  Test 0:  index=[4 5 6 7]
  Test 1:  index=[ 8  9 10 11]
>>> cv = CombinatorialPurgedCV(n_folds=3, n_test_folds=2, purged_size=1)
>>> for i, (train_index, tests) in enumerate(cv.split(X)):
...     print(f"Split {i}:")
...     print(f"  Train: index={train_index}")
...     for j, test_index in enumerate(tests):
...         print(f"  Test {j}:  index={test_index}")
Split 0:
  Train: index=[ 9 10 11]
  Test 0:  index=[0 1 2 3]
  Test 1:  index=[4 5 6 7]
Split 1:
  Train: index=[5 6]
  Test 0:  index=[0 1 2 3]
  Test 1:  index=[ 8  9 10 11]
Split 2:
  Train: index=[0 1 2]
  Test 0:  index=[4 5 6 7]
  Test 1:  index=[ 8  9 10 11]
>>> cv = CombinatorialPurgedCV(n_folds=3, n_test_folds=2, embargo_size=1)
>>> for i, (train_index, tests) in enumerate(cv.split(X)):
...     print(f"Split {i}:")
...     print(f"  Train: index={train_index}")
...     for j, test_index in enumerate(tests):
...         print(f"  Test {j}:  index={test_index}")
Split 0:
  Train: index=[ 9 10 11]
  Test 0:  index=[0 1 2 3]
  Test 1:  index=[4 5 6 7]
Split 1:
  Train: index=[5 6 7]
  Test 0:  index=[0 1 2 3]
  Test 1:  index=[ 8  9 10 11]
Split 2:
  Train: index=[0 1 2 3]
  Test 0:  index=[4 5 6 7]
  Test 1:  index=[ 8  9 10 11]

Methods

get_path_ids()

Return the path id of each test sets in each split

plot_train_test_folds()

Plot the train/test fold locations

plot_train_test_index(X)

Plot the training and test indices for each combinations by assigning 0 to training, 1 to test and -1 to both purge and embargo indices.

split(X[, y, groups])

Generate indices to split data into training and test set.

summary

property binary_train_test_sets#

Identify training and test folds for each combinations by assigning 0 to training folds and 1 to test folds

get_path_ids()[source]#

Return the path id of each test sets in each split

property n_splits#

Number of splits

property n_test_paths#

Number of test paths that can be reconstructed from the train/test combinations

plot_train_test_folds()[source]#

Plot the train/test fold locations

plot_train_test_index(X)[source]#

Plot the training and test indices for each combinations by assigning 0 to training, 1 to test and -1 to both purge and embargo indices.

property recombined_paths#

Recombine each test path by returning the test set location in each split.

split(X, y=None, groups=None)[source]#

Generate indices to split data into training and test set.

Parameters:
Xarray-like of shape (n_samples, n_features)

Training data, where n_samples is the number of samples and n_features is the number of features.

yarray-like of shape (n_samples,), optional

The (multi-)target variable

groupsarray-like of shape (n_samples,), optional

Group labels for the samples used while splitting the dataset into train/test set.

Yields:
trainndarray

The training set indices for that split.

testndarray

The testing set indices for that split.

property test_set_index#

Location of each test set