.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/risk/backtesting/plot_01_kupiec.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_risk_backtesting_plot_01_kupiec.py: Kupiec's proportion-of-failures test ==================================== A 99% VaR should be exceeded on about 1% of days. Kupiec (1995) turned "about" into a likelihood-ratio test: with :math:`x` exceptions in :math:`n` days and :math:`\hat p = x/n`, .. math:: LR_{uc} = -2 \ln \frac{(1 - p)^{n - x} p^x}{(1 - \hat p)^{n - x} \hat p^x} \sim \chi^2_1. He also showed how weak the test is on a year of data: a model that is badly wrong is often not rejected. .. GENERATED FROM PYTHON SOURCE LINES 18-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.risk import kupiec_test, parametric_var, var_exceptions .. GENERATED FROM PYTHON SOURCE LINES 24-26 Non-rejection regions --------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python for n in (250, 500, 1000): accepted = [x for x in range(60) if not kupiec_test(np.arange(n) < x, 0.99).reject()] print(f"n = {n:4d}: a 99% VaR passes with {accepted[0]} to {accepted[-1]} exceptions (expected {n // 100})") .. rst-class:: sphx-glr-script-out .. code-block:: none n = 250: a 99% VaR passes with 1 to 6 exceptions (expected 2) n = 500: a 99% VaR passes with 2 to 9 exceptions (expected 5) n = 1000: a 99% VaR passes with 5 to 16 exceptions (expected 10) .. GENERATED FROM PYTHON SOURCE LINES 32-38 Testing a model that underestimates volatility ---------------------------------------------- Returns have a 1.2% daily volatility; the model assumes 1%. With 250 days the test catches the error only about half the time; with four years, almost always. .. GENERATED FROM PYTHON SOURCE LINES 38-60 .. code-block:: Python rng = np.random.default_rng(0) var = parametric_var(0.0, 0.01, 0.99) power = {} for n in (250, 1000): pnl = 0.012 * rng.standard_normal((1000, n)) power[n] = np.mean([kupiec_test(var_exceptions(row, var)).reject() for row in pnl]) print(f"n = {n:4d}: rejection rate {power[n]:.0%}") pnl = 0.012 * rng.standard_normal(1000) res = kupiec_test(var_exceptions(pnl, var)) print(f"one sample: {res.n_exceptions} exceptions vs {res.expected:.0f} expected, LR = {res.statistic:.2f}, p = {res.p_value:.4f}") x = np.arange(0, 25) fig, ax = plt.subplots(figsize=(7, 3.5)) ax.plot(x, [kupiec_test(np.arange(1000) < k).statistic for k in x], ".-", label="LR statistic, n = 1000") ax.axhline(3.841, color="C3", ls="--", label="5% critical value") ax.set_xlabel("number of exceptions") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/risk/backtesting/images/sphx_glr_plot_01_kupiec_001.png :alt: plot 01 kupiec :srcset: /api/gallery/risk/backtesting/images/sphx_glr_plot_01_kupiec_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none n = 250: rejection rate 48% n = 1000: rejection rate 97% one sample: 28 exceptions vs 10 expected, LR = 21.99, p = 0.0000 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.553 seconds) .. _sphx_glr_download_api_gallery_risk_backtesting_plot_01_kupiec.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/risk/backtesting/plot_01_kupiec.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_kupiec.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_kupiec.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_kupiec.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_