Market Data & Web Scraping Beginner

Generate Multi-Stock Return Correlation Matrix & Diversification Filter in Python

Compute Pearson and Spearman daily return correlation matrices across a stock basket in Python Pandas and flag highly correlated redundant pairs (>0.80).

#pandas#numpy#yfinance

Problem Statement

Holding 10 stocks that all have a 0.85 correlation with each other provides zero diversification during a market crash. This script downloads returns and isolates highly correlated pairs.

Formula / Core Logic

Daily_Return = Close.pct_change() Correlation r_xy = Cov(X, Y) / (Std(X) * Std(Y))

Installation

pip install pandas numpy yfinance
correlation-matrix-heatmap-stock-portfolio-python.py
import pandas as pd
import numpy as np

def find_high_correlation_pairs(prices_df, threshold=0.80):
    corr = prices_df.pct_change().dropna().corr()
    pairs = []
    cols = corr.columns
    for i in range(len(cols)):
        for j in range(i + 1, len(cols)):
            val = corr.iloc[i, j]
            if abs(val) >= threshold:
                pairs.append({'Asset_1': cols[i], 'Asset_2': cols[j], 'Correlation': round(val, 3)})
    return corr, pd.DataFrame(pairs)

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: Why calculate correlation on `.pct_change()` instead of raw stock prices?

Raw stock prices are non-stationary and exhibit spurious correlation due to inflation and long-term index drift; always correlate daily percentage returns.