Clustermap

Financial Asset Correlations

Cross-asset correlation matrix with sector clustering

Output
Financial Asset Correlations
Python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)

# Simulated asset returns correlation
assets = ['SPY', 'QQQ', 'IWM', 'TLT', 'GLD', 'USO', 'VNQ', 'EEM', 'FXE', 'BTC']
n = len(assets)

# Create realistic correlation structure
corr = np.eye(n)
# Equity correlations (high)
for i in range(4):
    for j in range(i+1, 4):
        c = 0.6 + np.random.rand() * 0.3
        corr[i, j] = corr[j, i] = c

# Negative bond correlation with equities
for i in range(4):
    corr[i, 3] = corr[3, i] = -0.2 - np.random.rand() * 0.2

# Other correlations
for i in range(n):
    for j in range(i+1, n):
        if corr[i, j] == 0:
            corr[i, j] = corr[j, i] = np.random.rand() * 0.4 - 0.2

df = pd.DataFrame(corr, index=assets, columns=assets)

# Asset class colors
classes = ['Equity']*3 + ['Bond'] + ['Commodity']*2 + ['REIT', 'Equity', 'Currency', 'Crypto']
class_palette = {'Equity': '#27D3F5', 'Bond': '#6CF527', 'Commodity': '#F5B027', 
                 'REIT': '#F5276C', 'Currency': '#4927F5', 'Crypto': '#F527B0'}
colors = pd.Series(classes, index=assets).map(class_palette)

# Diverging NEON colormap
neon_div = LinearSegmentedColormap.from_list('neon_div', ['#27D3F5', '#0a0a0f', '#F5276C'])

g = sns.clustermap(df, cmap=neon_div, center=0, vmin=-1, vmax=1,
                   row_colors=colors, col_colors=colors,
                   method='average', linewidths=0.5, linecolor='#1a1a2e',
                   figsize=(8, 7), dendrogram_ratio=(0.12, 0.12),
                   cbar_pos=(0.01, 0.08, 0.008, 0.12),
                   tree_kws={'linewidths': 1.5, 'colors': '#27D3F5'})

g.fig.patch.set_facecolor('#0a0a0f')
g.ax_heatmap.set_facecolor('#0a0a0f')
g.ax_heatmap.tick_params(colors='white', labelsize=9)

for ax in [g.ax_row_dendrogram, g.ax_col_dendrogram]:
    ax.set_facecolor('#0a0a0f')

g.fig.suptitle('Asset Correlation Matrix', color='white', fontsize=14, fontweight='bold', y=1.02)


plt.show()
Library

Matplotlib

Category

Heatmaps & Density

Did this help you?

Support PyLucid to keep it free & growing

Support