BLOG

Behind the numbers.

Code examples, market analysis, and data quality deep-dives.

Which Assets Hedge Inflation Shocks? Macro Factor Betas in Python
Does Covariance Shrinkage Beat the Sample Covariance? Minimum-Variance Portfolios in Python
Do Faster Inventory Turns Mean Thinner Margins? Gross Margin Return on Inventory in Python
SEC EDGAR API vs Fundamentals API: Which to Use
Does Fast Earnings Growth Persist? Rank Correlation Analysis in Python
Split Adjustment Explained: Adjusted Close vs Close
Which Trading Day of the Month Pays Best? Turn-of-the-Month Analysis in Python
Can You Use Yahoo Finance Data Commercially?
How Many Independent Bets Does a Nine-Sector Portfolio Give You? Eigenvalue Analysis in Python
Which Volatility Forecast Wins One Month Ahead? HAR vs EWMA in Python
How Concentrated Are Institutional Equity Portfolios? Form 13F Concentration Analysis in Python
Do Stocks Earn Their Returns Overnight or Intraday? Return Decomposition in Python
When Do Corporate Insiders Actually Trade? Form 4 Timing Analysis in Python
Data Requirements for Backtesting a Trading Strategy
What Is Survivorship Bias in Backtesting?
Do High Dividend Yields Come From Bigger Payouts or Falling Prices? Yield Decomposition in Python
Do Stocks Fall Harder Than They Rise? Downside Beta vs Upside Beta in Python
Does Volatility Targeting Improve Sharpe Ratios? Seven-Asset Backtest in Python
Free Stock Market Data APIs: What You Actually Get
How to Give an LLM Financial Data With an MCP Server
Does Post-Earnings Announcement Drift Survive Real Filing Dates? PEAD Event Study in Python
Do Insider Buying Clusters Predict Returns? Signal Testing in Python
Does the S&P 500 Index Effect Still Exist? Event Study in Python
Are Companies Leaving the S&P 500 Faster Than They Used To? Index Survival Analysis in Python
Does Gross Profitability Predict Stock Returns? Quintile Factor Test in Python
What Growth Rate Is the Market Pricing In? Reverse DCF in Python
Does a Strong Balance Sheet Cushion Drawdowns? Leverage and Downside Risk in Python
Does Ticker Recycling Corrupt a Mean-Reversion Backtest? Entity-Resolved Z-Scores in Python
How Much of a Growth Screen's Backtested Edge Is Survivorship Bias? Point-in-Time Index Testing in Python
Why Do Leveraged ETFs Decay? Measuring Volatility Drag in Python
Are Consumer Staples Margins Shrinking Under Inflation? Gross Margin Trend Analysis in Python
Do Weak Jobs Reports Predict Market Drawdowns? NFP Surprise Event Study in Python
Is the Rotation From Tech to Industrials Backed by Earnings? Relative EPS Growth Analysis in Python
Is the Semiconductor Rally Broadening Beyond NVIDIA? Return Dispersion Analysis in Python
Which Stocks Benefit Most When Oil Prices Fall? Oil Beta Screening in Python
Do Bond Returns Predict Stock Returns? Granger Causality Test in Python
Which Stocks Actually Drive Portfolio Returns? Shapley Value Attribution in Python
Does "Sell in May" Still Work? Calendar Anomaly Backtest in Python
How to Build Complete Price History Through Ticker Changes? Entity Resolution in Python
Are KO and PEP Cointegrated? Pairs Trading Signal Construction in Python
Which Commodities Have the Strongest Momentum? Rotation Backtest in Python
Which Commodity ETFs Have the Worst Tail Risk? Expected Shortfall in Python
Are Gold Miners Leveraged Gold Bets? Rolling Beta Analysis in Python
Does the Base-Metals-to-Gold Ratio Lead Cyclical Stocks? Signal Test in Python
Can Risk Parity Tame Commodity Volatility? Portfolio Optimization in Python
Are Power Stocks Becoming an AI Infrastructure Trade? Momentum Screening in Python
Which AI Chip Stocks Have Margin Momentum? Profitability Trend Analysis in Python
Which AI Stocks Are Cheapest Relative to Growth? Growth-Adjusted Valuation in Python
Does AI Stock Leadership Persist? Momentum Backtest in Python
Which AI Stocks Have the Cleanest Balance Sheets? Net Cash Screening in Python
Can Risk Parity Reduce Mega-Cap Drawdowns? Portfolio Optimization in Python
Which Growth Stocks Are Self-Funding? Cash-Flow Quality Screening in Python
Which Sectors Struggle When the Dollar Rallies? Sector Rotation Analysis in Python
Do Cheap Stocks Hold Up When Bonds Sell Off? Valuation Rotation in Python
Does the Nasdaq 100 Have Better Growth Quality Than the Dow? Index Constituent Analysis in Python
Do Healthcare Cash-Flow Margins Predict Returns? Signal Evaluation in Python
Which Dividend Stocks Survive a Cash-Flow Stress Test? Dividend Screening in Python
Does Heavy Insider Selling Predict Weak Returns? Insider Flow Test in Python
Can Quality Screens Reduce Small-Cap Balance-Sheet Risk? Russell 2000 Test in Python
Which Retailers Have Positive Operating Leverage? Margin Screening in Python
Is MSTR a Leveraged Bitcoin Proxy? Rolling Beta Analysis in Python
Is Micron's Memory Cycle Recovering? Inventory and Margin Forecasting in Python
Which Sectors Work When Bonds Rally? Rate-Sensitive Rotation in Python
Do One-Month Price Extremes Reverse? Signal Evaluation in Python
Do Low-Volatility S&P 500 Stocks Reduce Drawdowns? Factor Test in Python
Is AI Capex Paying Back Fast Enough? Revenue Hurdle Forecasting in Python
Could Shorter AI Asset Lives Hit Earnings? Depreciation Stress Test in Python
How Much AI Capex Risk Can a Portfolio Remove? Constrained Optimization in Python
Is the AI Capex Trade Crowded? Rolling Volatility and Sector Rotation in Python
Did the AI Boom Come From Existing S&P 500 Members? Point-in-Time Momentum Test in Python
Is AI Revenue Circular? Customer-Vendor Capex Loop Analysis in Python
Is the AI Trade Connected to Private Credit? Rolling Correlation Network in Python
Is Apollo More Balance-Sheet Sensitive Than Peers? Leverage Screen in Python
Are AI Earnings Supported by Cash Flow? Accrual and Capex Screen in Python
Can Defensive Stocks Hedge AI Drawdowns? Basket Regime Test in Python
How Fast Does the Market Price In Fed Decisions? FOMC Event Study in Python
How Much Are Options Sellers Overpaid? The Variance Risk Premium in Python
Which Companies Have the Worst Earnings Quality? Sloan Accrual Screen with Geographic Revenue Data in Python
Does the Oil-to-Gold Ratio Signal Recessions? XLE/GLD Backtest in Python
Is AI Spending Crowding Out Free Cash Flow? Capex Sustainability Across the Mag 7 in Python
Does a Long Energy / Short Bonds Portfolio Capture Inflation Surprises? Factor Construction in Python
Can a Hidden Markov Model Detect Oil Market Regimes? HMM Analysis in Python
Do Grain Prices Predict Food Inflation? Granger Causality Test in Python
Does the Corporate Credit Spread Predict Stock Market Crashes? BAA-AAA Spread Analysis in Python
Do Oil Stocks Hedge Inflation? Rolling Beta Analysis in Python
Which Stocks Are Most Rate-Sensitive? Equity Duration via Bond Beta in Python
Which Companies Have the Highest Accrual Ratios? Earnings Quality Screening in Python
Is Alpha Persistent or Decaying? Rolling Sharpe Ratio Analysis in Python
Are Markets Trending or Mean-Reverting? Hurst Exponent Analysis in Python
Is Consumer Discretionary vs Staples a Leading Indicator? XLY/XLP Ratio Analysis in Python
Does Heavy Capex Predict Future Stock Returns? Capital Expenditure Analysis in Python
How to Estimate Cost of Equity Using CAPM in Python
Is Volatility Predictable? Testing for Volatility Clustering in Python
Which Industrials Are Overleveraged? Net Debt to EBITDA Screening in Python
GM Before and After Bankruptcy: Why Entity Resolution Matters for Financial Data
What Is Adjusted Beta? Merrill Lynch Beta Shrinkage in Python
How Good Is a Stock Pick? Information Ratio and Tracking Error in Python
Do Stock Returns Follow a Normal Distribution? Testing for Fat Tails in Python
Which Large Caps Have the Highest Free Cash Flow Yield? FCF Screening in Python
Which Sectors Won Over 5 Years? Sector Rotation Analysis in Python
How to Forecast Stock Volatility with GARCH Models in Python
Are Stock Prices Mean-Reverting? Augmented Dickey-Fuller Test in Python
How to Calculate CAPM Alpha and Beta with Regression in Python
How to Compare Sector Sharpe Ratios and Sortino Ratios in Python
DELL: Why Stitching Historical Price Data Together Is Wrong
How to Analyze Drawdown and Recovery for Bank Stocks in Python
How to Screen SaaS Stocks by Revenue Growth and Cash Flow in Python
How to Screen REITs by Dividend Yield and Valuation in Python
How Correlated Are the Magnificent 7? Intra-Group Correlation in Python
AAPL vs XOM: Do Individual Stocks Have Seasonal Patterns?
How to Rank Large-Cap Stocks by Momentum in Python
How to Build a Multi-Endpoint Financial Dashboard in Python
How to Compare Volatility Across Energy Stocks in Python
How to Screen Healthcare Stocks by Valuation in Python
How to Build a Sector Correlation Matrix for Portfolio Diversification in Python
How to Find Oversold and Overbought Stocks Using Z-Scores in Python
How to Measure Earnings Quality: Cash Flow vs Net Income in Python
How to Build a Multi-Factor Stock Screen in Python (Value + Momentum + Quality)
How to Build a Simple DCF Model for Any Stock in Python
How to Screen Tech Stocks by Revenue Growth in Python
How to Screen Stocks by Balance Sheet Health in Python
Is "Sell in May" Real? SPY Monthly Seasonality Over 10 Years
How to Compare Sector Performance YTD Using Python
How to Screen Dividend Stocks by Yield and Quality in Python
How to Calculate Max Drawdown and Recovery Time for Any Stock in Python
How to Compare Profitability Across Mega-Cap Tech Stocks in Python
Why Ticker Symbols Are Unreliable: The Recycling Problem Every Quant Should Know
How to Calculate and Compare Stock Volatility in Python
How to Screen Blue-Chip Stocks by P/E Ratio in Python
How to Track Companies Through Ticker Changes, Bankruptcies, and Renames in Python
S&P 500 Turnover: How Much the Index Has Changed Since 2010
How to Calculate Stock Beta and Correlation in Python
← All articles

