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 
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"""
Advanced Risk Analytics and Stress Testing Engine.

Comprehensive risk analysis tools:
- Scenario analysis (historical and hypothetical)
- Monte Carlo stress testing
- Correlation breakdown analysis
- Tail risk analysis  
- Risk factor attribution
- Extreme value theory (EVT)

Example
-------
>>> from quantlib_pro.risk.advanced_analytics import StressTester, ScenarioAnalyzer
>>>
>>> # Stress testing
>>> stress_tester = StressTester(portfolio_returns)
>>> results = stress_tester.run_monte_carlo_stress(n_scenarios=10000)
>>> print(results.summary())
>>>
>>> # Scenario analysis
>>> analyzer = ScenarioAnalyzer(portfolio)
>>> scenarios = analyzer.generate_historical_scenarios(['2008 Crisis', '2020 COVID'])
>>> impact = analyzer.analyze_scenario(scenarios['2008 Crisis'])
"""

from __future__ import annotations

import logging
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Dict, List, Optional, Tuple

import numpy as np
import pandas as pd
from scipy import stats

log = logging.getLogger(__name__)


class ScenarioType(Enum):
    """Types of stress scenarios."""
    HISTORICAL = 'historical'
    HYPOTHETICAL = 'hypothetical'
    MONTE_CARLO = 'monte_carlo'
    REGULATORY = 'regulatory'


@dataclass
class Scenario:
    """Stress test scenario definition."""
    name: str
    scenario_type: ScenarioType
    description: str
    market_shocks: Dict[str, float]  # asset -> return shock
    correlation_multiplier: float = 1.0  # Correlation intensification factor
    volatility_multiplier: float = 1.0  # Volatility scaling factor
    
    def apply_shock(self, returns: pd.Series) -> pd.Series:
        """Apply scenario shocks to returns."""
        shocked_returns = returns.copy()
        
        for asset, shock in self.market_shocks.items():
            if asset in shocked_returns.index:
                shocked_returns[asset] = shock
        
        return shocked_returns


@dataclass
class StressTestResult:
    """Results from stress testing."""
    scenario_name: str
    portfolio_loss: float
    portfolio_loss_pct: float
    var_95: float
    cvar_95: float
    max_drawdown: float
    sharpe_ratio: float
    asset_contributions: Dict[str, float]
    risk_metrics: Dict[str, float] = field(default_factory=dict)
    
    def summary(self) -> str:
        """Generate summary string."""
        return f"""
Stress Test Results: {self.scenario_name}
{'=' * 60}
Portfolio Loss:       ${self.portfolio_loss:,.2f} ({self.portfolio_loss_pct:.2%})
VaR (95%):           {self.var_95:.2%}
CVaR (95%):          {self.cvar_95:.2%}
Max Drawdown:        {self.max_drawdown:.2%}
Sharpe Ratio:        {self.sharpe_ratio:.2f}

Top Asset Contributions:
{self._format_contributions()}
"""
    
    def _format_contributions(self) -> str:
        """Format asset contributions."""
        sorted_contribs = sorted(
            self.asset_contributions.items(),
            key=lambda x: abs(x[1]),
            reverse=True
        )[:10]
        
        lines = []
        for asset, contrib in sorted_contribs:
            lines.append(f"  {asset:>10}: {contrib:>10.2%}")
        
        return '\n'.join(lines)


