Spaces:
Sleeping
Sleeping
Download backtesting/coverage.py from MinsaitEU/VaR_model: direct link, hf CLI and curl.
- Browser
- Download file 4.29 kB
-
https://huggingface.co/spaces/MinsaitEU/VaR_model/resolve/main/backtesting/coverage.py
- Command line
-
hf download hf://spaces/MinsaitEU/VaR_model/backtesting/coverage.py
-
curl -L -o coverage.py https://huggingface.co/spaces/MinsaitEU/VaR_model/resolve/main/backtesting/coverage.py
4.29 kB
| """ | |
| Statistical tests on VaR exceedances. | |
| - Kupiec (1995) proportion of failures: is the exceedance | |
| rate consistent with 1 - confidence? | |
| - Christoffersen (1998) independence: do exceedances cluster? | |
| - Conditional coverage: both at once. | |
| - Basel traffic light: zone from the binomial probability of | |
| observing at most that many exceedances. | |
| """ | |
| import numpy as np | |
| import pandas as pd | |
| from scipy.special import xlogy | |
| from scipy.stats import binom, chi2 | |
| from data.schema import Out | |
| # Basel traffic-light thresholds on the cumulative binomial | |
| # probability of the observed exceedance count. | |
| GREEN_LIMIT = 0.95 | |
| YELLOW_LIMIT = 0.9999 | |
| def _bernoulli_loglik(failures, successes, p): | |
| """ | |
| log L = x ln p + (n - x) ln(1 - p), with 0 ln 0 = 0. | |
| """ | |
| return xlogy(failures, p) + xlogy(successes, 1 - p) | |
| def kupiec_test(exceedances, observations, confidence): | |
| p = 1 - confidence | |
| x = exceedances | |
| n = observations | |
| lr = -2 * ( | |
| _bernoulli_loglik(x, n - x, p) | |
| - _bernoulli_loglik(x, n - x, x / n) | |
| ) | |
| return float(lr), float(chi2.sf(lr, df=1)) | |
| def christoffersen_independence_test(hits): | |
| """ | |
| Likelihood-ratio test of a first-order Markov chain | |
| against independent exceedances. | |
| """ | |
| hits = np.asarray(hits, dtype=int) | |
| previous, current = hits[:-1], hits[1:] | |
| n00 = np.sum((previous == 0) & (current == 0)) | |
| n01 = np.sum((previous == 0) & (current == 1)) | |
| n10 = np.sum((previous == 1) & (current == 0)) | |
| n11 = np.sum((previous == 1) & (current == 1)) | |
| pi01 = n01 / (n00 + n01) if n00 + n01 else 0.0 | |
| pi11 = n11 / (n10 + n11) if n10 + n11 else 0.0 | |
| pi = (n01 + n11) / len(current) | |
| lr = -2 * ( | |
| _bernoulli_loglik(n01 + n11, n00 + n10, pi) | |
| - _bernoulli_loglik(n01, n00, pi01) | |
| - _bernoulli_loglik(n11, n10, pi11) | |
| ) | |
| lr = max(float(lr), 0.0) | |
| return lr, float(chi2.sf(lr, df=1)) | |
| def traffic_light(exceedances, observations, confidence): | |
| probability = binom.cdf( | |
| exceedances, observations, 1 - confidence | |
| ) | |
| if probability < GREEN_LIMIT: | |
| return "Green" | |
| if probability < YELLOW_LIMIT: | |
| return "Yellow" | |
| return "Red" | |
| def backtest_statistics( | |
| forecasts: pd.DataFrame, | |
| significance: float, | |
| ) -> pd.DataFrame: | |
| """ | |
| One row of coverage statistics per confidence level. | |
| """ | |
| rows = [] | |
| for confidence, group in forecasts.groupby(Out.CONFIDENCE): | |
| hits = group[Out.EXCEEDANCE].to_numpy() | |
| n = len(hits) | |
| x = int(hits.sum()) | |
| kupiec_lr, kupiec_p = kupiec_test(x, n, confidence) | |
| ind_lr, ind_p = christoffersen_independence_test(hits) | |
| cc_lr = kupiec_lr + ind_lr | |
| cc_p = float(chi2.sf(cc_lr, df=2)) | |
| # Severity: realised loss over predicted ES on exceedance | |
| # days. Around 1 means ES is well calibrated. | |
| exceeded = group[group[Out.EXCEEDANCE]] | |
| es_ratio = ( | |
| float( | |
| exceeded[Out.REALIZED_LOSS].mean() | |
| / exceeded[Out.ES].mean() | |
| ) | |
| if x | |
| else np.nan | |
| ) | |
| if kupiec_p < significance: | |
| verdict = ( | |
| "Rejected: too many exceedances" | |
| if x > n * (1 - confidence) | |
| else "Rejected: too few exceedances (overly conservative)" | |
| ) | |
| elif ind_p < significance: | |
| verdict = "Rejected: exceedances cluster" | |
| elif cc_p < significance: | |
| verdict = "Rejected: conditional coverage" | |
| else: | |
| verdict = "Not rejected" | |
| rows.append( | |
| { | |
| Out.CONFIDENCE: confidence, | |
| Out.OBSERVATIONS: n, | |
| Out.EXPECTED_EXCEEDANCES: n * (1 - confidence), | |
| Out.EXCEEDANCES: x, | |
| Out.EXCEEDANCE_RATE: x / n, | |
| Out.KUPIEC_LR: kupiec_lr, | |
| Out.KUPIEC_P: kupiec_p, | |
| Out.INDEPENDENCE_LR: ind_lr, | |
| Out.INDEPENDENCE_P: ind_p, | |
| Out.CONDITIONAL_LR: cc_lr, | |
| Out.CONDITIONAL_P: cc_p, | |
| Out.ZONE: traffic_light(x, n, confidence), | |
| Out.ES_RATIO: es_ratio, | |
| Out.VERDICT: verdict, | |
| } | |
| ) | |
| return pd.DataFrame(rows) | |