Cross-asset correlation analysis including rolling correlation, hierarchical clustering, tail dependence, and regime-dependent correlation
npx skills add https://github.com/agiprolabs/claude-trading-skills --skill correlation-analysis
Cross-asset correlation analysis for diversification assessment, risk management, pairs trading signal generation, and portfolio construction.
Correlation measures how assets move together. In crypto markets this is critical for:
Linear correlation assuming normality. Most common but least robust for crypto.
import pandas as pd
import numpy as np
# Always compute on returns, never on prices
returns_a = prices_a.pct_change().dropna()
returns_b = prices_b.pct_change().dropna()
pearson_corr = returns_a.corr(returns_b) # default is Pearson
Converts values to ranks, then computes Pearson on ranks. Captures monotonic (not just linear) relationships.
spearman_corr = returns_a.corr(returns_b, method='spearman')
Counts concordant vs discordant pairs. Most robust to outliers.
kendall_corr = returns_a.corr(returns_b, method='kendall')
Static correlation hides regime changes. Rolling correlation reveals how relationships evolve.
# Rolling Pearson correlation
rolling_corr = returns_a.rolling(window=60).corr(returns_b)
# Multiple windows for different time horizons
windows = {
'short': 20, # ~1 month of trading days
'medium': 60, # ~3 months
'long': 120, # ~6 months
}
for label, w in windows.items():
df[f'corr_{label}'] = returns_a.rolling(w).corr(returns_b)
Exponentially weighted — more responsive to recent changes.
def ewma_correlation(x: pd.Series, y: pd.Series, span: int = 60) -> pd.Series:
"""Compute EWMA correlation between two return series."""
cov_xy = x.mul(y).ewm(span=span).mean() - x.ewm(span=span).mean() * y.ewm(span=span).mean()
std_x = x.ewm(span=span).std()
std_y = y.ewm(span=span).std()
return cov_xy / (std_x * std_y)
| Window | Days | Use Case |
|--------|------|----------|
| Short | 20 | Tactical trading, pairs entry/exit |
| Medium | 60 | Strategy allocation, regime detection |
| Long | 120 | Portfolio construction, strategic allocation |
# Build return matrix for multiple assets
returns = pd.DataFrame({
'BTC': btc_returns,
'ETH': eth_returns,
'SOL': sol_returns,
'AVAX': avax_returns,
})
# Correlation matrix (Pearson)
corr_matrix = returns.corr()
# Spearman (better for crypto)
spearman_matrix = returns.corr(method='spearman')
Decompose the correlation matrix to identify driving factors.
eigenvalues, eigenvectors = np.linalg.eigh(corr_matrix.values)
# Sort descending
idx = eigenvalues.argsort()[::-1]
eigenvalues = eigenvalues[idx]
eigenvectors = eigenvectors[:, idx]
# First eigenvalue = market factor (explains most variance)
# Subsequent eigenvalues = sector/style factors
market_factor_pct = eigenvalues[0] / eigenvalues.sum() * 100
from numpy.linalg import inv
cov_matrix = returns.cov()
ones = np.ones(len(cov_matrix))
inv_cov = inv(cov_matrix.values)
# Minimum variance weights
weights = inv_cov @ ones / (ones @ inv_cov @ ones)
Group assets by correlation similarity to identify natural clusters.
from scipy.cluster.hierarchy import linkage, fcluster
from scipy.spatial.distance import squareform
# Convert correlation to distance
dist_matrix = np.sqrt(2 * (1 - corr_matrix.values))
np.fill_diagonal(dist_matrix, 0)
# Hierarchical clustering
condensed = squareform(dist_matrix)
linkage_matrix = linkage(condensed, method='ward')
# Cut at threshold to get clusters
clusters = fcluster(linkage_matrix, t=1.0, criterion='distance')
Applications:
Normal correlation understates co-movement during crashes. Tail dependence measures how often assets experience extreme returns simultaneously.
def tail_dependence(x: pd.Series, y: pd.Series, quantile: float = 0.05) -> float:
"""Estimate lower tail dependence coefficient.
Measures P(Y < q | X < q) for quantile q.
Higher values mean assets crash together more often.
"""
threshold_x = x.quantile(quantile)
threshold_y = y.quantile(quantile)
joint_extreme = ((x < threshold_x) & (y < threshold_y)).sum()
marginal_extreme = (x < threshold_x).sum()
return joint_extreme / marginal_extreme if marginal_extreme > 0 else 0.0
In crypto markets, tail dependence typically exceeds normal correlation:
Correlation is not constant — it changes with market regime.
| Regime | Typical Correlation | Implication |
|--------|-------------------|-------------|
| Bull (trending up) | 0.4–0.7 | Moderate — some diversification works |
| Range-bound | 0.2–0.5 | Lower — best diversification environment |
| Bear (crash) | 0.8–0.95 | Very high — diversification fails |
| Recovery | 0.5–0.7 | Declining from crash highs |
def correlation_zscore(rolling_corr: pd.Series, lookback: int = 252) -> pd.Series:
"""Z-score of rolling correlation vs its own history."""
mean = rolling_corr.rolling(lookback).mean()
std = rolling_corr.rolling(lookback).std()
return (rolling_corr - mean) / std
# Flag regime shift when z-score exceeds threshold
zscore = correlation_zscore(rolling_corr_60d)
regime_shift = zscore.abs() > 2.0
| Pair | Normal Range | Notes |
|------|-------------|-------|
| BTC / ETH | 0.7–0.9 | Highest among majors |
| BTC / SOL | 0.6–0.85 | SOL more volatile, slightly less correlated |
| BTC / Altcoin | 0.5–0.8 | Varies by market cap and sector |
| Meme / BTC | 0.2–0.5 | Lower normal correlation |
| Meme / Meme | 0.1–0.4 | Low normal but high tail dependence |
| Stablecoin / BTC | -0.1–0.1 | Should be near zero |
references/methodology.md — Correlation formulas, statistical tests, estimation methodsreferences/portfolio_applications.md — Diversification metrics, pairs trading, risk decompositionscripts/correlation_matrix.py — Multi-asset correlation matrix, clustering, diversification metricsscripts/rolling_correlation.py — Rolling correlation, regime detection, tail dependence analysisIntegration with protocols.io API for managing scientific protocols. This skill should be used when working with protocols.io to search, create, update, or publish protocols; manage protocol steps and materials; handle discussions and comments; organize workspaces; upload and manage files; or integrate protocols.io functionality into workflows. Applicable for protocol discovery, collaborative protocol development, experiment tracking, lab protocol management, and scientific documentation.
Analyzes job descriptions and generates tailored resumes that highlight relevant experience, skills, and achievements to maximize interview chances
Generate Excalidraw diagrams from natural language descriptions. Use when asked to "create a diagram", "make a flowchart", "visualize a process", "draw a system architecture", "create a mind map", or "generate an Excalidraw file". Supports flowcharts, relationship diagrams, mind maps, and system architecture diagrams. Outputs .excalidraw JSON files that can be opened directly in Excalidraw.
Build and distribute Expo development clients locally or via TestFlight
Use when you have a written implementation plan to execute in a separate session with review checkpoints
Data structure for annotated matrices in single-cell analysis. Use when working with .h5ad files or integrating with the scverse ecosystem. This is the data format skill—for analysis workflows use scanpy; for probabilistic models use scvi-tools; for population-scale queries use cellxgene-census.
Benchling R&D platform integration. Access registry (DNA, proteins), inventory, ELN entries, workflows via API, build Benchling Apps, query Data Warehouse, for lab data management automation.
Comprehensive molecular biology toolkit. Use for sequence manipulation, file parsing (FASTA/GenBank/PDB), phylogenetics, and programmatic NCBI/PubMed access (Bio.Entrez). Best for batch processing, custom bioinformatics pipelines, BLAST automation. For quick lookups use gget; for multi-service integration use bioservices.
Take agiprolabs/correlation-analysis from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.