VaR_model / backtesting /engine.py
MinsaitEU's picture
Claude Opus 5.5
Load VaR inputs from the Coral zip; add backtesting and stress tests
cbdae8d
Raw History Blame Contribute Delete
3.39 kB
"""
Rolling out-of-sample VaR forecasts.
For every day t after the first `min_window` returns, the model
is re-estimated with returns strictly before t, a one-day VaR
and ES are simulated, and the forecast is compared with the
realised P&L of day t.
The portfolio is held constant at today's positions
(hypothetical P&L backtesting): the test isolates the risk
model from changes in the book.
"""
import numpy as np
import pandas as pd
from data.schema import Out
from covariance.calculations import (
calculate_covariance,
ensure_psd,
)
from simulation.gaussian import (
simulate_correlated_shocks,
shocks_to_prices,
)
from portfolio.revaluation import position_pnl
from var.metrics import calculate_var_metrics
def rolling_var_forecasts(
prices,
returns,
portfolio,
return_methods,
confidence_levels,
covariance_method,
ewma_lambda,
lookback,
min_window,
simulations,
seed,
):
"""
Returns
-------
pd.DataFrame
One row per (date, confidence) with the forecast VaR
and ES, the realised P&L and loss, and whether the
loss exceeded VaR.
"""
if len(returns) <= min_window:
raise ValueError(
f"Backtesting needs more than {min_window} returns; "
f"only {len(returns)} available."
)
factors = list(returns.columns)
mean = np.zeros(len(factors))
rows = []
for i in range(min_window, len(returns)):
date = returns.index[i]
window = returns.iloc[:i]
if lookback:
window = window.tail(lookback)
covariance, _ = ensure_psd(
calculate_covariance(
window,
method=covariance_method,
ewma_lambda=ewma_lambda,
)
)
shocks = simulate_correlated_shocks(
mean=mean,
cholesky_matrix=np.linalg.cholesky(
covariance.to_numpy()
),
n_simulations=simulations,
seed=seed + i,
)
# Prices the forecast is made from: previous quote.
previous = prices.index[prices.index.get_loc(date) - 1]
current_prices = prices.loc[previous, factors]
scenario_prices = shocks_to_prices(
current_prices=current_prices.to_numpy(),
shocks=shocks,
columns=factors,
return_methods=return_methods,
)
simulated_losses = -position_pnl(
current_prices,
scenario_prices,
portfolio,
).sum(axis=1).to_numpy()
realized_pnl = float(
position_pnl(
current_prices,
prices.loc[[date], factors].to_numpy(),
portfolio,
).sum(axis=1).iloc[0]
)
for metric in calculate_var_metrics(
simulated_losses,
confidence_levels,
):
rows.append(
{
Out.DATE: date,
Out.CONFIDENCE: metric[Out.CONFIDENCE],
Out.VAR: metric[Out.VAR],
Out.ES: metric[Out.ES],
Out.REALIZED_PNL: realized_pnl,
Out.REALIZED_LOSS: -realized_pnl,
Out.EXCEEDANCE: -realized_pnl > metric[Out.VAR],
}
)
return pd.DataFrame(rows)