[WITH CODE] Portfolio: Robust covariance estimation
How to keep your portfolio balanced even when markets misbehave
Table of contents:
Introduction.
Classical approach.
Why the classical approach is called “classical”?
Understanding the Mahalanobis Distance.
Improving computational methods.
An iterative reweighting scheme.
Further than traditional enhancements.
Robust covariance estimation via iterative reweighting.
Shrinkage for additional stability.
Introduction
It’s saturday night, you’re at a lively party where everyone has a story to share. Most guests follow a predictable rhythm, but then there’s that one person—loud, unpredictable, and totally off-beat.
In statistics, such a guest is known as an outlier, and its presence can throw off your entire analysis. In traditional quantitative finance and risk management, the covariance matrix plays a starring role. It tells us about the relationships between different assets or variables, much like understanding who at the party pairs well with whom. However, the classical estimation methods, while elegant under ideal conditions, quickly lose their charm when faced with noisy, messy data.
Let’s take a closer look and check if we can enhance this method a little bit.
Classical approach
The concept of the covariance matrix is foundational in portfolio optimization. When dealing with multivariate data—observations that have multiple components or dimensions—understanding how different variables co-vary is critical. This is precisely what the covariance matrix encapsulates: it describes not only how each individual variable spreads out—its variance—but also how each pair of variables co-varies—their covariance.
In a typical scenario, you might have a random vector x∈Rp. This vector has p components, for example, p different stock returns, or p measurements taken on a patient in a clinical trial. The population covariance matrix of x, denoted by Σ, is defined as:
where μ=E[x] is the population mean vector. Of course, in real-world applications, the true mean μ and the true covariance Σ are almost never known. Instead, we collect a sample of observations and use them to estimate these quantities. This is where the classical approach to covariance estimation comes into play.
Suppose we have n observations of the random vector x. We label these observations as x1,x2,…,xn, where each xi∈Rp. The classical sample covariance matrix S is defined by:
with
being the sample mean vector. This definition might look compact, but it encodes several important properties:
Centering by the sample mean: We first subtract the sample mean from each observation. This step ensures that the estimated covariance matrix reflects the variability around the center of the data rather than around an arbitrary origin.
Outer product structure: Each term is a p×p matrix capturing the pairwise product of deviations from the mean for all dimensions. Summing these outer products accumulates information about how variables vary and co-vary.
Division by n−1: Dividing by n−1 (rather than n) is crucial for making S an unbiased estimator of the population covariance Σ. This subtle difference—using n−1 instead of n—comes from Bessel’s correction, a concept that ensures that E[S]=Σ.
Let’s generate some synthetic bivariate data and compute the classical covariance matrix:
import numpy as np
# Generate synthetic data: 100 observations in 2 dimensions
np.random.seed(42)
data = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 1]], size=100)
# Compute the classical covariance matrix
classical_cov = np.cov(data, rowvar=False)
print("Classical Covariance Matrix:")
print(classical_cov)
The classical covariance estimator works beautifully when the data behaves nicely. But financial data is anything but nice data.