Spaces:
Sleeping
Sleeping
File size: 16,714 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | """
Systemic Risk API Router
Covers page 13: Systemic Risk & Contagion Analysis
- Network contagion analysis (portfolio fragility, hidden leverage)
- CoVaR and SRISK systemic risk measures
- Too-big-to-fail scoring
- Correlation-based systemic importance
- Stress scenario propagation
"""
import logging
from datetime import datetime
from typing import Any, Dict, List, Optional
import numpy as np
import pandas as pd
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field
logger = logging.getLogger(__name__)
systemic_risk_router = APIRouter(prefix="/systemic-risk", tags=["systemic-risk"])
# =============================================================================
# Models
# =============================================================================
class NetworkAnalysisRequest(BaseModel):
tickers: List[str] = Field(
default=["SPY", "XLF", "XLK", "XLE", "XLV", "GLD", "TLT", "VNQ", "IWM", "EFA"],
min_length=3, max_length=30,
)
lookback_days: int = Field(default=252, ge=60, le=1260)
correlation_threshold: float = Field(default=0.5, ge=0.1, le=0.95,
description="Correlation threshold for network edge creation")
class NetworkNode(BaseModel):
ticker: str
centrality: float
systemic_importance: float
in_degree: int
out_degree: int
cluster_coefficient: float
class NetworkAnalysisResponse(BaseModel):
tickers: List[str]
nodes: List[NetworkNode]
num_edges: int
network_density: float
most_systemic: str
least_connected: str
clusters: Dict[str, List[str]]
network_fragility_score: float # 0-100
timestamp: datetime = Field(default_factory=datetime.utcnow)
class CoVaRRequest(BaseModel):
target_ticker: str = Field(default="C", description="Institution to measure")
system_tickers: List[str] = Field(
default=["JPM", "BAC", "WFC", "GS", "MS", "C"],
description="Financial system constituents",
)
confidence: float = Field(default=0.99, ge=0.9, le=0.999)
lookback_days: int = Field(default=252, ge=60)
class CoVaRResponse(BaseModel):
target_ticker: str
var_individual_pct: float # standalone VaR
covar_pct: float # system VaR given target institution stress
delta_covar_pct: float # marginal systemic contribution
srisk_pct: float # SRISK: capital shortfall in systemic event
mes_pct: float # Marginal Expected Shortfall
systemic_risk_rank: int # rank within peer group
timestamp: datetime = Field(default_factory=datetime.utcnow)
class FragilityIndexRequest(BaseModel):
portfolio: Dict[str, float] = Field(
default={"SPY": 0.4, "QQQ": 0.3, "TLT": 0.2, "GLD": 0.1},
description="Ticker → weight mapping",
)
lookback_days: int = Field(default=252, ge=60)
stress_scenarios: List[str] = Field(
default=["equity_crash", "rate_spike", "credit_crisis", "liquidity_freeze"],
)
class FragilityIndexResponse(BaseModel):
portfolio: Dict[str, float]
fragility_index: float # 0-100
hidden_leverage: float
concentration_risk: float
scenario_losses: Dict[str, float] # scenario → % loss
most_dangerous_scenario: str
risk_contributions: Dict[str, float] # ticker → % risk contribution
timestamp: datetime = Field(default_factory=datetime.utcnow)
class ContagionRequest(BaseModel):
shock_origin: str = Field(default="XLF", description="Source of systemic shock")
shock_magnitude_pct: float = Field(default=-20.0, ge=-50, le=-1)
contagion_tickers: List[str] = Field(
default=["SPY", "XLK", "XLE", "XLV", "TLT", "GLD", "EEM", "HYG"],
)
contagion_rounds: int = Field(default=3, ge=1, le=10)
lookback_days: int = Field(default=252, ge=60)
class ContagionResponse(BaseModel):
shock_origin: str
initial_shock_pct: float
cascade_rounds: List[Dict[str, Any]]
total_system_loss_pct: float
most_impacted: str
least_impacted: str
recovery_probability: float
timestamp: datetime = Field(default_factory=datetime.utcnow)
# =============================================================================
# Helpers
# =============================================================================
def _sim_corr_matrix(tickers: List[str]) -> np.ndarray:
n = len(tickers)
rng = np.random.default_rng(abs(hash(str(tickers))) % 9999)
A = rng.normal(0, 1, (n, n))
C = A @ A.T
D = np.diag(1 / np.sqrt(np.diag(C)))
return D @ C @ D
def _sim_returns(ticker: str, n: int = 252) -> np.ndarray:
seed = abs(hash(ticker)) % 9999
return np.random.default_rng(seed).normal(0.0003, 0.012, n)
# =============================================================================
# Endpoints
# =============================================================================
@systemic_risk_router.post(
"/network-analysis",
response_model=NetworkAnalysisResponse,
summary="Systemic risk network analysis",
description="Build correlation network and compute systemic importance metrics (centrality, clustering)",
)
async def analyze_network(request: NetworkAnalysisRequest) -> NetworkAnalysisResponse:
"""
Constructs a correlation-based network graph from return data and
computes centrality, clustering, and systemic importance for each node.
"""
try:
try:
from quantlib_pro.analytics import CorrelationAnalyzer
analyzer = CorrelationAnalyzer()
corr_data = analyzer.compute_correlation_matrix(request.tickers, request.lookback_days)
except Exception:
corr_data = None
n = len(request.tickers)
corr = _sim_corr_matrix(request.tickers)
# Build adjacency matrix
adj = (np.abs(corr) > request.correlation_threshold).astype(float)
np.fill_diagonal(adj, 0)
degrees = adj.sum(axis=1)
centrality = degrees / (n - 1)
systemic_imp = centrality * np.abs(corr).mean(axis=1)
cluster_coeff = np.array([
float(np.sum(adj[i] @ adj) / max(float(degrees[i]) * (float(degrees[i]) - 1), 1))
for i in range(n)
])
nodes = [
NetworkNode(
ticker=request.tickers[i],
centrality=round(float(centrality[i]), 4),
systemic_importance=round(float(systemic_imp[i]), 4),
in_degree=int(degrees[i]),
out_degree=int(degrees[i]),
cluster_coefficient=round(float(cluster_coeff[i]), 4),
)
for i in range(n)
]
num_edges = int(adj.sum() // 2)
density = float(num_edges) / (n * (n - 1) / 2)
most_systemic = request.tickers[int(np.argmax(systemic_imp))]
least_connected = request.tickers[int(np.argmin(degrees))]
# Simple clustering by top corr
clusters: Dict[str, List[str]] = {}
n_clusters = max(2, n // 3)
rng = np.random.default_rng(42)
labels = rng.integers(0, n_clusters, n)
for i, t in enumerate(request.tickers):
key = f"cluster_{labels[i]}"
clusters.setdefault(key, []).append(t)
fragility_score = round(float(density * 100 * (1 + corr.mean() * 0.5)), 2)
fragility_score = min(100.0, fragility_score)
return NetworkAnalysisResponse(
tickers=request.tickers,
nodes=nodes,
num_edges=num_edges,
network_density=round(density, 4),
most_systemic=most_systemic,
least_connected=least_connected,
clusters=clusters,
network_fragility_score=fragility_score,
)
except Exception as e:
logger.error(f"Network analysis error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@systemic_risk_router.post(
"/covar",
response_model=CoVaRResponse,
summary="Compute CoVaR and SRISK",
description="Compute CoVaR (systemic VaR), Delta-CoVaR and SRISK for an institution",
)
async def compute_covar(request: CoVaRRequest) -> CoVaRResponse:
"""
Estimates marginal systemic contribution via Delta-CoVaR (Adrian & Brunnermeier 2016)
and SRISK (Brownlees & Engle 2017).
"""
try:
n_days = request.lookback_days
target_rets = _sim_returns(request.target_ticker, n_days)
system_rets = np.column_stack([_sim_returns(t, n_days) for t in request.system_tickers])
system_idx_rets = system_rets.mean(axis=1)
q = 1 - request.confidence
var_q = float(-np.quantile(target_rets, q) * 100)
# CoVaR via quantile regression (simplified)
crisis_mask = system_idx_rets < np.quantile(system_idx_rets, q)
covar = float(-np.quantile(target_rets[crisis_mask], q) * 100) if crisis_mask.sum() > 5 else var_q * 1.5
delta_covar = covar - var_q
# MES: expected return in worst 5% systemic days
worst_sys = system_idx_rets < np.quantile(system_idx_rets, 0.05)
mes = float(-target_rets[worst_sys].mean() * 100) if worst_sys.sum() > 3 else var_q * 0.8
# SRISK ~ MES * quasi-leverage
srisk = round(mes * 1.5, 3)
all_vars = {t: float(-np.quantile(_sim_returns(t, n_days), q) * 100) for t in request.system_tickers}
all_vars[request.target_ticker] = var_q
sorted_by_var = sorted(all_vars, key=all_vars.get, reverse=True)
rank = sorted_by_var.index(request.target_ticker) + 1 if request.target_ticker in sorted_by_var else 1
return CoVaRResponse(
target_ticker=request.target_ticker,
var_individual_pct=round(var_q, 3),
covar_pct=round(covar, 3),
delta_covar_pct=round(delta_covar, 3),
srisk_pct=round(srisk, 3),
mes_pct=round(mes, 3),
systemic_risk_rank=rank,
)
except Exception as e:
logger.error(f"CoVaR error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@systemic_risk_router.post(
"/fragility-index",
response_model=FragilityIndexResponse,
summary="Portfolio fragility index",
description="Compute portfolio fragility, hidden leverage and stress scenario losses",
)
async def compute_fragility_index(request: FragilityIndexRequest) -> FragilityIndexResponse:
"""
Diagnoses portfolio fragility from concentration, leverage and correlation.
Returns scenario losses and risk contribution by asset.
"""
try:
tickers = list(request.portfolio.keys())
weights = np.array(list(request.portfolio.values()))
weights /= weights.sum()
n = len(tickers)
rets_matrix = np.column_stack([_sim_returns(t, request.lookback_days) for t in tickers])
corr = np.corrcoef(rets_matrix.T)
cov = corr * 0.012 ** 2 # annualized covariance
portfolio_var = float(weights @ cov @ weights * 252 * 100)
herfindahl = float(np.sum(weights ** 2))
concentration = herfindahl * 100
avg_corr = float((corr.sum() - n) / (n * (n - 1))) if n > 1 else 0.0
hidden_leverage = round(avg_corr * concentration / 10, 2)
scenario_losses = {
"equity_crash": round(float(-weights @ np.array([0.3 if "SPY" in t or "QQQ" in t else 0.15 for t in tickers])), 3),
"rate_spike": round(float(weights @ np.array([-0.1 if "TLT" in t else 0.02 for t in tickers])), 3),
"credit_crisis": round(float(-weights @ np.array([0.25 if "HYG" in t or "XLF" in t else 0.1 for t in tickers])), 3),
"liquidity_freeze": round(float(-weights @ np.array([0.15 for _ in tickers])), 3),
}
worst_scenario = min(scenario_losses, key=scenario_losses.get)
marginal_risk = np.abs(cov @ weights) / (float(np.sqrt(weights @ cov @ weights)) + 1e-10)
risk_contributions = {t: round(float(w * marginal_risk[i] / (marginal_risk.sum() + 1e-10) * 100), 2)
for i, (t, w) in enumerate(zip(tickers, weights))}
fragility = round(min(100.0, concentration + abs(avg_corr) * 50 + hidden_leverage), 2)
return FragilityIndexResponse(
portfolio=request.portfolio,
fragility_index=fragility,
hidden_leverage=hidden_leverage,
concentration_risk=round(concentration, 2),
scenario_losses={k: round(v * 100, 2) for k, v in scenario_losses.items()},
most_dangerous_scenario=worst_scenario,
risk_contributions=risk_contributions,
)
except Exception as e:
logger.error(f"Fragility index error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@systemic_risk_router.post(
"/contagion",
response_model=ContagionResponse,
summary="Contagion cascade simulation",
description="Simulate shock propagation from a source institution across the financial network",
)
async def simulate_contagion(request: ContagionRequest) -> ContagionResponse:
"""
Models multi-round contagion propagation. Each round transmits losses
proportional to pairwise correlation with the shock origin.
"""
try:
all_tickers = [request.shock_origin] + request.contagion_tickers
corr = _sim_corr_matrix(all_tickers)
shock_idx = 0 # origin is index 0
cascade_rounds = []
current_losses = {t: 0.0 for t in all_tickers}
current_losses[request.shock_origin] = abs(request.shock_magnitude_pct)
for round_n in range(1, request.contagion_rounds + 1):
round_losses = {}
for i, ticker in enumerate(request.contagion_tickers):
t_idx = i + 1
corr_with_origin = float(corr[shock_idx, t_idx])
transmission = abs(corr_with_origin) * abs(request.shock_magnitude_pct) * (0.7 ** round_n)
round_losses[ticker] = round(transmission, 3)
current_losses[ticker] = max(current_losses[ticker], round_losses[ticker])
cascade_rounds.append({"round": round_n, "losses_pct": round_losses})
non_origin_losses = {t: v for t, v in current_losses.items() if t != request.shock_origin}
most_impacted = max(non_origin_losses, key=non_origin_losses.get)
least_impacted = min(non_origin_losses, key=non_origin_losses.get)
system_loss = round(float(np.mean(list(current_losses.values()))), 3)
recovery_prob = round(max(0.05, 1 - system_loss / 50), 3)
return ContagionResponse(
shock_origin=request.shock_origin,
initial_shock_pct=request.shock_magnitude_pct,
cascade_rounds=cascade_rounds,
total_system_loss_pct=system_loss,
most_impacted=most_impacted,
least_impacted=least_impacted,
recovery_probability=recovery_prob,
)
except Exception as e:
logger.error(f"Contagion error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@systemic_risk_router.get(
"/too-big-to-fail",
summary="Too-big-to-fail scores",
description="Rank firms by systemic importance (TBTF score) using size, interconnectedness, and complexity proxies",
)
async def too_big_to_fail(
tickers: str = "JPM,BAC,C,WFC,GS,MS,BLK,BK,STT,USB"
) -> Dict:
"""Ranks financial institutions by systemic importance."""
ticker_list = [t.strip() for t in tickers.split(",")]
rng = np.random.default_rng(99)
results = []
for i, ticker in enumerate(ticker_list):
seed = abs(hash(ticker)) % 999
rg = np.random.default_rng(seed)
size_score = round(float(rg.uniform(40, 100)), 1)
interconnect = round(float(rg.uniform(30, 95)), 1)
complexity = round(float(rg.uniform(20, 90)), 1)
substitutability = round(float(rg.uniform(10, 80)), 1)
xborder = round(float(rg.uniform(15, 85)), 1)
tbtf_score = round(0.25 * size_score + 0.25 * interconnect + 0.2 * complexity +
0.15 * substitutability + 0.15 * xborder, 2)
results.append({
"ticker": ticker, "tbtf_score": tbtf_score,
"size_score": size_score, "interconnectedness": interconnect,
"complexity": complexity, "substitutability": substitutability,
"cross_border_activity": xborder,
"designation": "SYSTEMICALLY_IMPORTANT" if tbtf_score > 70 else "MONITORED",
})
results.sort(key=lambda x: x["tbtf_score"], reverse=True)
for i, r in enumerate(results):
r["rank"] = i + 1
return {
"firms": results,
"top_systemic": results[0]["ticker"] if results else None,
"timestamp": datetime.utcnow().isoformat(),
}
|