File size: 3,393 Bytes
cbdae8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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)