Does Covariance Shrinkage Beat the Sample Covariance? Minimum-Variance Portfolios in Python

What’s the question?

A minimum-variance portfolio is the set of weights producing the lowest possible variance from a given set of assets. It needs one input: the covariance matrix of returns, never known and always estimated. Thirty assets means 30 variances plus 435 covariances, so one rebalance rests on 465 estimated numbers the optimiser treats as exact.

That is a problem, because minimum-variance weights depend on the inverse of the matrix. Inversion divides by the smallest eigenvalues, exactly the directions where the sample estimate is least reliable. Two stocks that happened to move together during the estimation window look like a free variance reduction, so the optimiser takes a large long position in one and a large short in the other. When the correlation reverts, that pair stops cancelling and starts contributing risk.

Ledoit and Wolf proposed the standard repair in 2004: mix the sample matrix with a structured target, here a scaled identity matrix that assumes equal variances and zero correlation everywhere. The target is wrong, but it carries no estimation error, and a weighted average of a noisy unbiased estimate and a clean biased one can beat both. The mixing weight, called the shrinkage intensity, comes from the data.

The approach

The universe is the current 30 members of the Dow Jones Industrial Average, pulled from the index endpoint. Every name is live and trades on every day of the sample, giving a complete panel of 1,255 trading days across calendar years 2021 through 2025.

  1. Every 21 trading days, estimate the covariance matrix from a trailing window of daily returns.
  2. Build four portfolios: equal weight, minimum variance on the sample matrix, minimum variance on the shrunk matrix, and minimum variance on the sample matrix with weights held non-negative.
  3. Hold each for the following 21 trading days. No information from the holding period enters the weights.
  4. Repeat with estimation windows of 252 and 60 days, keeping the out-of-sample period fixed at 987 days so the runs are comparable.

