BLOG

Behind the numbers.

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

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

How Many Independent Bets Does a Nine-Sector Portfolio Give You? Eigenvalue Analysis in Python

What’s the question?

Nine sector funds look like nine positions. They rarely behave like nine. When every sector moves together, the account holds one position bought nine times, and a risk report listing nine line items is describing bookkeeping rather than risk.

The quantity that matters is the effective number of independent bets. Extract the eigenvalues of the correlation matrix of the nine daily return series. Each one measures how much variance sits along an uncorrelated direction, and the nine always sum to nine. Perfect independence puts one unit in each direction; perfect co-movement piles all nine into the first. Collapsing that spread into a single figure uses the entropy of the normalised eigenvalues, exponentiated:

N = exp(-sum(p_i * ln p_i)),   p_i = lambda_i / 9

N lands on the same scale as the position count, so it reads without translation. Nine means nine genuine bets. Two means two. Two questions follow: how far below nine does a standard sector allocation sit, and what does the shortfall cost in volatility?

The approach

The universe is the nine original Select Sector SPDR funds, which partition the S&P 500 and have traded since December 1998. Real estate and communication services, added later, are left out so that one cross-section runs across the whole 27 years.

  1. Pull daily adj_close from December 1998 to 24 July 2026. It is split-adjusted and gap-free, which matters: five of these funds split two-for-one on 5 December 2025, and a raw quoted price would enter the sample as a 50% single-day loss.
  2. Convert to daily returns, keeping the dates every fund trades so all nine correlations rest on the same observations.
  3. On each trailing 252-day window, build the 9x9 correlation matrix, take its eigenvalues, and convert them to N.
  4. Measure the realised benefit over the same window: volatility of the equal-weight blend against the average volatility of its nine parts.
  5. Read the series at five stress episodes and at the most recent window.

Two checks guard the arithmetic. Eigenvalues of a correlation matrix must sum to the number of assets, and every one of the 6,680 windows returns 9.000000. An entropy measure is also only worth computing if it tracks something an investor can spend; N moves with the realised volatility reduction at 0.988.

Code

import numpy as np
import pandas as pd
import xfinlink as xfl

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

SECTORS = ["XLB", "XLE", "XLF", "XLI", "XLK", "XLP", "XLU", "XLV", "XLY"]
WINDOW = 252

frames = [xfl.prices(t, start="1998-12-01", end="2026-07-24",
                     fields=["adj_close"], max_rows=20000) for t in SECTORS]
px = pd.concat(frames).pivot(index="date", columns="ticker", values="adj_close").dropna()
rets = px.pct_change().dropna()

n = rets.shape[1]
R = rets.values
rows = []
for i in range(WINDOW - 1, len(rets)):
    corr = np.corrcoef(R[i - WINDOW + 1:i + 1], rowvar=False)
    eig = np.sort(np.linalg.eigvalsh(corr))[::-1]
    p = np.clip(eig / n, 1e-12, None)
    rows.append((rets.index[i],
                 float(np.exp(-(p * np.log(p)).sum())),
                 eig[0] / n,
                 corr[np.triu_indices(n, 1)].mean()))

res = pd.DataFrame(rows, columns=["date", "n_eff", "pc1", "avg_corr"]).set_index("date")

sector_vol = rets.rolling(WINDOW).std().mean(axis=1)
port_vol = rets.mean(axis=1).rolling(WINDOW).std()
res["vol_cut"] = (1 - port_vol / sector_vol).reindex(res.index)

print(res.loc[["2012-06-29", "2020-03-31", res.index[-1]]])
print("N vs realised volatility reduction:", round(res["n_eff"].corr(res["vol_cut"]), 3))

Full script with formatting and visualisation: sector-diversification-effective-bets-pca-python.py

Output

