使用df.corr()计算标普500股票相关性全为1的问题求助
Hey there, I’ve run into this exact head-scratcher before with stock price data—let’s break down why this is happening and how to fix it.
The Most Likely Culprit: Using Price Data Instead of Returns
Stock prices for S&P 500 components tend to move in the same general direction over time (think bull markets where almost everything goes up, or bear markets where most drop). This means their raw price series will have near-perfect positive correlation, hence the all-1 matrix and solid green heatmap.
You need to work with daily returns (percentage changes) instead—this captures how each stock moves relative to others, which will show the varied positive/negative correlations you expect.
Here’s how to adjust your code:
def visualize_data(): # Assuming df is your raw price DataFrame # Calculate daily percentage returns, drop the first row (NaN from first change) returns_df = df.pct_change().dropna() # Compute correlation matrix on returns corr_matrix = returns_df.corr() # Plot the heatmap with a diverging colormap (coolwarm shows positive/negative clearly) import seaborn as sns import matplotlib.pyplot as plt plt.figure(figsize=(12, 8)) sns.heatmap(corr_matrix, cmap='coolwarm', center=0, annot=False, fmt='.2f') plt.title('S&P 500 Stock Returns Correlation') plt.show()
Other Possible Issues to Check
If switching to returns doesn’t fix it, rule out these edge cases:
- Duplicate columns: If you accidentally imported the same stock data multiple times, those columns will have a perfect correlation of 1. Check with
df.T.duplicated()to spot duplicates. - Non-varying data: If any column has all identical values (e.g., a stock that never moved), its correlation with everything will be undefined or 1. Use
df.nunique()to check columns with only 1 unique value. - Data type errors: Ensure all columns are numeric (
df.dtypesshould showfloat64orint64). Non-numeric data can cause unexpected behavior incorr().
Give the returns approach a shot first—it’s 90% of the time the fix for this exact scenario with stock data.
内容的提问来源于stack exchange,提问作者Q. Wieber