Estimating 465 parameters from 252 observations is already thin; at 60 observations there are two data points per asset. If shrinkage is worth anything, the short window is where it should show.

Code

import numpy as np
import pandas as pd
from scipy.optimize import minimize
import xfinlink as xfl

xfl.set_api_key("YOUR_API_KEY")  # free at https://xfinlink.com/signup

tickers = sorted(xfl.index("djia")["ticker"].tolist())
px = xfl.prices(tickers, start="2021-01-01", end="2025-12-31", fields=["return_daily"])
R = px.pivot(index="date", columns="ticker", values="return_daily").sort_index().dropna()
X, N = R.to_numpy(), R.shape[1]


def ledoit_wolf(Z):
    """Shrink the sample covariance toward a scaled identity matrix."""
    T, n = Z.shape
    Zc = Z - Z.mean(axis=0)
    S = Zc.T @ Zc / T
    mu = np.trace(S) / n
    d2 = np.sum((S - mu * np.eye(n)) ** 2) / n
    b2 = (np.sum(np.einsum("ij,ij->i", Zc, Zc) ** 2) - T * np.sum(S ** 2)) / (n * T ** 2)
    a = float(np.clip(b2 / d2, 0.0, 1.0))
    return a * mu * np.eye(n) + (1 - a) * S, a


