File size: 4,290 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
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)