Skip to main content
Ultra Feature. Requires an Ultra subscription. Get started at api.mathematicalcompany.com

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.
Returns a list of CorrelationEntry objects (one per pair).

hz.detect_regime_shift

Compare historical correlation distribution to recent window using z-score.

hz.contagion_score

Score how much a source market would drag others with it.

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.

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

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).
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.
For a source market, contagion score is the correlation-weighted sum of absolute price changes across affected markets (those with correlation above the threshold).