def min_var(cov):
    w = np.linalg.solve(cov, np.ones(len(cov)))
    return w / w.sum()


def min_var_long_only(cov):
    n = len(cov)
    return minimize(lambda w: w @ cov @ w, np.repeat(1 / n, n),
                    jac=lambda w: 2 * cov @ w, method="SLSQP",
                    bounds=[(0.0, 1.0)] * n,
                    constraints=[{"type": "eq", "fun": lambda w: w.sum() - 1.0}]).x


for window in (252, 60):
    oos = {k: [] for k in ("equal", "sample", "shrunk", "long_only")}
    for t in range(252, len(X) - 20, 21):
        train, test = X[t - window:t], X[t:t + 21]
        S = np.cov(train, rowvar=False)
        LW, intensity = ledoit_wolf(train)
        oos["equal"].append(test @ np.repeat(1 / N, N))
        oos["sample"].append(test @ min_var(S))
        oos["shrunk"].append(test @ min_var(LW))
        oos["long_only"].append(test @ min_var_long_only(S))

    for k, v in oos.items():
        r = np.concatenate(v)
        print(window, k, "vol %.2f%%" % (r.std(ddof=1) * np.sqrt(252) * 100))

Full script with formatting and visualisation: covariance-shrinkage-minimum-variance-portfolio-python.py

Output

Out-of-sample annualised volatility of four Dow 30 portfolios at 252-day and 60-day estimation windows, and gross exposure of the sample-covariance and shrunk-covariance minimum-variance portfolios over time.
=== SANITY ===
tickers: 30 | trading days: 1255
range: 2021-01-04 -> 2025-12-31 | monotonic: True
NaNs in matrix: 0
min daily return: -0.2238  max daily return: 0.2437
obs with |return| > 20%: 3
    2024-07-26 MMM 0.2299
    2023-05-25 NVDA 0.2437
    2025-04-17 UNH -0.2238