class StressTester:
    """
    Comprehensive stress testing engine.
    
    Performs various stress tests including historical scenarios,
    hypothetical shocks, and Monte Carlo simulations.
    
    Parameters
    ----------
    returns : pd.DataFrame
        Historical returns data (assets × time)
    weights : np.ndarray, optional
        Portfolio weights
    """
    
    def __init__(
        self,
        returns: pd.DataFrame,
        weights: Optional[np.ndarray] = None,
    ):
        self.returns = returns
        self.weights = weights if weights is not None else np.ones(len(returns.columns)) / len(returns.columns)
        
        # Calculate base statistics
        self.mean_returns = returns.mean()
        self.cov_matrix = returns.cov()
        self.std_returns = returns.std()
        self.correlation = returns.corr()
        
        log.info(f"Initialized StressTester with {len(returns.columns)} assets, {len(returns)} periods")
    
    def run_monte_carlo_stress(
        self,
        n_scenarios: int = 10000,
        stress_level: float = 2.0,
        correlation_breakdown: bool = True,
    ) -> StressTestResult:
        """
        Run Monte Carlo stress test.
        
        Parameters
        ----------
        n_scenarios : int
            Number of scenarios to simulate
        stress_level : float
            Stress intensity (standard deviations)
        correlation_breakdown : bool
            Whether to simulate correlation breakdown
        
        Returns
        -------
        StressTestResult
            Stress test results
        """
        log.info(f"Running Monte Carlo stress test: {n_scenarios} scenarios, stress={stress_level}σ")
        
        # Generate stressed scenarios
        if correlation_breakdown:
            # Reduce correlations during stress
            stressed_corr = self.correlation * 0.5
            stressed_cov = self._correlation_to_covariance(stressed_corr, self.std_returns)
        else:
            stressed_cov = self.cov_matrix
        
        # Generate scenarios
        scenarios = np.random.multivariate_normal(
            mean=-self.mean_returns * stress_level,  # Negative shock
            cov=stressed_cov * (stress_level ** 2),
            size=n_scenarios
        )
        
        # Calculate portfolio returns for each scenario
        portfolio_returns = scenarios @ self.weights
        
        # Calculate metrics
        portfolio_loss = np.min(portfolio_returns)
        portfolio_loss_pct = portfolio_loss
        
        var_95 = np.percentile(portfolio_returns, 5)
        cvar_95 = portfolio_returns[portfolio_returns <= var_95].mean()
        
        # Max drawdown
        cumulative = (1 + portfolio_returns).cumprod()
        running_max = np.maximum.accumulate(cumulative)
        drawdown = (cumulative - running_max) / running_max
        max_drawdown = np.min(drawdown)
        
        # Sharpe ratio
        sharpe = portfolio_returns.mean() / portfolio_returns.std() * np.sqrt(252) if portfolio_returns.std() > 0 else 0
        
        # Asset contributions (average contribution to portfolio variance)
        asset_contributions = self._calculate_risk_contributions(scenarios)
        
        return StressTestResult(
            scenario_name=f"Monte Carlo Stress ({stress_level}σ, N={n_scenarios})",
            portfolio_loss=portfolio_loss,
            portfolio_loss_pct=portfolio_loss_pct,
            var_95=var_95,
            cvar_95=cvar_95,
            max_drawdown=max_drawdown,
            sharpe_ratio=sharpe,
            asset_contributions=asset_contributions,
        )
    
    def run_historical_scenario(
        self,
        scenario_name: str,
        start_date: str,
        end_date: str,
    ) -> StressTestResult:
        """
        Run historical scenario analysis.
        
        Parameters
        ----------
        scenario_name : str
            Name of historical scenario
        start_date : str
            Scenario start date
        end_date : str
            Scenario end date
        
        Returns
        -------
        StressTestResult
            Stress test results
        """
        log.info(f"Running historical scenario: {scenario_name} ({start_date} to {end_date})")
        
        # Filter historical returns for scenario period
        scenario_returns = self.returns.loc[start_date:end_date]
        
        if scenario_returns.empty:
            log.warning(f"No data found for scenario period")
            # Return empty result
            return self._empty_result(scenario_name)
        
        # Calculate portfolio returns
        portfolio_returns = (scenario_returns @ self.weights).values
        
        # Calculate metrics
        portfolio_loss = np.min(portfolio_returns)
        portfolio_loss_pct = portfolio_loss
        
        var_95 = np.percentile(portfolio_returns, 5)
        cvar_95 = portfolio_returns[portfolio_returns <= var_95].mean()
        
        # Max drawdown
        cumulative = (1 + portfolio_returns).cumprod()
        running_max = np.maximum.accumulate(cumulative)
        drawdown = (cumulative - running_max) / running_max
        max_drawdown = np.min(drawdown)
        
        # Sharpe ratio
        sharpe = portfolio_returns.mean() / portfolio_returns.std() * np.sqrt(252) if portfolio_returns.std() > 0 else 0
        
        # Asset contributions
        asset_contributions = self._calculate_risk_contributions(scenario_returns.values)
        
        return StressTestResult(
            scenario_name=f"Historical: {scenario_name}",
            portfolio_loss=portfolio_loss,
            portfolio_loss_pct=portfolio_loss_pct,
            var_95=var_95,
            cvar_95=cvar_95,
            max_drawdown=max_drawdown,
            sharpe_ratio=sharpe,
            asset_contributions=asset_contributions,
        )
    
    def run_hypothetical_scenario(self, scenario: Scenario) -> StressTestResult:
        """
        Run hypothetical scenario.
        
        Parameters
        ----------
        scenario : Scenario
            Scenario definition
        
        Returns
        -------
        StressTestResult
            Stress test results
        """
        log.info(f"Running hypothetical scenario: {scenario.name}")
        
        # Apply shocks to mean returns
        shocked_returns = self.mean_returns.copy()
        for asset, shock in scenario.market_shocks.items():
            if asset in shocked_returns.index:
                shocked_returns[asset] = shock
        
        # Apply correlation and volatility multipliers
        stressed_cov = self.cov_matrix * (scenario.volatility_multiplier ** 2)
        
        if scenario.correlation_multiplier != 1.0:
            stressed_corr = self.correlation * scenario.correlation_multiplier
            # Clip to valid correlation range
            stressed_corr = np.clip(stressed_corr, -1, 1)
            stressed_cov = self._correlation_to_covariance(stressed_corr, self.std_returns * scenario.volatility_multiplier)
        
        # Simulate scenarios
        n_scenarios = 1000
        scenarios = np.random.multivariate_normal(
            mean=shocked_returns.values,
            cov=stressed_cov.values,
            size=n_scenarios
        )
        
        # Calculate portfolio returns
        portfolio_returns = scenarios @ self.weights
        
        # Calculate metrics
        portfolio_loss = np.min(portfolio_returns)
        portfolio_loss_pct = portfolio_loss
        
        var_95 = np.percentile(portfolio_returns, 5)
        cvar_95 = portfolio_returns[portfolio_returns <= var_95].mean()
        
        # Max drawdown
        cumulative = (1 + portfolio_returns).cumprod()
        running_max = np.maximum.accumulate(cumulative)
        drawdown = (cumulative - running_max) / running_max
        max_drawdown = np.min(drawdown)
        
        # Sharpe ratio
        sharpe = portfolio_returns.mean() / portfolio_returns.std() * np.sqrt(252) if portfolio_returns.std() > 0 else 0
        
        # Asset contributions
        asset_contributions = self._calculate_risk_contributions(scenarios)
        
        return StressTestResult(
            scenario_name=f"Hypothetical: {scenario.name}",
            portfolio_loss=portfolio_loss,
            portfolio_loss_pct=portfolio_loss_pct,
            var_95=var_95,
            cvar_95=cvar_95,
            max_drawdown=max_drawdown,
            sharpe_ratio=sharpe,
            asset_contributions=asset_contributions,
        )
    
    def _calculate_risk_contributions(self, scenarios: np.ndarray) -> Dict[str, float]:
        """Calculate risk contributions for each asset."""
        contributions = {}
        
        # Calculate marginal contribution to risk
        portfolio_var = np.var(scenarios @ self.weights)
        
        for i, asset in enumerate(self.returns.columns):
            # Contribution = weight × (covariance with portfolio / portfolio variance)
            asset_returns = scenarios[:, i]
            portfolio_returns = scenarios @ self.weights
            
            cov = np.cov(asset_returns, portfolio_returns)[0, 1]
            contribution = self.weights[i] * cov / portfolio_var if portfolio_var > 0 else 0
            
            contributions[asset] = contribution
        
        return contributions
    
    def _correlation_to_covariance(
        self,
        correlation: pd.DataFrame,
        std: pd.Series,
    ) -> pd.DataFrame:
        """Convert correlation matrix to covariance matrix."""
        std_matrix = np.outer(std.values, std.values)
        cov = correlation.values * std_matrix
        return pd.DataFrame(cov, index=correlation.index, columns=correlation.columns)
    
    def _empty_result(self, name: str) -> StressTestResult:
        """Create empty result."""
        return StressTestResult(
            scenario_name=name,
            portfolio_loss=0.0,
            portfolio_loss_pct=0.0,
            var_95=0.0,
            cvar_95=0.0,
            max_drawdown=0.0,
            sharpe_ratio=0.0,
            asset_contributions={},
        )


