BLOG

Behind the numbers.

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

How Many Stocks Does It Take to Diversify? Random Portfolio Simulation in Python
How Many Days of Data Does a Volatility Estimate Need? Range-Based Estimators in Python
How Much of S&P 500 Cash Flow Is Stock Compensation? Cross-Sectional Analysis in Python
What Is a 13F Filing? Institutional Holdings Explained
Does Revenue Growth Explain Profit Growth? Cross-Sectional Decomposition in Python
How Much of the Nasdaq 100 Is Already in the S&P 500? Index Overlap Analysis in Python
How Often Does a 99% Value-at-Risk Limit Actually Break? VaR Backtesting in Python
Real-Time vs End-of-Day Market Data: Which Do You Need?
How Concentrated Are S&P 500 Earnings? Point-in-Time Index Analysis in Python
Does Volatility Scale With the Square Root of Time? Variance Ratio Test in Python
Does Goodwill Distort the Price-to-Book Screen? Goodwill-Adjusted Valuation in Python
How Are Shares Outstanding Reported (and Why They Disagree)
Do Low-Volatility Stocks Deliver Better Risk-Adjusted Returns? S&P 500 Quintile Sorts in Python
Does Trend Following Beat Buy and Hold? Time-Series Momentum in Python
Has the Stock-Bond Correlation Flipped? 60/40 Portfolio Risk in Python
What API to Use for a Stock Screener
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

How Many Stocks Does It Take to Diversify? Random Portfolio Simulation in Python

What’s the question?

A concentrated equity manager holds 25 to 30 names and cites the standard result: past roughly 30 stocks, the volatility saved by adding another is too small to matter. Evans and Archer put the number near 10 in 1968, and Statman revised it upward in 1987, to at least 30 for a borrowing investor and 40 for a lending one.

Every version of that result describes an average. Draw many 30-stock portfolios at random, measure the volatility of each, and the mean sits close to the volatility of the market itself.

Nobody holds the average portfolio. An investor holds one draw from a distribution, and the width of that distribution is itself a risk. Both the typical risk of an N-name portfolio and the spread of risk across the portfolios that could have been picked instead shrink as N grows, at different speeds, and the second is the one a risk budget has to survive.

The approach

The exercise draws random equal-weighted portfolios from S&P 500 members and measures two properties of each: annualised volatility, and maximum drawdown, the deepest fall from a running peak of the cumulative return path.

  1. Take the current index roster, keyed on entity identifiers.
  2. Pull daily split-adjusted closes from 4 August 2021 to 3 August 2026. Set aside entities that traded under more than one symbol in the window, names without a complete daily history, and any name whose largest single-day change exceeds 100%, a size that records a corporate action rather than a price change. 435 companies and 1,253 sessions survive.
  3. For each portfolio size from 1 to 100, draw 2,000 random subsets without replacement, equally weighted and rebalanced daily so the weights stay fixed.
  4. Report the mean and the 10th and 90th percentiles of each measure across the draws.

Fixed weights buy a closed form worth checking against. Expected variance for N names drawn at random equals the average single-name variance divided by N, plus the average pairwise covariance multiplied by (1 − 1/N). The first term is what diversification removes; the second is a floor that does not move. The roster is current membership, so the panel holds index members as at the end of the window; the measures reported depend on the covariance structure rather than on average returns.

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

SIZES = [1, 2, 3, 5, 8, 10, 15, 20, 25, 30, 40, 50, 75, 100]
DRAWS, SEED = 2000, 20260804

members = xfl.index("sp500").dropna(subset=["entity_id"])
ids = sorted(set(members["entity_id"].astype(int)))
px = pd.concat([xfl.prices(entity_id=ids[i:i + 100], start="2021-08-04",
                           end="2026-08-03", fields=["adj_close"], max_rows=200000)
                for i in range(0, len(ids), 100)], ignore_index=True)

single = px.groupby("entity_id")["ticker"].nunique() == 1
wide = (px[px["entity_id"].isin(single[single].index)]
        .pivot_table(index="date", columns="ticker", values="adj_close").sort_index())
ret = wide.dropna(axis=1).pct_change().dropna()
ret = ret.drop(columns=ret.columns[ret.abs().max() >= 1.0])


def max_dd(r):
    curve = (1.0 + r).cumprod()
    return float((curve / np.maximum.accumulate(curve) - 1.0).min())


R = ret.to_numpy()
rng = np.random.default_rng(SEED)
for n in SIZES:
    vol, dd = [], []
    for _ in range(DRAWS):
        p = R[:, rng.choice(R.shape[1], size=n, replace=False)].mean(axis=1)
        vol.append(p.std(ddof=1) * np.sqrt(252))
        dd.append(max_dd(p))
    print("%4d  volatility mean %.2f unlucky %.2f   drawdown mean %.2f unlucky %.2f"
          % (n, 100 * np.mean(vol), 100 * np.percentile(vol, 90),
             100 * np.mean(dd), 100 * np.percentile(dd, 10)))