Effective number of independent bets in a nine-sector equity portfolio, 1999 to 2026, falling to 1.80 in mid-2012 and reaching 6.33 in July 2026, with the corresponding percentage of volatility removed by holding all nine sectors below it.
Nine Select Sector SPDR funds, daily price returns from split-adjusted closes
Sample      : 1998-12-23 to 2026-07-24 (6,931 trading days, 6,680 rolling windows)
Window      : 252 trading days
Check       : eigenvalues sum to 9.000000 - 9.000000 (must equal 9)

EFFECTIVE NUMBER OF INDEPENDENT BETS   (maximum possible = 9)
                            window end      N   PC1 share  avg corr  vol cut  port vol
Dot-com unwind              2002-09-30   3.58       67.2%      0.62    18.1%     22.7%
Global financial crisis     2008-11-28   2.75       75.6%      0.72    13.2%     36.1%
Euro sovereign crisis       2012-06-29   1.80       87.8%      0.86     5.8%     23.1%
COVID crash                 2020-03-31   1.90       85.7%      0.84     7.7%     30.6%
Inflation shock             2022-09-30   3.47       65.7%      0.59    20.9%     18.8%
Latest window               2026-07-24   6.33       36.6%      0.23    44.6%      9.6%

WHAT A GIVEN N IS WORTH
     N range   windows   avg corr   volatility removed
   below 2.5       887       0.82                 8.2%
   2.5 - 3.0     1,046       0.71                13.5%
   3.0 - 3.5     1,372       0.64                17.5%
   3.5 - 4.0     1,550       0.57                21.8%
   4.0 - 4.5       649       0.51                25.5%
   above 4.5     1,176       0.38                33.2%

Window sensitivity: April 2025 on its own carries an average pairwise correlation of 0.85 across 21 days.
  252 days to 2026-03-31            N = 4.14
  same window minus April 2025      N = 5.73

Full-sample low  : 1.80 on 2012-06-29   high: 6.42 on 2001-03-19   median: 3.53
Latest reading   : 6.33, above 99.6% of all windows since 1999
N tracks the realised volatility reduction with a correlation of 0.988

What this tells us

Nine sectors have never once delivered nine bets. The median window returns 3.53, so a fully spread sector sleeve behaves like three and a half independent positions on an ordinary day.

The collapse is worst where it hurts. At the euro sovereign crisis trough in June 2012, N reached 1.80, the first principal component absorbed 87.8% of variance, and average pairwise correlation stood at 0.86. Holding all nine sectors removed 5.8% of the volatility of holding one average sector: nine positions, a 6% risk reduction. The COVID window ending March 2020 is close behind at 1.90, and 2008 is milder than either at 2.75. The deepest crash was not the worst diversification failure. The 2011 to 2012 risk-on, risk-off regime was, which lines up with the record highs in option-implied index correlation printed over the same stretch.

The bucket table puts a price on it. Below N = 2.5, an equal-weight blend strips out 8.2% of average single-sector volatility; above N = 4.5, it strips out 33.2%. Same nine holdings, four times the benefit.

The latest window is an outlier: N reads 6.33, average pairwise correlation sits at 0.23, and the blend removes 44.6% of single-sector volatility. Only 24 earlier days out of 6,680 scored higher, all in the dispersion regime of 2000 and 2001.

A rolling estimate is hostage to what sits inside the window. April 2025 carried an average pairwise correlation of 0.85 across 21 trading days and dominated every window containing it: the 252 days to 31 March 2026 read 4.14 with that month in and 5.73 without it. Most of the jump through April 2026 is one shock month ageing out.

So what?

Size risk against N rather than against the position count. A nine-sector sleeve run to a volatility target should carry a smaller notional when N sits near 2 than when it sits near 5, because the identical holdings deliver an 8% volatility reduction in the first case and 33% in the second. A rebalancing rule built on a fixed assumed correlation levers up precisely when correlation spikes.

Treat N as a state variable, not a forecast. Computed from trailing data, it says nothing about direction. It answers the question a position report cannot: whether the diversification a mandate is credited with currently exists.

Right now it does. Sector dispersion has not been this wide since 2001. That is a regime rather than a constant, and the 1.80 of June 2012 is what these same nine funds look like when the regime ends.

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