=== 252-DAY ESTIMATION WINDOW: 47 rebalances, 987 out-of-sample days, 30 assets ===
Portfolio                   Vol   Return   Sharpe    Gross   MaxWgt  Shorts
Equal weight             15.58%   12.56%     0.81     1.00     3.3%     0.0
Min-var, sample cov      13.06%    4.73%     0.36     1.75    21.3%     9.9
Min-var, shrunk cov      12.75%    5.24%     0.41     1.55    17.3%     8.8
Min-var, long-only       12.36%    6.31%     0.51     1.00    19.8%     0.0
Mean shrinkage intensity: 0.078   Vol change, shrunk vs sample: -2.4%

=== 60-DAY ESTIMATION WINDOW: 47 rebalances, 987 out-of-sample days, 30 assets ===
Portfolio                   Vol   Return   Sharpe    Gross   MaxWgt  Shorts
Equal weight             15.58%   12.56%     0.81     1.00     3.3%     0.0
Min-var, sample cov      17.01%    7.20%     0.42     3.11    36.2%    11.8
Min-var, shrunk cov      12.87%    5.94%     0.46     1.59    15.6%     7.8
Min-var, long-only       12.75%    7.51%     0.59     1.00    26.2%     0.0
Mean shrinkage intensity: 0.251   Vol change, shrunk vs sample: -24.3%

=== CHECKS ===
window 252 | max |sum(w)-1| = 4.4e-16 | min long-only weight = 0.0e+00 | shrinkage 0.024-0.138 | max cond(S) = 215 | NaNs = 0
window  60 | max |sum(w)-1| = 4.4e-16 | min long-only weight = 0.0e+00 | shrinkage 0.068-0.518 | max cond(S) = 1377 | NaNs = 0

What this tells us

The value of shrinkage depends almost entirely on how much data the covariance estimate is built from.

On the 252-day window the sample matrix is already good enough. Minimum variance on it delivered 13.06% realised volatility against 15.58% for equal weight, and shrinkage improved that to 12.75%. Average intensity was 0.078, so the estimator gave the sample matrix 92% of the weight; with a condition number peaking at 215, that judgement is right, and shrinkage earns about 30 basis points.

The 60-day window inverts the result. Minimum variance on the sample matrix produced 17.01% realised volatility, worse than naive equal weighting: a procedure whose purpose is to minimise variance ended up increasing it. The lower panel shows why. Gross exposure averaged 3.11 with peaks above 5.5, the average largest position was 36.2%, and the optimiser financed its phantom hedges with 11.8 short positions, working from a matrix whose condition number reached 1,377. Shrinkage cut volatility to 12.87%, a 24.3% reduction, and pulled gross exposure to 1.59 as average intensity rose to 0.251.

The long-only portfolio is the quiet winner. It never touched the shrinkage code, yet delivered 12.36% and 12.75% volatility at the two windows. Jagannathan and Ma showed in 2003 that a non-negativity constraint is mathematically equivalent to shrinking the covariance matrix, since each binding constraint acts like a reduction in the estimated covariances of the asset it excludes. Forbidding shorts stabilised the portfolio as effectively as the explicit estimator.

One number cuts against all three optimised portfolios: equal weight produced the highest Sharpe ratio in every run, 0.81 against a range of 0.36 to 0.59, because it held the high-volatility, high-return names minimum variance avoids by construction.

So what?

Check the ratio of observations to assets before choosing an estimator. At roughly eight observations per asset the sample covariance matrix works and shrinkage is a small refinement worth ten lines of code. At two observations per asset it is actively harmful, and the resulting portfolio can carry more risk than one built on no estimate at all.

Anyone wanting minimum-variance exposure without maintaining an estimator has a simpler route: impose a long-only constraint. It delivered the lowest realised volatility of any portfolio tested here, at both window lengths, with gross exposure fixed at 1.00. Gross exposure of 3.11 is not a modelling artefact; it is a borrow and financing bill that no volatility number above includes.

The lesson generalises past minimum variance. Any optimiser inverting an estimated matrix, mean-variance and risk parity included, inherits the same amplification of estimation error. Constrain the output, shrink the input, or estimate fewer parameters; doing none of the three produces a portfolio optimal only in the sample it was fitted to.

Built with xfinlink — free financial data API for Python. pip install -U xfinlink
← All articles