.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/risk/expected_shortfall/plot_03_acerbi_tasche.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_expected_shortfall_plot_03_acerbi_tasche.py: Acerbi-Tasche expected shortfall ================================ "The average loss beyond VaR" sounds like one number but is several when the distribution has atoms, as every sample does, and some versions are not coherent. Acerbi and Tasche (2002) gave the definition that is: average exactly the worst fraction :math:`p = 1 - \alpha` of outcomes, charging only part of the atom at the quantile, .. math:: \mathrm{ES} = -\frac{1}{np}\Big(\sum_{i=1}^{\lfloor np \rfloor} X_{(i)} + (np - \lfloor np \rfloor) X_{(\lfloor np \rfloor + 1)}\Big). In 2016 the Basel Committee's Fundamental Review of the Trading Book replaced 99% VaR by 97.5% ES. .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.risk import expected_shortfall, historical_var, parametric_es, parametric_var .. GENERATED FROM PYTHON SOURCE LINES 27-33 A discrete example ------------------ With 100 outcomes and a 2.5% tail, the estimator takes the two worst in full and half of the third. The naive "mean of losses at or beyond VaR" would average three. .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: Python x = -np.arange(1.0, 101.0) print(f"VaR {historical_var(x, 0.975).var}, Acerbi-Tasche ES {expected_shortfall(x, 0.975)}, naive tail mean {-np.mean(np.sort(x)[:3]):.1f}") .. rst-class:: sphx-glr-script-out .. code-block:: none VaR 98.0, Acerbi-Tasche ES 99.2, naive tail mean 99.0 .. GENERATED FROM PYTHON SOURCE LINES 38-44 Convergence to the closed form ------------------------------ For normal and Student-t profit and loss, the sample estimator converges to the closed forms, and the Basel 97.5% ES is close to the old 99% VaR for the normal but larger under fat tails. .. GENERATED FROM PYTHON SOURCE LINES 44-66 .. code-block:: Python rng = np.random.default_rng(0) nu = 4 sizes = np.geomspace(250, 100_000, 12).astype(int) fig, ax = plt.subplots(figsize=(7, 4)) for name, draw, exact in [ ("normal", lambda k: rng.standard_normal(k), parametric_es(0.0, 1.0, 0.975)), ("Student-t(4)", lambda k: rng.standard_t(nu, k) / np.sqrt(nu / (nu - 2)), parametric_es(0.0, 1.0, 0.975, distribution="t", df=nu)), ]: est = np.array([[expected_shortfall(draw(k), 0.975) for _ in range(20)] for k in sizes]) line = ax.errorbar(sizes, est.mean(axis=1), yerr=est.std(axis=1), fmt="o-", ms=3, capsize=2, label=f"{name} sample ES") ax.axhline(exact, color=line[0].get_color(), ls="--", lw=1) ax.set_xscale("log") ax.set_xlabel("sample size") ax.set_ylabel("97.5% expected shortfall") ax.legend() fig.tight_layout() for name, kw in [("normal", {}), ("t(4)", {"distribution": "t", "df": nu})]: print(f"{name:7s} 97.5% ES {parametric_es(0.0, 1.0, 0.975, **kw):.3f} vs 99% VaR {parametric_var(0.0, 1.0, 0.99, **kw):.3f}") plt.show() .. image-sg:: /api/gallery/risk/expected_shortfall/images/sphx_glr_plot_03_acerbi_tasche_001.png :alt: plot 03 acerbi tasche :srcset: /api/gallery/risk/expected_shortfall/images/sphx_glr_plot_03_acerbi_tasche_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none normal 97.5% ES 2.338 vs 99% VaR 2.326 t(4) 97.5% ES 2.824 vs 99% VaR 2.649 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.710 seconds) .. _sphx_glr_download_api_gallery_risk_expected_shortfall_plot_03_acerbi_tasche.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/expected_shortfall/plot_03_acerbi_tasche.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_acerbi_tasche.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_acerbi_tasche.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_acerbi_tasche.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_