Recommended resources
The freely available resources this lesson will direct you to.
Welcome to the next stage of our journey into advanced feature engineering. In our last lesson, we developed a powerful "microscope" for time series analysis—the wavelet transform—allowing us to dissect a single series into its time-frequency components. We learned to see not just what was happening, but when it was happening. Now, we will zoom out to a "macro" perspective. Instead of looking inside one signal, we will look at the intricate dance between multiple signals.
In finance, as in many complex systems, nothing exists in a vacuum. The price of an airline stock is affected by the price of oil. The value of the Australian dollar is related to that of the New Zealand dollar. The stock prices of Google and Microsoft, while competitors, often move in response to broader tech sector trends. These interconnections are not noise; they are rich sources of predictive information.
This lesson is dedicated to engineering features that capture these cross-asset dynamics. We will learn how to quantify and model correlations, identify stable long-term relationships, and detect subtle lead-lag effects where one asset's movement precedes another's. You will learn to build features that represent not just an asset's own history, but its context within the wider market ecosystem.
Capturing Lead-Lag Relationships with Cross-Correlation
One of the most fundamental relationships between two time series is that of a leader and a follower. Does a change in asset A consistently happen a few days before a similar change in asset B? If so, asset A's recent behavior is a powerful feature for predicting asset B's future. The classic tool for measuring this is cross-correlation.
Cross-correlation systematically calculates the correlation between two series at different time offsets or "lags." It involves sliding one series forward and backward in time relative to the other and computing the correlation at each step. The resulting plot of correlation versus lag reveals the strength of the relationship at different time horizons.
To get a precise definition and see a practical example, the following article provides a concise walkthrough.
Cross-Correlation and Coherence in Time Series Analysis ...
This article clearly defines cross-correlation and demonstrates its application in Python to find lead-lag relationships between stock prices.
Please read the first major section, "Cross-Correlation: Uncovering Time-Based Relationships." Focus on the definition and the mathematical formula. Then, study the stock price example where the author analyzes the relationship between Apple (AAPL) and Microsoft (MSFT). Pay close attention to the interpretation of positive, negative, and zero lags.
As the article demonstrates, the output of a cross-correlation analysis gives you direct, interpretable features:
- The lag with the maximum correlation: This tells you the typical lead/lag time between the two series. For instance, a peak at
lag = +2suggests that series Y's movements at time are most correlated with series X's movements at time , meaning X leads Y by two time steps. This lag value itself can be a feature. - The value of the maximum correlation: The height of this peak quantifies the strength of the lead-lag relationship.
Engineering Spreads and Relative Value Features
While cross-correlation is excellent for finding a time-shifted relationship, another powerful class of features arises from modeling the simultaneous relationship between assets. Often, two related assets have a "normal" price relationship that they tend to maintain. Deviations from this normal relationship can be temporary and predictive of a reversion to the mean. Features that capture these deviations are central to strategies like pairs trading.
The simplest way to model this is by creating a spread feature, which is often just the difference between the two asset prices or, more robustly, their log returns. A model can then learn how the behavior of this spread predicts the future movement of one or both assets.
The following video provides an excellent, code-oriented explanation of how to engineer features based on both an asset's own past (auto-regression) and another asset's past (cross-regression), culminating in the spread feature.
Feature Engineering for Quant Trading
This video, "Feature Engineering for Quant Trading," is a practical guide to creating predictive features from price movements.
First, to ensure we are working with the correct data transformations, watch the segment on price movements. This covers the crucial concepts of simple returns and log returns, which are the stationary building blocks for our cross-asset features. Next, watch the segment on cross-regression. This introduces the core idea of using the price movement of asset Y to predict the future price movement of asset X. Then, see how to combine an asset's own history with another's in the section on distributed lags. Finally, watch the key section on the spread feature. This explains how the difference, or spread, between two assets' log returns can be used as a single powerful feature that models the imbalance between them.
The video demonstrates a clear hierarchy: you can use asset Y's returns to predict asset X (cross-regression), or you can create a more sophisticated feature—the spread—which represents the relative value between them. An increasing spread might signal that asset X is becoming overvalued relative to Y, and a reversion is likely.
A More Robust Foundation: Cointegration
Basing a spread feature on a simple difference of prices or returns is intuitive, but it rests on a shaky assumption: that the relationship between the assets is stable. Stock prices are typically non-stationary (they have trends, and their statistical properties change over time). Simply calculating the correlation or difference between two non-stationary series can lead to spurious, unreliable results.
A much more statistically sound approach is to test for cointegration. Two or more non-stationary time series are said to be cointegrated if a specific linear combination of them is stationary. In essence, this means that even though each series may wander off on its own, they are bound by a long-term equilibrium relationship that they tend to revert to.
The spread created from a cointegrated pair is, by definition, stationary (mean-reverting), making it a far more reliable feature for a predictive model.
This next video explains the concept of cointegration and why it is superior to simple correlation for pairs trading strategies.
Pairs Trading: The Cointegration Approach and Minimum Profit Optimization
This video from Hudson & Thames, "Pairs Trading: The Cointegration Approach," provides the theoretical foundation for building robust spread features.
Watch the introduction to understand why correlation is not enough and why we need a more robust concept for non-stationary price series. Next, focus on the formal definition of cointegration. The key idea is that a linear combination of two non-stationary (I(1)) series creates one stationary (I(0)) series. Then, understand the implications of cointegration. It describes a long-term equilibrium, suggests a shared risk profile, and, crucially, provides a unique "hedge ratio" (the beta coefficient) for constructing the tradable spread. Finally, briefly review the section on finding the cointegration coefficient, which mentions the Engle-Granger and Johansen tests used to estimate this beta.
Interview Alert: You are tasked with building a model to trade the relationship between West Texas Intermediate (WTI) crude oil and United Airlines (UAL) stock. How would you engineer a feature to capture their relative value?
A strong answer would be: "My primary approach would be to test for cointegration between the WTI and UAL price series. Both are likely non-stationary, so a simple correlation of their prices would be misleading. A cointegration test, like the Engle-Granger or Johansen test, would determine if a stable, long-term equilibrium relationship exists between them.
If they are cointegrated, the test will provide a cointegrating vector,
[1, -β]. I would then engineer a 'spread' feature defined asSpread = log(UAL_price) - β * log(WTI_price). This resulting spread series would be stationary by definition.The key advantages of this feature are twofold: First, its stationarity means it has a stable mean and variance, making it a reliable input for a machine learning model. Second, its value represents the deviation from the long-term equilibrium. A large positive or negative value for the spread would be a strong signal that a price correction is likely, providing significant predictive power for short-term price movements in either UAL or WTI."
Advanced Technique: Cross-Asset Embeddings
So far, we've focused on pairwise relationships. But what if we want to capture an asset's relationship to the entire market or a large sector simultaneously? Inspired by techniques from NLP, where a word's meaning is captured by its context (the "company it keeps"), we can create a dense vector representation—an embedding—for each asset based on its similarity to other assets.
The process is as follows:
- Define a set of "base" assets. These could be major market indices (S&P 500, NASDAQ), key commodities (Oil, Gold), or representative stocks from various sectors.
- Create a similarity matrix. For each asset in our universe, we calculate a vector of similarity scores between it and every asset in our base set. The similarity metric can be Pearson correlation, but more advanced, non-linear measures like Dynamic Time Warping (DTW) can capture more complex relationships.
- Create dense embeddings. This similarity matrix will be wide and sparse. We can use a dimensionality reduction technique like Principal Component Analysis (PCA) on this matrix to create a low-dimensional, dense embedding for each asset.
Each asset is now represented by a vector (e.g., of length 10) that encodes its relationship to the broader market. Assets that behave similarly will have embeddings that are close to each other in this new vector space. This embedding can then be fed as a powerful contextual feature into a deep learning model like an LSTM.
The following research paper details exactly such a system. It's a comprehensive resource, so we will focus on the conceptual and implementation sections.
LSTM Feature Engineering Through Time Series Similarity ...
This master's thesis presents a complete system for creating and utilizing cross-asset embeddings to improve time series prediction with LSTMs.
First, read the Introduction (Chapter 1) to grasp the core problem: how to provide a model with contextual information about a time series' origin without making the input too wide. Also, read the Aim subsection. Next, familiarize yourself with the foundational concepts. Skim the section on Similarity Measurements (Section 2.1) to see the different ways to define "similarity," including Pearson Correlation and DTW. Then, read the section on Embeddings and Similarity Matrices (Section 2.2) to understand how a matrix of pairwise similarities is constructed. Now, let's see how it's built. Read the description of the Embedding System in Section 4.5.3. This outlines the full pipeline: calculating similarities, smoothing them over time, and using PCA to create the final, dense embeddings. Finally, to understand the value of this method, read the answer to the research question: "Are embeddings useful tools?" (Section 6.6.2). The conclusion is that they can provide significant accuracy gains, especially in complex domains like finance.
This embedding approach is a perfect example of the "out-of-the-box" thinking you're interested in. It transforms the problem from simple pairwise analysis to a holistic, market-aware feature representation. As a final thought, this idea of representing assets and their relationships can be taken even further by modeling the entire market as a graph, where assets are nodes and their relationships are edges. You could then apply Graph Neural Networks (GNNs) to learn features directly on this structure, as shown in the conceptual diagram below.

Conclusion
In this lesson, we have significantly expanded our feature engineering toolkit for time series. We've moved beyond analyzing single series in isolation and learned to harness the rich information encoded in the relationships between them.
Key Takeaways:
- Lead-Lag Detection: Cross-correlation is a fundamental tool for identifying which assets lead and which follow, allowing you to use one asset's past to predict another's future.
- Relative Value & Spreads: The deviation of two assets from their normal relationship is a powerful predictive signal. While simple spreads are useful, features based on cointegration are more statistically robust as they model long-term equilibrium.
- Scalable Context with Embeddings: For complex, many-asset systems, you can create dense vector embeddings for each asset. These embeddings capture an asset's "identity" within the broader market by encoding its similarity to a set of reference assets.
So far in this module, we have engineered features from individual series dynamics, their volatility, their multi-scale properties, and now, their relationships to each other. In our final lesson for this module, we will drill down to an even finer level of granularity and explore feature engineering for high-frequency trading by looking at order book data.