File size: 8,575 Bytes
72fc0ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
Correlation analysis and regime detection.

Analyzes:
  - Rolling correlations across asset classes
  - Correlation regime shifts (calm → stress → crisis)
  - Eigenvalue spectrum and diversification collapse
  - Correlation contagion and breakdown
"""

from __future__ import annotations

import logging
from dataclasses import dataclass
from typing import Optional

import numpy as np
import pandas as pd
from scipy.linalg import eigh

from quantlib_pro.utils.validation import ValidationError, require_positive

log = logging.getLogger(__name__)


@dataclass
class CorrelationMetrics:
    """Correlation matrix metrics."""
    timestamp: float
    avg_correlation: float
    max_correlation: float
    min_correlation: float
    eigenvalues: np.ndarray
    diversification_ratio: float  # Largest eigenvalue / sum
    regime: str  # 'calm', 'stress', 'crisis'


def rolling_correlation(
    returns: pd.DataFrame,
    window: int = 30,
) -> list[pd.DataFrame]:
    """
    Compute rolling correlation matrices.
    
    Parameters
    ----------
    returns : pd.DataFrame
        Asset returns (rows=time, columns=assets)
    window : int
        Rolling window size
    
    Returns
    -------
    list[pd.DataFrame]
        Rolling correlation matrices
    """
    require_positive(window, "window")
    
    if len(returns) < window:
        raise ValidationError(f"Returns length {len(returns)} < window {window}")
    
    corr_matrices = []
    
    for i in range(window, len(returns) + 1):
        window_returns = returns.iloc[i - window:i]
        corr = window_returns.corr()
        corr_matrices.append(corr)
    
    return corr_matrices


def correlation_regime(avg_corr: float) -> str:
    """
    Classify correlation regime.
    
    Parameters
    ----------
    avg_corr : float
        Average pairwise correlation
    
    Returns
    -------
    str
        'calm', 'stress', or 'crisis'
    """
    if avg_corr < 0.3:
        return 'calm'
    elif avg_corr < 0.6:
        return 'stress'
    else:
        return 'crisis'


def compute_correlation_metrics(
    corr_matrix: pd.DataFrame,
    timestamp: float = 0.0,
) -> CorrelationMetrics:
    """
    Compute metrics from correlation matrix.
    
    Parameters
    ----------
    corr_matrix : pd.DataFrame
        Correlation matrix (n x n)
    timestamp : float
        Timestamp for metrics
    
    Returns
    -------
    CorrelationMetrics
        Correlation metrics
    """
    n = len(corr_matrix)
    
    # Extract off-diagonal correlations
    mask = ~np.eye(n, dtype=bool)
    off_diag = corr_matrix.values[mask]
    
    avg_corr = float(np.mean(off_diag))
    max_corr = float(np.max(off_diag))
    min_corr = float(np.min(off_diag))
    
    # Eigenvalue analysis
    eigvals, _ = eigh(corr_matrix.values)
    eigvals = np.sort(eigvals)[::-1]  # Descending order
    
    # Diversification ratio
    div_ratio = eigvals[0] / np.sum(eigvals)
    
    regime = correlation_regime(avg_corr)
    
    return CorrelationMetrics(
        timestamp=timestamp,
        avg_correlation=avg_corr,
        max_correlation=max_corr,
        min_correlation=min_corr,
        eigenvalues=eigvals,
        diversification_ratio=float(div_ratio),
        regime=regime,
    )


def detect_correlation_breakdowns(
    corr_history: list[pd.DataFrame],
    threshold: float = 0.3,
) -> list[int]:
    """
    Detect correlation breakdown events.
    
    Breakdown = sudden jump in average correlation.
    
    Parameters
    ----------
    corr_history : list[pd.DataFrame]
        Historical correlation matrices
    threshold : float
        Minimum correlation increase to trigger breakdown
    
    Returns
    -------
    list[int]
        Indices of breakdown events
    """
    if len(corr_history) < 2:
        return []
    
    avg_corrs = []
    for corr in corr_history:
        n = len(corr)
        mask = ~np.eye(n, dtype=bool)
        avg_corrs.append(np.mean(corr.values[mask]))
    
    breakdowns = []
    
    for i in range(1, len(avg_corrs)):
        delta = avg_corrs[i] - avg_corrs[i - 1]
        if delta > threshold:
            breakdowns.append(i)
    
    return breakdowns


def correlation_contagion_score(
    corr_matrix: pd.DataFrame,
    baseline_corr: float = 0.3,
) -> float:
    """
    Measure correlation contagion.
    
    Contagion score = (avg_corr - baseline) / (1 - baseline)
    
    Parameters
    ----------
    corr_matrix : pd.DataFrame
        Current correlation matrix
    baseline_corr : float
        Baseline correlation level
    
    Returns
    -------
    float
        Contagion score [0, 1]
    """
    n = len(corr_matrix)
    mask = ~np.eye(n, dtype=bool)
    avg_corr = np.mean(corr_matrix.values[mask])
    
    if baseline_corr >= 1.0:
        return 0.0
    
    score = (avg_corr - baseline_corr) / (1.0 - baseline_corr)
    return max(0.0, min(1.0, score))


def eigenvalue_concentration(eigvals: np.ndarray, top_k: int = 3) -> float:
    """
    Measure eigenvalue concentration.
    
    Concentration = sum(top_k eigenvalues) / sum(all eigenvalues)
    
    High concentration → low diversification.
    
    Parameters
    ----------
    eigvals : np.ndarray
        Eigenvalues (sorted descending)
    top_k : int
        Number of top eigenvalues to consider
    
    Returns
    -------
    float
        Concentration ratio [0, 1]
    """
    if len(eigvals) == 0:
        return 0.0
    
    top_k = min(top_k, len(eigvals))
    return np.sum(eigvals[:top_k]) / np.sum(eigvals)


def make_psd(matrix: np.ndarray, tol: float = 1e-8) -> np.ndarray:
    """
    Make matrix positive semi-definite.
    
    Parameters
    ----------
    matrix : np.ndarray
        Symmetric matrix
    tol : float
        Minimum eigenvalue tolerance
    
    Returns
    -------
    np.ndarray
        PSD matrix
    """
    # Symmetrize
    sym = (matrix + matrix.T) / 2.0
    
    # Eigenvalue decomposition
    eigvals, eigvecs = eigh(sym)
    
    # Clip negative eigenvalues
    eigvals[eigvals < tol] = tol
    
    # Reconstruct
    psd = eigvecs @ np.diag(eigvals) @ eigvecs.T
    
    # Re-symmetrize
    return (psd + psd.T) / 2.0


def simulate_correlation_shock(
    base_corr: float,
    shock_intensity: float,
    n_assets: int,
) -> np.ndarray:
    """
    Simulate a correlation shock.
    
    Nonlinear transformation pushes correlations toward 1.
    
    Parameters
    ----------
    base_corr : float
        Base correlation level
    shock_intensity : float
        Shock intensity [0, 1]
    n_assets : int
        Number of assets
    
    Returns
    -------
    np.ndarray
        Shocked correlation matrix
    """
    require_positive(n_assets, "n_assets")
    
    if not 0 <= shock_intensity <= 1:
        raise ValidationError("shock_intensity must be in [0, 1]")
    
    # Base correlation matrix
    corr = np.full((n_assets, n_assets), base_corr)
    np.fill_diagonal(corr, 1.0)
    
    # Nonlinear shock: corr → corr + (1 - corr) * (1 - exp(-k * shock))
    k = 3.0
    shocked_corr = corr + (1.0 - corr) * (1.0 - np.exp(-k * shock_intensity))
    
    # Ensure PSD
    shocked_corr = make_psd(shocked_corr)
    
    # Re-normalize diagonal
    np.fill_diagonal(shocked_corr, 1.0)
    
    return shocked_corr


def cross_asset_correlation(
    returns: pd.DataFrame,
    asset1: str,
    asset2: str,
    window: int = 30,
) -> pd.Series:
    """
    Compute rolling correlation between two assets.
    
    Parameters
    ----------
    returns : pd.DataFrame
        Asset returns
    asset1 : str
        First asset
    asset2 : str
        Second asset
    window : int
        Rolling window
    
    Returns
    -------
    pd.Series
        Rolling correlation time series
    """
    if asset1 not in returns.columns:
        raise ValidationError(f"Asset {asset1} not in returns")
    if asset2 not in returns.columns:
        raise ValidationError(f"Asset {asset2} not in returns")
    
    return returns[asset1].rolling(window).corr(returns[asset2])


def correlation_heatmap_data(
    corr_matrix: pd.DataFrame,
) -> dict:
    """
    Prepare correlation matrix for heatmap visualization.
    
    Parameters
    ----------
    corr_matrix : pd.DataFrame
        Correlation matrix
    
    Returns
    -------
    dict
        Heatmap data with z, x, y, labels
    """
    return {
        'z': corr_matrix.values.tolist(),
        'x': corr_matrix.columns.tolist(),
        'y': corr_matrix.index.tolist(),
        'labels': corr_matrix.columns.tolist(),
    }