strateva.

Articles

Purging and embargo: when your labels overlap the test

In this article

Your model predicts the return of the next 20 days. You train it on all the past and test it on the future. It looks clean. There is a leak almost nobody sees at first.

In the article on walk-forward we mentioned it in passing: a gap is needed between training and testing. Here we explain it in detail, because in machine-learning models and in strategies that hold a position for several days it is one of the errors that inflates a result the most.

The problem, in one sentence

In a model of this kind, each row of data has two parts: what you knew that day (the features) and what happened afterwards (the label, the answer the model learns to predict).

If the label is "return of the next 20 days", yesterday's label will not be known for another 19 days. If you train today on yesterday's row, you are using a figure that does not exist yet.

And there is something worse: that label shares 19 of its 20 days with the one you are trying to predict today. It is not just information from the future. It is almost the same answer.

Purging and embargo

  • Purging: removing from training the rows whose label overlaps in time with the test period. If you predict 20 days ahead, the last 20 rows before the test are dropped.
  • Embargo: also leaving a margin of unused rows after the test period, before training resumes on later data. It is needed when the validation trains on data from after the test, as cross-validation does. It stops the dependence between nearby days from leaking information in from the other side.

In a strict walk-forward, where training only uses the past, the purge is what matters. The embargo comes into play when the design mixes earlier and later stretches.

How much it inflates the result: a real example

We ran a test on the S&P 500 since 1871. A simple model predicts the return of the next H months from three past returns. It is retrained every month on earlier data only, in two ways:

  • No purge: it trains on every earlier month, including the most recent ones, whose label had not finished yet.
  • With a purge: it trains only on the months whose label was already known that day.

We measured the correlation between what the model predicted and what happened. Zero means it predicts nothing.

Without a purge the predictive power does not fade; with one, it vanishesA simple model predicts the S&P 500 return over 1, 2, 3, 6, 12 and 24 months, retrained every month on the past only. Without a purge it also trains on months whose label had not finished yet. At 12 months, the correlation between prediction and outcome is 0.15 without a purge and −0.01 with one. At 1 month there is no overlap and the two coincide.
0.000.050.100.150.2012361224

Label horizon, in months

  • No purge
  • Purge the size of the label

Source: Robert Shiller’s monthly S&P 500 series, with dividends. No risk-free rate deducted; time out of the market earns zero.

Label horizonNo purgeWith a purge
1 month0.160.16
3 months0.120.06
6 months0.150.05
12 months0.15−0.01
24 months0.130.00

Three readings:

  1. At one month there is nothing to purge. Each month's label ends before the next prediction, so the two ways coincide.
  2. Without a purge, predictive power seems not to depend on the horizon. It stays between 0.11 and 0.16 at any range. That is a warning sign: predicting two years ahead should not be as easy as predicting one month ahead.
  3. With a purge, it vanishes. At 12 and 24 months the correlation is zero. Everything the model "knew" at those ranges came from labels that shared almost their whole period with the answer.

Without the purge, this model would have been presented as able to anticipate the market one and two years ahead.

How our RF100 case does it

In RF100 the model predicts each stock's return over the following 42 sessions, which is how long each position lasts. Its design sets a 43-session purge between the last training label and the first test observation, and the record of each of the 17 annual folds stores that separation so it can be checked.

That separation is among the choices fixed in its pre-registration. It is one of the reasons the predictive ability the model showed is credible, even though it was not enough to beat momentum.

Where it shows up without you noticing

  • Strategies that hold a position for several days. If the label is the outcome of a trade lasting two weeks, it overlaps with the trades opened on the following days.
  • Labels with a variable exit. If the trade closes on hitting a target or a stop, the horizon of each label is different. The purge has to cover the longest case.
  • Shuffled cross-validation. Splitting rows at random between training and test leaves each test row surrounded by near-identical neighbours in training. With overlapping labels it is the fastest way to an inflated result.
  • Features that look far back. A 200-session average changes very little from one day to the next, so neighbouring rows are almost identical in their features too.
  • Daily retraining. The more often you retrain, the more times the same slip is repeated at the edge.

Warning signs

  • Predictive power does not fall as the horizon lengthens.
  • The result improves when you retrain more often, for no clear reason.
  • The model does very well in validation and almost nothing on a genuinely later period.
  • The code shows no separation between the end of training and the start of the test.

Honest limits

  • A purge removes a leak; it does not create an edge. A properly purged model can still predict nothing, like the one in this example at 12 months.
  • It costs data. With long horizons you lose an appreciable part of the most recent training data, which is usually the most useful.
  • It does not fix other leaks. Features computed with future data, universes chosen in hindsight or parameters tuned on the test are still there. We explain them in the article on look-ahead bias.
  • The example has a trap of its own. The series uses the monthly average of daily closes, which smooths prices. Part of the 0.16 at one month comes from that and not from real predictive ability.

What to ask if someone shows you a predictive model

  • How many days ahead does it predict?
  • How many rows are left out between training and test? Is it at least that horizon?
  • Does the validation shuffle the rows or respect time order?
  • Is it recorded, fold by fold, where training ends and where the test begins?

Where it fits in a validation

When a strategy predicts more than one period ahead, our validation checks that the separation between training and test covers the label horizon and that it is recorded in each fold. If it cannot be checked, the report states it as a limitation instead of accepting the result.

These tests are part of the quantitative validation. At Strateva we validate strategies in stocks, ETFs, spot crypto and spot FX, always unleveraged and with no margin. We don't take engagements on CFDs, futures, perpetuals or options.

To ask whether yours fits, write to stan@strateva.ai with the market (stocks, ETFs, crypto or FX, spot), the frequency, how many days ahead your model predicts and how you separate training and test. Don't send private code or passwords in the first email.


Educational and research content. Not investment advice. The example is our own illustrative calculation on Robert Shiller's monthly S&P 500 series, with dividends: a 10-nearest-neighbour model on the returns of the last 1, 12 and 24 months, retrained every month after an initial 240 months. The figure is the correlation between prediction and realised return. The RF100 data comes from its public repository, Filip303/RF100.

Related cases

RF100 — Can a Random Forest beat simple momentum?

All articles

CONTACT

Tell me about your strategy. I will say whether it fits within 48 business hours.

Tell me what you want checked, the asset, the frequency and what you have: rules, a report or code. I will tell you whether it fits and which plan it would be.

What to include in your email

  • Market, instruments and frequency (Spot only).
  • What you want checked, and the period.
  • Language and approximate code size, without pasting the code.
  • What data you have, and whether you need an NDA.

Do not send private code, keys or passwords.

stan@strateva.ai

Reply within 48 h (business days). No private material in the first message. No obligation to commission work.

Who replies

I am Filip Stanciu. I have an MSc in Quantitative Finance from the University of Alcalá, a degree in Finance from the Complutense University of Madrid, and a MiFID II certificate. I have worked in quantitative research for an asset manager, validating stock and ETF strategies. I read and reply to every email myself.

I am not a certification body and I am not a large team. I am one person, and I say so.

Nicolae Filip Stanciu