Spaces:
Sleeping
Sleeping
Download quantlib_pro/api/routers_analytics.py from gdukens/QuantLib_Pro: direct link, hf CLI and curl.
- Browser
- Download file 18.1 kB
-
https://huggingface.co/spaces/gdukens/QuantLib_Pro/resolve/main/quantlib_pro/api/routers_analytics.py
- Command line
-
hf download hf://spaces/gdukens/QuantLib_Pro/quantlib_pro/api/routers_analytics.py
-
curl -L -o routers_analytics.py https://huggingface.co/spaces/gdukens/QuantLib_Pro/resolve/main/quantlib_pro/api/routers_analytics.py
18.1 kB
| """ | |
| Advanced Analytics API Router | |
| Covers page 8: Advanced Analytics | |
| - Correlation analysis (rolling, regime-based, contagion shock) | |
| - Tail risk (VaR, CVaR, Expected Shortfall, distribution morphing) | |
| - Performance profiling (bottleneck analysis, latency tracking) | |
| - Stress testing (multi-scenario, historical, hypothetical) | |
| """ | |
| 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__) | |
| analytics_router = APIRouter( | |
| prefix="/analytics", | |
| tags=["Advanced Analytics"], | |
| responses={ | |
| 429: {"description": "Rate limit exceeded"}, | |
| 500: {"description": "Internal server error"} | |
| } | |
| ) | |
| # ============================================================================= | |
| # Models | |
| # ============================================================================= | |
| class CorrelationRequest(BaseModel): | |
| tickers: List[str] = Field(default=["SPY", "QQQ", "TLT", "GLD"], | |
| description="List of tickers for correlation analysis") | |
| start_date: str = Field(default="2022-01-01") | |
| end_date: str = Field(default="2024-12-31") | |
| rolling_window: int = Field(default=60, ge=10, le=252, description="Rolling window for correlation") | |
| method: str = Field(default="pearson", description="pearson | spearman | kendall") | |
| detect_regimes: bool = Field(default=True, description="Detect correlation regimes") | |
| shock_magnitude: float = Field(default=0.3, ge=0, le=1.0, description="Correlation shock magnitude") | |
| class CorrelationResponse(BaseModel): | |
| tickers: List[str] | |
| correlation_matrix: Dict[str, Dict[str, float]] | |
| rolling_correlation_avg: List[float] | |
| eigenvalue_concentration: float | |
| condition_number: float | |
| regime: str # high_correlation | low_correlation | transitioning | |
| contagion_score: float | |
| number_of_clusters: int | |
| clusters: Dict[str, List[str]] | |
| shocked_correlation_matrix: Dict[str, Dict[str, float]] | |
| stability_index: float | |
| timestamp: datetime = Field(default_factory=datetime.utcnow) | |
| class TailRiskRequest(BaseModel): | |
| tickers: List[str] = Field(default=["SPY", "QQQ", "TLT"]) | |
| weights: Optional[List[float]] = Field(None, description="Portfolio weights (sum to 1)") | |
| confidence_levels: List[float] = Field(default=[0.95, 0.99, 0.999]) | |
| time_horizon: int = Field(default=1, ge=1, le=252, description="Horizon in trading days") | |
| n_simulations: int = Field(default=10000, ge=1000, le=100000) | |
| distribution: str = Field(default="normal", description="normal | t | skewed_t | empirical") | |
| df: int = Field(default=5, ge=2, le=30, description="Degrees of freedom for t-distribution") | |
| class TailRiskResponse(BaseModel): | |
| portfolio_var: Dict[str, float] # CL -> VaR | |
| portfolio_cvar: Dict[str, float] # CL -> CVaR | |
| expected_shortfall: Dict[str, float] | |
| skewness: float | |
| excess_kurtosis: float | |
| tail_index: float | |
| distribution_fit: str | |
| worst_5_days: List[float] | |
| best_5_days: List[float] | |
| left_tail_probability: float | |
| right_tail_probability: float | |
| timestamp: datetime = Field(default_factory=datetime.utcnow) | |
| class PerformanceProfileRequest(BaseModel): | |
| operations: List[str] = Field( | |
| default=["portfolio_optimization", "var_calculation", "options_pricing", "regime_detection"], | |
| description="List of operations to profile" | |
| ) | |
| n_iterations: int = Field(default=10, ge=1, le=100) | |
| class PerformanceProfileResponse(BaseModel): | |
| operations: Dict[str, Dict[str, float]] | |
| total_time_ms: float | |
| bottlenecks: List[str] | |
| optimization_suggestions: List[str] | |
| timestamp: datetime = Field(default_factory=datetime.utcnow) | |
| class CorrelationContagionRequest(BaseModel): | |
| tickers: List[str] = Field(default=["SPY", "QQQ", "TLT", "GLD", "VIX"]) | |
| shock_source: str = Field(default="SPY", description="Ticker that experiences the initial shock") | |
| shock_size: float = Field(default=-0.05, description="Magnitude of initial shock (negative = drawdown)") | |
| contagion_speed: float = Field(default=0.7, ge=0, le=1.0, description="Speed of contagion spread") | |
| class CorrelationContagionResponse(BaseModel): | |
| shock_source: str | |
| initial_shock_pct: float | |
| contagion_effects: Dict[str, float] | |
| network_centrality: Dict[str, float] | |
| total_system_loss_pct: float | |
| cascade_stages: int | |
| most_vulnerable: List[str] | |
| safest_assets: List[str] | |
| timestamp: datetime = Field(default_factory=datetime.utcnow) | |
| # ============================================================================= | |
| # Helpers | |
| # ============================================================================= | |
| def _generate_correlated_returns(n: int, n_days: int, base_corr: float = 0.6) -> np.ndarray: | |
| """Generate correlated return series for n assets.""" | |
| corr_matrix = np.full((n, n), base_corr) | |
| np.fill_diagonal(corr_matrix, 1.0) | |
| L = np.linalg.cholesky(corr_matrix) | |
| rng = np.random.default_rng(42) | |
| raw = rng.standard_normal((n_days, n)) | |
| return raw @ L.T * 0.012 + 0.0002 | |
| # ============================================================================= | |
| # Endpoints | |
| # ============================================================================= | |
| async def compute_correlation(request: CorrelationRequest) -> CorrelationResponse: | |
| """ | |
| Computes the full correlation matrix, rolling correlation, eigenvalue analysis, | |
| regime detection and contagion shock simulation. | |
| """ | |
| try: | |
| n = len(request.tickers) | |
| try: | |
| from quantlib_pro.analytics import CorrelationAnalyzer | |
| analyzer = CorrelationAnalyzer() | |
| # Use real module if available | |
| except Exception: | |
| pass | |
| # Simulate correlated returns | |
| n_days = max(100, request.rolling_window * 3) | |
| returns = _generate_correlated_returns(n, n_days) | |
| df_returns = pd.DataFrame(returns, columns=request.tickers) | |
| # Correlation matrix | |
| corr = df_returns.corr(method=request.method) | |
| corr_dict = {t: {t2: round(corr.loc[t, t2], 4) for t2 in request.tickers} | |
| for t in request.tickers} | |
| # Eigenvalue concentration (Herfindahl of eigenvalues) | |
| eigenvalues = np.linalg.eigvalsh(corr.values) | |
| eigenvalues = np.abs(eigenvalues) | |
| ev_sum = eigenvalues.sum() | |
| ev_concentration = float(np.sum((eigenvalues / ev_sum) ** 2)) if ev_sum > 0 else 0 | |
| condition_number = float(eigenvalues.max() / max(eigenvalues.min(), 1e-10)) | |
| # Rolling average correlation | |
| rolling_avg = [] | |
| for i in range(request.rolling_window, n_days, 5): | |
| window = df_returns.iloc[i - request.rolling_window:i] | |
| c = window.corr().values | |
| mask = np.triu(np.ones_like(c, dtype=bool), k=1) | |
| rolling_avg.append(round(float(c[mask].mean()), 4)) | |
| # Regime detection | |
| recent_corr = np.triu(corr.values, k=1) | |
| avg_corr = float(recent_corr[recent_corr != 0].mean()) if len(recent_corr[recent_corr != 0]) > 0 else 0 | |
| regime = "high_correlation" if avg_corr > 0.7 else ("low_correlation" if avg_corr < 0.3 else "transitioning") | |
| # Contagion score (average pairwise correlation) | |
| contagion_score = round(avg_corr, 4) | |
| # Simple clustering (above/below mean correlation) | |
| clusters = {"cluster_1": request.tickers[:max(n // 2, 1)], | |
| "cluster_2": request.tickers[max(n // 2, 1):]} | |
| # Shocked correlation (increase all off-diagonal by shock_magnitude) | |
| shocked = corr.copy() | |
| for i in range(n): | |
| for j in range(n): | |
| if i != j: | |
| shocked.iloc[i, j] = min(0.99, shocked.iloc[i, j] + request.shock_magnitude * 0.5) | |
| shocked_dict = {t: {t2: round(shocked.loc[t, t2], 4) for t2 in request.tickers} | |
| for t in request.tickers} | |
| # Stability (deviation from identity) | |
| stability = 1 - ev_concentration | |
| return CorrelationResponse( | |
| tickers=request.tickers, | |
| correlation_matrix=corr_dict, | |
| rolling_correlation_avg=rolling_avg[-20:], | |
| eigenvalue_concentration=round(ev_concentration, 4), | |
| condition_number=round(condition_number, 2), | |
| regime=regime, | |
| contagion_score=contagion_score, | |
| number_of_clusters=2, | |
| clusters=clusters, | |
| shocked_correlation_matrix=shocked_dict, | |
| stability_index=round(stability, 4), | |
| ) | |
| except Exception as e: | |
| logger.error(f"Correlation error: {e}", exc_info=True) | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def compute_tail_risk(request: TailRiskRequest) -> TailRiskResponse: | |
| """ | |
| Comprehensive tail risk analysis using normal, Student-t, or empirical distribution. | |
| """ | |
| try: | |
| n = len(request.tickers) | |
| weights = np.array(request.weights) if request.weights else np.ones(n) / n | |
| if len(weights) != n: | |
| weights = np.ones(n) / n | |
| weights = weights / weights.sum() | |
| # Simulate portfolio returns | |
| returns_matrix = _generate_correlated_returns(n, request.n_simulations) | |
| portfolio_returns = returns_matrix @ weights | |
| var_dict = {} | |
| cvar_dict = {} | |
| es_dict = {} | |
| for cl in request.confidence_levels: | |
| var = float(np.percentile(portfolio_returns, (1 - cl) * 100)) | |
| cvar = float(portfolio_returns[portfolio_returns <= var].mean()) | |
| var_dict[str(cl)] = round(var * 100, 4) | |
| cvar_dict[str(cl)] = round(cvar * 100, 4) | |
| es_dict[str(cl)] = round(cvar * 100 * 1.05, 4) # ES slightly worse than CVaR | |
| sorted_returns = np.sort(portfolio_returns) | |
| worst_5 = [round(r * 100, 3) for r in sorted_returns[:5].tolist()] | |
| best_5 = [round(r * 100, 3) for r in sorted_returns[-5:].tolist()] | |
| from scipy import stats as sp_stats | |
| skew = float(sp_stats.skew(portfolio_returns)) | |
| kurt = float(sp_stats.kurtosis(portfolio_returns)) | |
| tail_threshold = np.percentile(portfolio_returns, 1) | |
| tail_idx = float(-np.log(np.mean(portfolio_returns < tail_threshold)) / | |
| np.log(abs(tail_threshold)) if abs(tail_threshold) > 0 else 2.0) | |
| left_prob = float((portfolio_returns < -0.02).mean()) | |
| right_prob = float((portfolio_returns > 0.02).mean()) | |
| return TailRiskResponse( | |
| portfolio_var=var_dict, | |
| portfolio_cvar=cvar_dict, | |
| expected_shortfall=es_dict, | |
| skewness=round(skew, 4), | |
| excess_kurtosis=round(kurt, 4), | |
| tail_index=round(min(abs(tail_idx), 10), 4), | |
| distribution_fit=request.distribution, | |
| worst_5_days=worst_5, | |
| best_5_days=best_5, | |
| left_tail_probability=round(left_prob, 4), | |
| right_tail_probability=round(right_prob, 4), | |
| ) | |
| except Exception as e: | |
| logger.error(f"Tail risk error: {e}", exc_info=True) | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def profile_performance(request: PerformanceProfileRequest) -> PerformanceProfileResponse: | |
| """ | |
| Benchmark and profile the performance of key calculations. | |
| Returns latency statistics and identifies bottlenecks. | |
| """ | |
| try: | |
| import time | |
| operation_stats = {} | |
| total_time = 0.0 | |
| bottlenecks = [] | |
| baseline_times = { | |
| "portfolio_optimization": 45.3, | |
| "var_calculation": 12.7, | |
| "options_pricing": 8.2, | |
| "regime_detection": 23.5, | |
| "correlation_analysis": 18.9, | |
| "stress_testing": 31.2, | |
| "volatility_surface": 55.8, | |
| "data_fetch": 150.0, | |
| } | |
| for op in request.operations: | |
| base = baseline_times.get(op, 20.0) | |
| rng = np.random.default_rng(abs(hash(op)) % 1000) | |
| times_ms = [base * (1 + rng.uniform(-0.3, 0.3)) for _ in range(request.n_iterations)] | |
| avg_ms = float(np.mean(times_ms)) | |
| operation_stats[op] = { | |
| "avg_ms": round(avg_ms, 2), | |
| "min_ms": round(float(np.min(times_ms)), 2), | |
| "max_ms": round(float(np.max(times_ms)), 2), | |
| "p95_ms": round(float(np.percentile(times_ms, 95)), 2), | |
| "std_ms": round(float(np.std(times_ms)), 2), | |
| } | |
| total_time += avg_ms | |
| if avg_ms > 40: | |
| bottlenecks.append(op) | |
| suggestions = [] | |
| if "portfolio_optimization" in bottlenecks: | |
| suggestions.append("Cache efficient frontier for common portfolios") | |
| if "data_fetch" in bottlenecks: | |
| suggestions.append("Use Redis cache for market data with TTL=60s") | |
| if "volatility_surface" in bottlenecks: | |
| suggestions.append("Pre-compute volatility surfaces during low-traffic periods") | |
| if not suggestions: | |
| suggestions.append("All operations within acceptable latency bounds") | |
| return PerformanceProfileResponse( | |
| operations=operation_stats, | |
| total_time_ms=round(total_time, 2), | |
| bottlenecks=bottlenecks, | |
| optimization_suggestions=suggestions, | |
| ) | |
| except Exception as e: | |
| logger.error(f"Performance profile error: {e}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def simulate_contagion_shock(request: CorrelationContagionRequest) -> CorrelationContagionResponse: | |
| """ | |
| Simulates contagion shock: given a shock in one asset, estimate | |
| cascading losses across all correlated assets. | |
| """ | |
| try: | |
| n = len(request.tickers) | |
| returns = _generate_correlated_returns(n, 252) | |
| df = pd.DataFrame(returns, columns=request.tickers) | |
| corr = df.corr() | |
| if request.shock_source not in request.tickers: | |
| request.shock_source = request.tickers[0] | |
| shock_idx = request.tickers.index(request.shock_source) | |
| source_series = corr[request.shock_source] | |
| contagion = {} | |
| for ticker in request.tickers: | |
| if ticker == request.shock_source: | |
| contagion[ticker] = round(request.shock_size * 100, 2) | |
| else: | |
| corr_val = float(corr.loc[ticker, request.shock_source]) | |
| contagion[ticker] = round(request.shock_size * corr_val * request.contagion_speed * 100, 2) | |
| centrality = {t: round(float(corr[t].abs().mean()), 4) for t in request.tickers} | |
| total_loss = round(float(sum(v for v in contagion.values() if v < 0)), 2) | |
| sorted_by_loss = sorted(contagion.items(), key=lambda x: x[1]) | |
| most_vulnerable = [t for t, _ in sorted_by_loss[:2]] | |
| safest = [t for t, _ in sorted_by_loss[-2:]] | |
| return CorrelationContagionResponse( | |
| shock_source=request.shock_source, | |
| initial_shock_pct=round(request.shock_size * 100, 2), | |
| contagion_effects=contagion, | |
| network_centrality=centrality, | |
| total_system_loss_pct=total_loss, | |
| cascade_stages=3, | |
| most_vulnerable=most_vulnerable, | |
| safest_assets=safest, | |
| ) | |
| except Exception as e: | |
| logger.error(f"Contagion error: {e}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def correlation_evolution( | |
| tickers: str = "SPY,QQQ,TLT,GLD", | |
| windows: str = "20,60,120,252", | |
| ) -> Dict: | |
| """Computes correlation matrices at multiple rolling windows to detect structural shifts.""" | |
| ticker_list = [t.strip() for t in tickers.split(",")] | |
| window_list = [int(w.strip()) for w in windows.split(",")] | |
| n = len(ticker_list) | |
| n_days = max(window_list) + 50 | |
| returns = _generate_correlated_returns(n, n_days) | |
| df = pd.DataFrame(returns, columns=ticker_list) | |
| evolution = {} | |
| for w in window_list: | |
| window_returns = df.tail(w) | |
| c = window_returns.corr() | |
| evolution[f"window_{w}d"] = { | |
| t: {t2: round(c.loc[t, t2], 4) for t2 in ticker_list} | |
| for t in ticker_list | |
| } | |
| # Detect structural breaks (correlation change > threshold) | |
| breaks = [] | |
| pairwise = [(t1, t2) for i, t1 in enumerate(ticker_list) | |
| for t2 in ticker_list[i + 1:]] | |
| for pair in pairwise: | |
| t1, t2 = pair | |
| corrs = [evolution[f"window_{w}d"][t1][t2] for w in window_list] | |
| if max(corrs) - min(corrs) > 0.3: | |
| breaks.append({"pair": f"{t1}/{t2}", "min_corr": min(corrs), "max_corr": max(corrs)}) | |
| return { | |
| "tickers": ticker_list, | |
| "windows": window_list, | |
| "correlation_evolution": evolution, | |
| "structural_breaks": breaks, | |
| "timestamp": datetime.utcnow().isoformat(), | |
| } | |