Spaces:
Sleeping
Sleeping
Download quantlib_pro/api/routers_systemic_risk.py from gdukens/QuantLib_Pro: direct link, hf CLI and curl.
- Browser
- Download file 16.7 kB
-
https://huggingface.co/spaces/gdukens/QuantLib_Pro/resolve/main/quantlib_pro/api/routers_systemic_risk.py
- Command line
-
hf download hf://spaces/gdukens/QuantLib_Pro/quantlib_pro/api/routers_systemic_risk.py
-
curl -L -o routers_systemic_risk.py https://huggingface.co/spaces/gdukens/QuantLib_Pro/resolve/main/quantlib_pro/api/routers_systemic_risk.py
16.7 kB
| """ | |
| 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 | |
| # ============================================================================= | |
| 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)) | |
| 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)) | |
| 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)) | |
| 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)) | |
| 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(), | |
| } | |