Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/risk/stressed_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,30 @@ def stressed_var_window(pnl_series, window=250, alpha=0.99, stress_window=60):
raise ValueError("pnl_series must be longer than the VaR and stress windows")

full_var, full_es = historical_var_es(pnl, alpha=alpha)
best = None
for end_idx in range(stress_window, len(pnl) + 1):
start_idx = end_idx - stress_window
sample = pnl.iloc[start_idx:end_idx]
win_var, win_es = historical_var_es(sample, alpha=alpha)
if best is None or win_var > best['stressed_var']:
best = {
'selected_start_index': start_idx,
'selected_end_index': end_idx - 1,
'selected_window_observations': int(len(sample)),
'selected_window_pnl_volatility': float(sample.std(ddof=1)),
'stressed_var': float(win_var),
'stressed_es': float(win_es),
}
losses = -pnl.to_numpy()
start_index = 0
stressed_var = float(np.quantile(losses[:stress_window], alpha))
for start in range(1, len(pnl) - stress_window + 1):
window_var = float(np.quantile(losses[start:start + stress_window], alpha))
if window_var > stressed_var:
stressed_var = window_var
start_index = start

selected = pnl.iloc[start_index:start_index + stress_window]
selected_losses = losses[start_index:start_index + stress_window]
tail = selected_losses[selected_losses >= stressed_var]
stressed_es = float(tail.mean()) if len(tail) else stressed_var
full_vol = float(pnl.std(ddof=1))
stressed_var = best['stressed_var']
stressed_es = best['stressed_es']
multiplier = float(stressed_var / full_var) if full_var else np.nan
status = 'REVIEW' if multiplier > 1.25 else 'PASS'
return pd.DataFrame([{
'method': 'max_rolling_var_stressed_window_proxy',
'alpha': alpha,
'stress_window_days': stress_window,
'selected_start_index': best['selected_start_index'],
'selected_end_index': best['selected_end_index'],
'selected_window_observations': best['selected_window_observations'],
'selected_window_pnl_volatility': best['selected_window_pnl_volatility'],
'selected_start_index': start_index,
'selected_end_index': start_index + stress_window - 1,
'selected_window_observations': int(len(selected)),
'selected_window_pnl_volatility': float(selected.std(ddof=1)),
'full_sample_pnl_volatility': full_vol,
'full_sample_var': float(full_var),
'full_sample_es': float(full_es),
Expand Down
Loading