Basic Prerequisites: Backtesting Hygiene. Everything else is built from scratch on this page.


1. Intuition & Practical Objective

This page builds the why of purged cross-validation with no prior ML-evaluation knowledge needed. The objective is one idea: when your training data contains a copy of the answer, the model “scores well” without learning anything - and financial labels are built out of copies of each other.

Start with the dumbest framing. You have data , . You want to know: does my model generalise to data it has never seen? The standard answer is cross-validation: split the data, train on one part, test on the other, and the test score measures generalisation - provided the two parts are independent. The word independent is doing all the work, and finance breaks it in a specific, measurable way.

Here is the mechanism, in three steps (López de Prado, AFML Ch. 7):

  1. The features are sticky. A serially correlated feature satisfies : today’s momentum, volatility, or order-flow is a near-twin of yesterday’s.
  2. The labels are made of shared data. A label like “sign of the next -bar return” at time uses bars ; the same label at uses bars . They share bars, so .
  3. The two near-duplicates land in different sets. The moment observation goes to training and goes to testing, the model has effectively seen the test answer in training. Train on , predict from , and because and , the prediction is right even when is pure noise.

The practical objective of this folder is to make step 3 impossible: purging deletes every training observation whose label overlaps a test label, embargoing deletes the serially-correlated observations right after the test block, and CPCV applies the honest split to every combination of test sets so you get a distribution of backtests instead of one fragile number.


2. Mathematical Ground Truth & Derivations

Why labels become near-duplicates. Let be the per-bar return and define the overlapping label at bar index by its evaluation window :

The two sums share of terms, so if are iid with , the correlation between the two sums is

For jointly normal sums, the probability the two signs agree is (Sheppard’s formula) , which for grows monotonically toward . Long-horizon labels are the most redundant - and therefore the most leakage-prone.

Why standard -fold CV misses it. Standard CV’s guarantee assumes each observation is drawn independently. When is built from overlapping windows, consecutive rows violate that: the “independent” test row is a function of nearly the same random draw as its training neighbour. Shuffling makes it worse - it interleaves the near-duplicate pairs across folds, so leakage is no longer confined to fold boundaries but is spread through every training set (AFML exercise 7.2: shuffled CV scores far higher than unshuffled on the same data).

The formal overlap (preview of page 03). Two interval labels and overlap - hence leak - if any of three sufficient conditions holds:

Purging removes from the training set every satisfying one of these against the test interval (AFML §7.4.1, Snippet 7.1).


3. Computational Implementation - seeing the redundancy with your own eyes

Stdlib only. This measures exactly how redundant overlapping labels are, as a function of the label horizon - the quantity that decides how much you must purge.

With (non-overlapping) the labels are fair coin flips () - independent, as CV expects. At an adjacent pair agrees of the time; at it agrees . That 0.93 is the ceiling a leaked classifier “earns” for free. If your CV reports higher accuracy than the agreement of adjacent labels on your noise floor, you are almost certainly reading leaked duplicates back.


4. Failure Modes & First-Principles Breakdowns

  1. “Shuffling is a neutral, harmless step” - false. Shuffling exists to break spurious ordering in IID data. In finance it spreads the near-duplicate train/test pairs through every fold, so it is not a fix - it is an amplifier of leakage (AFML Ex. 7.2).
  2. “I split once, so I’m clean” - insufficient. A single chronological split avoids this mechanism only if labels don’t overlap the split boundary. With an -bar label, the bars before the split are contaminated even in a “clean” train/test split.
  3. Redundancy ≠ signal. High adjacent-label agreement is a property of the label construction, not of your feature. Confusing the two is exactly how a noise model “beats” the market in a leaked backtest.

5. References

  • López de Prado, Advances in Financial Machine Learning, Ch. 7 (§7.2–7.3: why -fold fails in finance; the , mechanism)
  • Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, Ch. 7 (§7.10.2: the wrong-vs-right CV example)

6. Connected Graph Bridges