Recommended resources
The freely available resources this lesson will direct you to.
Welcome to your first lesson in our course on advanced feature engineering. We're embarking on a journey that will take us from the foundational principles of creating powerful features to the MLOps architecture required to manage them in production. Our goal is to equip you with the deep, and sometimes unconventional, techniques used to build state-of-the-art machine learning systems, with a special focus on the complexities of time-dependent data.
In this inaugural lesson, we'll tackle a topic that is not strictly feature engineering, but rather its essential prerequisite: robust model validation for time series. Before we can confidently create and select features, we must have a reliable way to measure their impact. As you know from your ML experience, cross-validation is the gold standard for assessing a model's generalization ability. However, naively applying standard techniques like k-fold cross-validation to time-series data is a catastrophic error that can lead to wildly optimistic performance estimates and models that fail spectacularly in the real world.
Today, you will learn how to implement robust time-series cross-validation strategies, such as walk-forward and blocked k-fold, to prevent this critical issue of temporal data leakage. We'll explore not just the "how" but the "why," grounding our discussion in the realities of financial markets and other time-dependent systems.
The Time-Travel Paradox: Why Standard Cross-Validation Fails
In most machine learning contexts, we assume that our data points are independent and identically distributed (I.I.D.). This assumption allows us to randomly shuffle and partition the data into training and testing folds, as is done in standard k-fold cross-validation.
Time-series data violates this assumption. Observations are not independent; the value of a stock today is related to its value yesterday. The underlying data distribution is often not identical over time; market conditions change, new trends emerge, and seasonal patterns repeat.
Applying random k-fold CV to this type of data creates an illusion of time travel. The model, during training, can be exposed to data points from the future relative to the data points it is trying to predict in the test set. This "peek into the future" is known as data leakage or lookahead bias, and it results in an overly optimistic evaluation of your model's performance.
To get a visceral sense of this problem and its real-world consequences, let's start with a short, narrative-driven article.
Cross-Validation vs Walk-Forward: The Time Series Trap That ...
This article tells the story of a costly mistake made by applying the wrong validation technique. It perfectly illustrates the core problem we are addressing.
Read from the beginning through Chapter 1 to understand the setup. Then, focus on Chapter 2, specifically the section The Time Travel Paradox Revealed. This demonstrates exactly how standard CV leads to leakage. After that, jump to Chapter 4 (The Mathematical Autopsy) for a concise explanation of the three core reasons for this failure: information leakage, temporal dependence violation, and the breakdown of the stationarity assumption. Finally, read the author's personal story in Chapter 5 (My Own £500K Lesson) to see the tangible business impact.
The First Line of Defense: Walk-Forward Validation
The most fundamental way to respect the arrow of time is to only use past data to train a model that predicts the future. This is the principle behind walk-forward validation (also called rolling forecast). Instead of random folds, we create a sequence of splits that mimic how a model would be retrained and used in a live environment.
There are two main flavors of walk-forward validation:
- Expanding Window (or Rolling Forecast Origin): In each split, the training set grows to include all historical data up to that point. This is useful when the entire history is relevant.
- Sliding Window: In each split, the training set maintains a fixed size. As the window slides forward in time, the oldest data is dropped as new data is added. This is often preferred in non-stationary environments where older data may be less relevant or even harmful.
The following video provides a very clear, code-driven explanation of both methods.
Bonus Lecture. Time Series Cross Validation
This lecture from HSMA provides a concise, practical implementation of walk-forward validation using Python generators.
Watch from the beginning to understand the motivation for time-series CV (the introduction). Then, focus on the two key implementation sections: Rolling Forecast Origin: This demonstrates the expanding window approach. Sliding Window: This shows the fixed-size window implementation. Pay attention to how the yield keyword is used to create a generator, which is an efficient way to produce these train/test splits, especially for large datasets. This is a practical pattern you can use in your own code.
A Surgical Approach: Purging and Embargoing
While walk-forward validation prevents the model from being trained on data from the future, a more subtle form of leakage can still occur. This is particularly prevalent in finance, where labels are often derived from future information (e.g., a label indicating whether the price went up or down over the next 5 days).
If the training set contains observations that are very close in time to the test set, the labels in the training set might be calculated using price information that overlaps with the test period. To combat this, Marcos Lopez de Prado introduced two powerful techniques: purging and embargoing.
-
Purging: This involves removing observations from the end of the training set. Specifically, we purge any training observations whose labels are derived from information that overlaps with the start of the test set. This creates a clean "air gap" between training and testing.
-
Embargoing: This involves removing observations from the beginning of the next training set that immediately follow the end of the current test set. Because financial data exhibits autocorrelation (market "memory"), the dynamics during the test period can bleed into the period immediately following it. An embargo acts as a "cooldown period," ensuring that the next training fold starts in a more statistically independent regime.
This image provides a clear visual of how these techniques are applied in a single cross-validation split.

