# Purged k-fold and CPCV

How do purging and embargo prevent overlapping labels from leaking across folds?

Remove training labels whose outcome intervals overlap a test interval, then embargo the adjacent period required by the horizon. CPCV combines several chronological test groups; its illustrative paths and the documented ML workflow need executable verification before claiming app support.

Published validation methods do not prove these Stochastly nodes execute correctly in a graph; the graph effect must be tested before availability claims.

Financial labels can use future bars. Purging removes training labels that overlap test labels; an embargo excludes nearby observations after a test block.

## Why ordinary folds leak

A sample dated before a test fold may still have a label ending inside that fold. Training on that label exposes part of the test outcome. Purged k-fold removes overlapping label intervals from training. An embargo creates an additional separation after the test interval for features with serial dependence.

## An interval check

Suppose a test block covers bars 10 through 19 and a training label starts at bar 8 but ends at bar 12. The intervals intersect on bars 10 through 12, so that row is purged. With a two-bar post-test embargo, candidate training rows at bars 20 and 21 are excluded too. The exact interval boundaries must follow the label definition.

```
Purge train label i when [start_i, end_i] intersects a test-label interval
```

## Combinatorial paths

Combinatorial purged cross-validation (CPCV) selects several test groups from chronological blocks, purging and embargoing each split. With six blocks and two test blocks per split, the number of test combinations is 6! / (2! × 4!) = 15. The method yields multiple out-of-sample paths, with dependence between paths that reuse observations.

```
Number of combinations = n! / (k! × (n - k)!)
```

For N = 6 groups and k = 2 test groups, C(6,2) = 15 splits and phi = k/N × C(N,k) = 5 paths. Each group is tested in C(5,1) = 5 splits. Boundary bands are schematic: actual purge and embargo exclusions depend on label intervals and the chosen horizon.

## Related but distinct CSCV

Combinatorially symmetric cross-validation (CSCV) ranks an in-sample winner against other candidates out of sample to estimate PBO. Its symmetric half-splits answer a selection question. CPCV adds label-aware purge and embargo to a model validation scheme. The documented ML walk-forward design includes a horizon-aware embargo, while the documented PBO design uses CSCV splits. Executable graph behavior and its effect on output need separate tests before either is presented as an available workflow.

## Frequently asked questions

### What does purging remove?

Training observations whose label intervals overlap a test label interval.

### Is an embargo a substitute for purging?

No. Purging checks label overlap; embargo excludes a neighboring period after the test block.

### Are CPCV and CSCV interchangeable?

No. CPCV organizes purged model-validation paths. CSCV uses symmetric splits to assess candidate selection and PBO.

## Sources

López de Prado (2018). [Advances in Financial Machine Learning](https://www.wiley.com/en-us/Advances+in+Financial+Machine+Learning-p-9781119482086). Wiley, chapters 7 and 12.

Bailey, Borwein, López de Prado and Zhu (2017). [The Probability of Backtest Overfitting](https://doi.org/10.21314/JCF.2016.322). Journal of Computational Finance 20(4), 39-69.

Arnott, Harvey and Markowitz (2019). [A Backtesting Protocol in the Era of Machine Learning](https://doi.org/10.3905/jfds.2019.1.064). Journal of Financial Data Science 1(1), 64-74.

[Primary source for Purged k-fold and CPCV](https://www.wiley.com/en-us/Advances+in+Financial+Machine+Learning-p-9781119482086)

## In the library

Combinatorial Purged Cross-Validation (CPCV) + Purging & Embargo

Purged K-Fold & CPCV

[When Cross-Validation Is Valid on Time Series, and When It Is Not](https://stochastly.com/library/machine-learning/when-cross-validation-is-valid-on-time-series)

Dangers of standard k-fold CV in finance (leakage)

## Related

[PBO calculator](https://stochastly.com/tools/probability-of-backtest-overfitting-calculator)

[Probability of backtest overfitting](https://stochastly.com/learn/probability-of-backtest-overfitting)

Purged k-fold and CPCV

---
Source: https://stochastly.com/learn/purged-k-fold-and-cpcv
