Correlation Regime

Live correlation tracking with z-score regime shift detection and contagion scoring

Markets that are uncorrelated in normal times can become highly correlated during crises. hz.live_correlation_matrix() tracks pairwise correlations incrementally, and hz.detect_regime_shift() flags when the correlation structure has changed significantly.

API

Live correlation matrix

python
import horizon as hz

tracker = hz.live_correlation_matrix(market_ids=["AAPL", "MSFT", "NVDA", "BTC"])

for tick in data:
    tracker.update(tick.market_id, tick.return_value)

corr = tracker.matrix()   # 4×4 correlation matrix

Updates are O(1) per observation using incremental Welford statistics.

Regime shift detection

python
shift = hz.detect_regime_shift(
    tracker,
    lookback=60,
    z_threshold=2.5,
)

if shift.detected:
    print(f"Correlation regime changed: z={shift.z_score:.2f}")
    print(f"Affected pairs: {shift.changed_pairs}")

Compares the recent correlation matrix against its rolling history. A z-score above z_threshold on any pair triggers a regime shift flag.

Contagion scoring

python
score = hz.contagion_score(tracker, target="AAPL")
# score: float in [0, 1] - how connected AAPL is to the rest of the portfolio

High contagion score means the asset moves with everything else during stress. Low score means it provides genuine diversification.

Find decorrelated markets

python
candidates = hz.find_decorrelated(tracker, threshold=0.3)
# Returns market pairs with |correlation| < threshold

When to use

  • Portfolio construction: monitor whether your “diversified” portfolio is actually diversified under current conditions
  • Risk management: spike in contagion score → tighten position limits
  • Regime adaptation: switch strategies when correlation structure shifts (e.g., from sector-rotation to risk-on/risk-off)

Next