class TailRiskAnalyzer:
    """
    Extreme value theory (EVT) tail risk analysis.
    
    Analyzes tail risk using:
    - Peaks Over Threshold (POT) method
    - Generalized Pareto Distribution (GPD)
    - Hill estimator for tail index
    """
    
    def __init__(self, returns: pd.Series):
        self.returns = returns
        self.negative_returns = -returns[returns < 0]
        
        log.info("Initialized TailRiskAnalyzer")
    
    def estimate_tail_index(self, threshold_percentile: float = 0.90) -> float:
        """
        Estimate tail index using Hill estimator.
        
        Parameters
        ----------
        threshold_percentile : float
            Percentile threshold for tail (e.g., 0.90 = top 10%)
        
        Returns
        -------
        float
            Tail index estimate
        """
        threshold = self.negative_returns.quantile(threshold_percentile)
        exceedances = self.negative_returns[self.negative_returns > threshold]
        
        if len(exceedances) == 0:
            return 0.0
        
        # Hill estimator
        log_exceedances = np.log(exceedances / threshold)
        tail_index = 1 / log_exceedances.mean()
        
        return tail_index
    
    def fit_gpd(self, threshold_percentile: float = 0.90) -> Tuple[float, float]:
        """
        Fit Generalized Pareto Distribution to tail.
        
        Parameters
        ----------
        threshold_percentile : float
            Percentile threshold
        
        Returns
        -------
        tuple of (shape, scale)
            GPD parameters
        """
        threshold = self.negative_returns.quantile(threshold_percentile)
        exceedances = self.negative_returns[self.negative_returns > threshold] - threshold
        
        if len(exceedances) < 10:
            return (0.0, 0.0)
        
        # Fit GPD using scipy
        shape, loc, scale = stats.genpareto.fit(exceedances, floc=0)
        
        return (shape, scale)
    
    def calculate_extreme_var(
        self,
        confidence: float = 0.99,
        threshold_percentile: float = 0.90,
    ) -> float:
        """
        Calculate VaR using EVT.
        
        Parameters
        ----------
        confidence : float
            Confidence level for VaR
        threshold_percentile : float
            Threshold for tail definition
        
        Returns
        -------
        float
            Extreme VaR estimate
        """
        threshold = self.negative_returns.quantile(threshold_percentile)
        shape, scale = self.fit_gpd(threshold_percentile)
        
        if scale == 0:
            # Fallback to empirical quantile
            return self.negative_returns.quantile(confidence)
        
        # Calculate extreme VaR using GPD
        n = len(self.negative_returns)
        n_exceedances = len(self.negative_returns[self.negative_returns > threshold])
        
        q = (n / n_exceedances) * (1 - confidence)
        
        if shape != 0:
            extreme_var = threshold + (scale / shape) * (q ** (-shape) - 1)
        else:
            extreme_var = threshold - scale * np.log(q)
        
        return extreme_var