Utilities¶
Utilities that do not fit well on other pages.
Test Results¶
-
class arch.utility.testing.WaldTestStatistic(stat: float, df: int, null: str, alternative: str, name: str =
'')[source]¶ Test statistic holder for Wald-type tests.
- Parameters:¶
- stat: float¶
The test statistic. Must not be negative. NaN is accepted and gives a NaN p-value, for a statistic that cannot be computed.
- df: int¶
Degree of freedom. Must be positive.
- null: str¶
A statement of the test’s null hypothesis.
- alternative: str¶
A statement of the test’s alternative hypothesis.
- name: str =
''¶ Name of test.
- Raises:¶
TypeError – If stat is not a real number, df is not an integer, or null, alternative or name is not a str.
ValueError – If stat is negative or df is not positive.
Residual Diagnostics¶
If a model is correctly specified, the standardized residuals \(\hat{e}_t = \hat{\epsilon}_t / \hat{\sigma}_t\) behave like draws from the distribution of the model, and so the tails of the residuals can be compared to the tails of the distribution that was assumed. The functions below summarize the tails of standardized residuals, and are also available for the results of a model, which use the standardized residuals and the estimated distribution of the model.
excess_kurtosis()is the sample excess kurtosis, the fourth central moment divided by the squared variance, less 3. It is 0 for a Normal, and is positive when the residuals have heavier tails than a Normal. Observations that are not finite are ignored.hill_estimator()is the Hill estimator of the tail index, using theklargest absolute residuals, andint(sqrt(n))of them ifkis not provided. The estimator is \(\hat{\nu} = \left(k^{-1}\sum_{i=1}^{k} \ln X_{(n-i+1)} - \ln X_{(n-k)}\right)^{-1}\), where \(X_{(j)}\) is the \(j\)-th smallest absolute residual. Smaller values indicate heavier tails. The tail index of a Student’s t distribution is its degrees of freedom.var_ratio()is the ratio of the Value-at-Risk (VaR) of a Normal to the VaR of the distribution of the model. It is the 99.9% VaR unlessp, the probability of an exception, is provided. A ratio below 1 means that the Normal understates the VaR of the model’s distribution. It depends on the distribution and its parameters only, and not on residuals.
The results of a model have the same diagnostics. They use the standardized
residuals, std_resid, and, for var_ratio, the estimated distribution and
parameters.
In [1]: import arch.data.sp500
In [2]: from arch.univariate import arch_model
In [3]: sp500 = arch.data.sp500.load()
In [4]: returns = 100 * sp500["Adj Close"].pct_change().dropna()
In [5]: res = arch_model(returns, dist="t").fit(disp="off")
In [6]: res.excess_kurtosis
Out[6]: 2.0564958812639436
In [7]: res.hill_estimator()
Out[7]: 5.044399457880951
In [8]: res.var_ratio()
Out[8]: 0.7506415839753905
The functions can also be used with any standardized residuals, and with a distribution that is not estimated.
In [9]: from arch.univariate import (
...: StudentsT,
...: excess_kurtosis,
...: hill_estimator,
...: var_ratio,
...: )
...:
In [10]: excess_kurtosis(res.std_resid)
Out[10]: 2.0564958812639436
In [11]: hill_estimator(res.std_resid, k=50)
Out[11]: 4.637081386972821
# The 99% VaR, p=0.01
In [12]: var_ratio(0.01, StudentsT(), [6.0])
Out[12]: 0.9066125541022965
|
Sample excess kurtosis of standardized residuals. |
|
Hill tail-index estimator of standardized residuals. |
|
Ratio of Gaussian VaR to model-distribution VaR. |
The corresponding members of the results classes are
excess_kurtosis,
hill_estimator() and
var_ratio().
Backtesting Value-at-Risk¶
The functions below compare a series of Value-at-Risk (VaR) forecasts with the returns
that were realized. VaR is reported as a positive loss (a 99% VaR of 2.5 is the loss
that is exceeded with probability 1%), and an observation is an exception if the loss
exceeds the VaR, \(-r_t > \mathrm{VaR}_t\). VaR is named by its confidence level, and
p is always the probability of an exception, the size of the tail: p=0.05 for a
95% VaR and p=0.01 for a 99% VaR. It must be in (0, 0.5], so that a confidence level
such as 0.99 raises an error. p is 0.05 by default, except in the Basel traffic
light, whose default is the 99% VaR of the framework. A quantile of the returns,
which is negative in the lower tail, must be negated first. The returns and the VaR must
be finite.
If both are Series they are aligned on the index, and the observations that they have in
common are used. Otherwise they must have the same length and are matched by position. The
forecasts of forecast() are indexed by the
date of the forecast origin by default, so the one-step forecast for day \(t\) has the
label of day \(t-1\). Use forecast(..., align="target"), or shift the VaR forward
by one observation, so that each VaR has the label of the return that it forecasts.
var_exceptions()is the bool array of exceptions, or a Series if the inputs are pandas objects.kupiec_pof()is the proportion-of-failures likelihood-ratio test of Kupiec (1995) that the probability of an exception isp. It is \(\chi^2_1\), and it is two-sided, so that too few exceptions also reject.christoffersen_independence()is the likelihood-ratio test of Christoffersen (1998) that the exceptions are independent, against a first-order Markov chain. It is \(\chi^2_1\).christoffersen_conditional_coverage()is the sum of the two statistics, which tests the coverage and the independence jointly. It is \(\chi^2_2\).traffic_light()is the zone of the framework of the Basel Committee on Banking Supervision (1996) for the exceptions in the lastwindowobservations (250 by default). The zone is green if the cumulative binomial probability of the number of exceptions is below 95%, yellow if it is at least 95% and below 99.99%, and red otherwise. For 250 observations andp=0.01green is 0 to 4 exceptions, yellow is 5 to 9 and red is 10 or more, as in Table 2 of the framework. It returns aTrafficLightResultwith the zone, the number of exceptions, the cumulative probability and the probability of a type 1 error, which is the probability of that number of exceptions or more. A series that is shorter thanwindowraises an error.
The three tests return a WaldTestStatistic. The
likelihood-ratio statistics are computed with \(0\ln 0=0\), so that a series with no
exception, or with only exceptions, has a statistic.
In [13]: from arch.univariate import (
....: christoffersen_conditional_coverage,
....: christoffersen_independence,
....: kupiec_pof,
....: traffic_light,
....: var_exceptions,
....: )
....:
# A 99% VaR (p=0.01) from the model with Student's t errors, as a positive loss
In [14]: nu = res.params[["nu"]].to_numpy()
In [15]: quantile = float(res.model.distribution.ppf(0.01, nu))
In [16]: var = -(res.params["mu"] + res.conditional_volatility * quantile)
In [17]: exceptions = var_exceptions(returns, var)
In [18]: exceptions.sum()
Out[18]: np.int64(67)
In [19]: kupiec_pof(returns, var, 0.01)
Out[19]:
Kupiec Proportion of Failures Test
H0: The probability of an exception is 0.01.
H1: The probability of an exception is not 0.01.
Statistic: 5.0722
P-value: 0.0243
Distributed: chi2(1)
WaldTestStatistic, id: 0x731f628c7b60
In [20]: christoffersen_independence(returns, var).pval
Out[20]: np.float64(0.07385551771640182)
In [21]: christoffersen_conditional_coverage(returns, var, 0.01).pval
Out[21]: np.float64(0.016023630716761238)
In [22]: traffic_light(returns, var, p=0.01)
Out[22]: TrafficLightResult(zone='yellow', exceptions=7, probability=0.9959746612881922, type1_error=0.013701447855203663, nobs=250, p=0.01)
|
Identify the exceptions of a VaR forecast. |
|
Kupiec proportion-of-failures test of the coverage of VaR. |
|
Christoffersen test of the independence of the exceptions of VaR. |
|
Christoffersen test of the conditional coverage of VaR. |
|
Basel traffic light zone of the exceptions of VaR. |
- class arch.univariate.TrafficLightResult(zone: str, exceptions: int, probability: float, type1_error: float, nobs: int, p: float)[source]¶
Result of the Basel traffic light backtest of VaR.
- Parameters:¶
- zone: str¶
The Basel zone of the number of exceptions.
- exceptions: int¶
The number of exceptions in the window.
- probability: float¶
The cumulative binomial probability of
exceptionsor fewer exceptions if the probability of an exception isp.- type1_error: float¶
The binomial probability of
exceptionsor more exceptions if the probability of an exception isp. It is the probability of rejecting an accurate model if a model with this number of exceptions is rejected, the type 1 error of Table 1 of the Basel framework.- nobs: int¶
The number of observations in the window.
- p: float¶
The probability of an exception that the VaR is designed for, 0.01 for a 99% VaR.
- probability¶
The cumulative binomial probability of
exceptionsor fewer exceptions if the probability of an exception isp.
- type1_error¶
The binomial probability of
exceptionsor more exceptions if the probability of an exception isp. It is the probability of rejecting an accurate model if a model with this number of exceptions is rejected, the type 1 error of Table 1 of the Basel framework.