<a id="skfolio-model-selection-combinatorialpurgedcv"></a>

# skfolio.model_selection.CombinatorialPurgedCV

<a id="skfolio.model_selection.CombinatorialPurgedCV"></a>

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

Combinatorial Purged Cross-Validation.

Provides train/test indices to split time series data samples based on
Combinatorial Purged Cross-Validation [[1]](#re93330c75414-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 consists of removing from the training set all observations whose labels
overlapped in time with those labels included in the testing set.

Embargoing consists 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_folds** *int, default=10*
  : Number of folds. Must be at least 3.

  **n_test_folds** *int, default=8*
  : Number of test folds. Must be at least 2.
    For only one test fold, use `sklearn.model_validation.KFold`.

  **purged_size** *int, 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_size** *int, 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)*

### Methods

| [`get_n_splits`](#skfolio.model_selection.CombinatorialPurgedCV.get_n_splits)([X, y, groups])   | Return the number of splitting iterations in the cross-validator.                                                                              |
|---------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| [`get_path_ids`](#skfolio.model_selection.CombinatorialPurgedCV.get_path_ids)()                 | Return the path id of each test sets in each split.                                                                                            |
| [`plot_train_test_folds`](#skfolio.model_selection.CombinatorialPurgedCV.plot_train_test_folds)()        | Plot the train/test fold locations.                                                                                                            |
| [`plot_train_test_index`](#skfolio.model_selection.CombinatorialPurgedCV.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`](#skfolio.model_selection.CombinatorialPurgedCV.split)(X[, y, groups])          | Generate indices to split data into training and test set.                                                                                     |

| **summary**   |    |
|---------------|----|

### References

* <a id='re93330c75414-1'>**[1]**</a> “Advances in Financial Machine Learning”, Marcos López de Prado (2018)

### Examples

Tutorials using `CombinatorialPurgedCV`:
: * [Drop Highly Correlated Assets](https://skfolio.org/auto_examples/pre_selection/plot_1_drop_correlated.html.md#sphx-glr-auto-examples-pre-selection-plot-1-drop-correlated-py)
  * [HRP vs HERC](https://skfolio.org/auto_examples/clustering/plot_3_hrp_vs_herc.html.md#sphx-glr-auto-examples-clustering-plot-3-hrp-vs-herc-py)
  * [NCO - Combinatorial Purged CV](https://skfolio.org/auto_examples/clustering/plot_5_nco_grid_search.html.md#sphx-glr-auto-examples-clustering-plot-5-nco-grid-search-py)

```pycon
>>> 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]
```

<a id="skfolio.model_selection.CombinatorialPurgedCV.binary_train_test_sets"></a>

#### *property* binary_train_test_sets

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

<a id="skfolio.model_selection.CombinatorialPurgedCV.get_n_splits"></a>

#### get_n_splits(X=None, y=None, groups=None)

Return the number of splitting iterations in the cross-validator.

* **Parameters:**
  **X** *object*
  : Always ignored, exists for compatibility.

  **y** *object*
  : Always ignored, exists for compatibility.

  **groups** *object*
  : Always ignored, exists for compatibility.
* **Returns:**
  **n_splits** *int*
  : Number of splitting iterations in the cross-validator.

<a id="skfolio.model_selection.CombinatorialPurgedCV.get_path_ids"></a>

#### get_path_ids()

Return the path id of each test sets in each split.

<a id="skfolio.model_selection.CombinatorialPurgedCV.n_splits"></a>

#### *property* n_splits

Number of splits.

<a id="skfolio.model_selection.CombinatorialPurgedCV.n_test_paths"></a>

#### *property* n_test_paths

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

<a id="skfolio.model_selection.CombinatorialPurgedCV.plot_train_test_folds"></a>

#### plot_train_test_folds()

Plot the train/test fold locations.

<a id="skfolio.model_selection.CombinatorialPurgedCV.plot_train_test_index"></a>

#### 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.

<a id="skfolio.model_selection.CombinatorialPurgedCV.recombined_paths"></a>

#### *property* recombined_paths

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

<a id="skfolio.model_selection.CombinatorialPurgedCV.split"></a>

#### split(X, y=None, groups=None)

Generate indices to split data into training and test set.

* **Parameters:**
  **X** *array-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.

  **y** *array-like of shape (n_samples,), optional*
  : The (multi-)target variable

  **groups** *array-like of shape (n_samples,), optional*
  : Group labels for the samples used while splitting the dataset into
    train/test set.
* **Yields:**
  **train** *ndarray*
  : The training set indices for that split.

  **test** *ndarray*
  : The testing set indices for that split.

<a id="skfolio.model_selection.CombinatorialPurgedCV.test_set_index"></a>

#### *property* test_set_index

Location of each test set.