S = np.cov(R, rowvar=False, ddof=1)
avg_var, avg_cov = np.mean(np.diag(S)), S[~np.eye(len(S), dtype=bool)].mean()
print("systematic floor %.2f" % (100 * np.sqrt(avg_cov * 252)))

Full script with formatting and visualisation: how-many-stocks-to-diversify-python.py

Output

Two panels showing annualised volatility and maximum drawdown of random equal-weighted S&P 500 portfolios against the number of stocks held, with the average draw and the middle 80% of draws
S&P 500 members, daily returns from split-adjusted closes
2021-08-05 to 2026-08-03 (1,253 sessions)
503 roster entities, 477 under a single symbol across the window,
436 with a complete daily history, 435 in the panel after the corporate-action screen

Equal-weighted portfolios rebalanced daily, 2,000 random draws per size

            annualised volatility %        maximum drawdown %
  size     mean   lucky   unlucky        mean   lucky   unlucky
     1    32.00   21.41    45.97      -46.37  -27.16    -67.64
     2    25.89   19.26    34.24      -37.19  -23.70    -53.64
     3    23.31   18.20    29.30      -32.79  -21.80    -45.49
     5    20.73   16.90    25.04      -27.93  -19.51    -37.69
     8    19.21   16.34    22.55      -25.53  -18.85    -33.08
    10    18.79   16.11    21.77      -24.67  -18.50    -31.76
    15    17.93   15.87    20.13      -23.23  -18.29    -28.84
    20    17.64   15.83    19.56      -22.78  -18.26    -27.59
    25    17.37   15.78    19.07      -22.24  -18.33    -26.63
    30    17.22   15.77    18.75      -21.97  -18.22    -25.99
    40    16.98   15.74    18.33      -21.54  -18.30    -25.24
    50    16.92   15.78    18.09      -21.41  -18.42    -24.61
    75    16.72   15.87    17.58      -20.94  -18.54    -23.48
   100    16.65   15.94    17.40      -20.74  -18.70    -22.90
   all    16.45                      -20.20

  lucky = 10th percentile of the 2,000 draws, unlucky = 90th

Systematic floor from average pairwise covariance   16.39%
SPY over the same window: volatility 17.24%, maximum drawdown -25.36%

Simulated against closed form, annualised volatility
  size   simulated   closed form   difference bp
     1       33.78         33.78           -0.9
     2       26.68         26.55          +12.6
     3       23.77         23.65          +11.5
     5       20.99         21.05           -6.2
     8       19.37         19.43           -6.6
    10       18.92         18.86           +6.3
    15       18.01         18.07           -5.9
    20       17.70         17.67           +3.0
    25       17.42         17.42           -0.3
    30       17.26         17.25           +0.8
    40       17.01         17.04           -2.6
    50       16.94         16.91           +3.4
    75       16.74         16.74           +0.1
   100       16.66         16.65           +1.1

What this tells us

The average curve reproduces the textbook. A single stock averaged 32.00% annualised volatility, ten names 18.79%, thirty names 17.22%, and the whole 435-name panel 16.45%. The floor implied by average pairwise covariance is 16.39%, so the average 30-stock portfolio sits within 0.83 points of a limit it cannot cross, and the remaining 405 names buy that 0.83 between them. Simulation and closed form agree to within 13 basis points at every size, confirming the simulation measures the quantity the algebra describes.

The percentiles carry the other half. At 30 names the average draw carried 17.22% volatility and the unlucky decile carried 18.75%, which is 2.36 points above the floor rather than 0.83. Pushing the unlucky decile down to where the average 20-stock portfolio already sits takes about 75 names: 17.58% against 17.64%. The average converges roughly four times faster than the tail.

Drawdown separates them further. The whole panel fell 20.20% at worst and SPY, which is capitalisation-weighted, fell 25.36%. The average 30-stock portfolio fell 21.97%; the unlucky decile fell 25.99%, deeper than the tracker.

The shallow end stops improving early. Between 8 names and 100 the shallowest decile of drawdowns travels from 18.85% to 18.70%, no movement at all, while the deep end improves across that whole range, from 33.08% to 22.90%. Names added after the first handful buy protection against the bad draw and little else.

So what?

A mandate capped at 25 or 30 holdings is not a mandate to accept much more volatility: on average it costs about a point against a 400-name basket. What it accepts is uncertainty over which 30, and one portfolio in ten built that way carried a drawdown 5.8 points deeper than the panel over a window whose worst stretch was the 2022 decline.

Size the risk budget off the unlucky percentile rather than the average: for a 30-name book in this market, plan around a 26% drawdown, not 22%. Then treat every marginal-name decision as a question about that tail. Moving from 30 holdings to 50 shifts average volatility by 0.30 points, the unlucky decile of volatility by 0.66, and the unlucky decile of drawdown by 1.38. The case for concentration is normally argued on the average — the bill arrives in the tail.

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

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