Correlation Regime
Markets that were uncorrelated yesterday can suddenly move in lockstep during a crisis. Horizon’s correlation regime module tracks live pairwise correlations, detects regime shifts via z-score analysis, scores contagion risk, and finds the most decorrelated markets for diversification. All math is in Rust with O(1) incremental updates.Overview
Live Correlation Matrix
hz.live_correlation_matrix() computes pairwise correlations across markets.Regime Shift Detection
hz.detect_regime_shift() flags when correlations break from historical norms.Contagion Scoring
hz.contagion_score() identifies which markets would drag others with them.Decorrelation Finder
hz.find_decorrelated() ranks markets by diversification value.Core Functions
hz.live_correlation_matrix
Compute all pairwise Pearson correlations from return series.| Parameter | Type | Description |
|---|---|---|
market_ids | list[str] | Market identifiers |
returns_matrix | list[list[float]] | Return series for each market |
CorrelationEntry objects (one per pair).
hz.detect_regime_shift
Compare historical correlation distribution to recent window using z-score.| Parameter | Type | Default | Description |
|---|---|---|---|
historical_correlations | list[float] | required | Historical correlation values |
recent_correlations | list[float] | required | Recent correlation values |
threshold | float | 2.0 | Z-score threshold for shift detection |
hz.contagion_score
Score how much a source market would drag others with it.| Parameter | Type | Description |
|---|---|---|
source_market | str | Market to analyze as contagion source |
market_ids | list[str] | All market identifiers |
correlations | list[float] | Correlation of each market to the source |
price_changes | list[float] | Recent price changes |
threshold | float | Minimum correlation to count as affected |
hz.find_decorrelated
Rank markets by diversification value (low average correlation).hz.incremental_correlation_update
O(1) single-pass correlation update. Maintain running statistics without recomputing from scratch.Pipeline Functions
hz.correlation_matrix
Track live correlations across feeds each cycle.| Parameter | Type | Default | Description |
|---|---|---|---|
feed_names | list[str] | required | Feed names to track |
window | int | 50 | Rolling window size |
hz.regime_shift
Detect correlation regime shifts.hz.contagion_alert
Monitor for contagion risk across markets.hz.decorrelation_finder
Find the most decorrelated markets for portfolio diversification.Examples
Correlation-Aware Portfolio
Mathematical Background
Pearson Correlation
Pearson Correlation
For two return series X and Y:r = [nSXY - SXSY] / sqrt([nSX2 - SX^2] * [nSY2 - SY^2])Where SX, SY are sums, SXY is sum of products, SX2, SY2 are sums of squares. The incremental update adds one (x, y) pair and recomputes in O(1).
Regime Shift Detection
Regime Shift Detection
Compares the mean of recent correlations to the historical distribution:z = (mean_recent - mean_historical) / std_historicalIf |z| exceeds the threshold (default 2.0), a regime shift is flagged. The confidence score equals the z-value.
Contagion Score
Contagion Score
For a source market, contagion score is the correlation-weighted sum of absolute price changes across affected markets (those with correlation above the threshold).