The following video explains these concepts in the context of de Prado's work.
Cross-Validation in Finance and Backtesting
This video from Hudson & Thames, a channel focused on quantitative finance, provides a great summary of de Prado's advanced validation methods.
Watch the section explaining purged k-fold cross-validation. This will walk you through the logic of both purging and embargoing, reinforcing the visual from the image above.
These techniques are most often applied in a Blocked K-Fold strategy. Instead of a single walk-forward path, the time series is split into sequential blocks. We then iterate through the blocks, using one block for testing and the others for training. Crucially, we apply purging and embargoing around the test block to prevent leakage. This gives us different performance estimates, providing a more robust measure than a single walk-forward test, while still respecting temporal order.
Interview Alert: A common question in quant and ML interviews is: "You have a time series of stock prices. How would you design a cross-validation scheme to tune a predictive model? Why is
sklearn.KFolda bad choice?"Your answer should start by explaining the failure of the I.I.D. assumption and the risk of lookahead bias. You would then propose a walk-forward or blocked k-fold approach, and for bonus points, you would meticulously explain the need for purging and embargoing to handle leakage from forward-looking labels and autocorrelation.
The Ultimate Test: Combinatorial Purged Cross-Validation (CPCV)
Walk-forward and blocked k-fold are significant improvements, but they are still path-dependent. The specific sequence of events in your single historical path might lead you to select parameters that are overfit to that particular history (e.g., a long bull market).
To achieve true robustness, especially in parameter tuning, we need to test our strategy against many possible historical paths. This is the idea behind Combinatorial Purged Cross-Validation (CPCV).
Instead of a few sequential splits, CPCV generates a large number of random but chronologically valid train/test splits. Each split is purged and embargoed. This process creates a distribution of performance outcomes for each parameter set, allowing us to analyze not just the average performance but also its stability and worst-case scenarios.
This approach shifts the goal from finding the "optimal" parameter to finding the most robust parameter—one that performs well across a wide variety of market regimes. This aligns perfectly with your interest in complex, out-of-the-box solutions from the world of finance.
The following resource provides a deep dive into CPCV, complete with code.
[WITH CODE] Combinatorial Purged Cross Validation for ...
This article from a quantitative finance blog is an excellent, advanced resource that details the theory and implementation of CPCV. It will be challenging but rewarding.
Start by reading the section The failure of standard CV in finance. This reinforces the problem and provides a detailed rationale for purging and embargoing in the context of parameter optimization. Next, focus on the section Probabilistic framework, which explains the "combinatorial" aspect of CPCV. Finally, review the Python code provided in the article for the CombinatorialPurgedSplit class. Don't worry about understanding every line of the associated backtesting logic for now. Focus on the split method within the class (the splitter class). See how it generates multiple random, non-overlapping but chronologically ordered train and test indices, incorporating a purge gap. This is the core engine of CPCV.
Conclusion
In this lesson, we have established the critical importance of robust validation in time-series analysis. Applying standard cross-validation is a recipe for disaster due to data leakage. We've built up a hierarchy of increasingly sophisticated and robust techniques to address this.
Key Takeaways:
- Standard K-Fold is Invalid: It violates the temporal nature of the data, leading to lookahead bias and over-optimistic results.
- Walk-Forward is the Minimum Standard: Methods like expanding and sliding windows respect the arrow of time by always training on the past to predict the future.
- Purging and Embargoing are Essential for Financial Data: These techniques provide a surgical solution to subtle data leakage caused by forward-looking labels and serial correlation.
- Blocked K-Fold Offers More Robustness: By creating multiple sequential test sets (with purging/embargoing), it gives a better estimate of generalization error than a single walk-forward path.
- Combinatorial Purged CV is State-of-the-Art for Robustness: By testing against many random historical paths, CPCV helps identify truly robust models and parameters, not just those that were optimal on one specific historical sequence.
These validation strategies are the bedrock upon which all subsequent time-series feature engineering and modeling must be built. Without a reliable way to measure performance, any "improvements" we think we're making with new features are meaningless.
In our next lesson, we will dive into the core of temporal feature engineering. Now that you know how to validate your models correctly, we'll start creating the features themselves, beginning with techniques like lag, rolling, and expanding window calculations. We'll see how the validation methods learned today are crucial for selecting the optimal parameters for these features, such as the right window size.