.. 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_02_euler_allocation.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_02_euler_allocation.py: Tasche: Euler risk allocation ============================= A bank's capital is set for the whole portfolio, but desks must be charged for their share. Because VaR, ES and volatility are positively homogeneous in the position sizes, Euler's theorem splits them exactly: .. math:: \rho(w) = \sum_i w_i \frac{\partial \rho}{\partial w_i}. Tasche (1999) proved that these Euler contributions are the only allocation under which growing a position with an above-average return on risk capital improves the whole portfolio's. A hedge gets a negative contribution: it releases capital. .. GENERATED FROM PYTHON SOURCE LINES 18-31 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.risk import euler_allocation, expected_shortfall, scenario_es_allocation from financialkit.risk.visualizers import plot_allocation labels = ["equities", "credit", "rates", "FX hedge"] w = np.array([40.0, 30.0, 50.0, 15.0]) # $m vols = np.array([0.012, 0.008, 0.004, 0.006]) corr = np.array([[1.0, 0.6, -0.2, 0.3], [0.6, 1.0, 0.0, 0.2], [-0.2, 0.0, 1.0, -0.1], [0.3, 0.2, -0.1, 1.0]]) cov = corr * np.outer(vols, vols) w[3] = -w[3] # the hedge is short .. GENERATED FROM PYTHON SOURCE LINES 32-34 Normal VaR and ES contributions ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-42 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(11, 4)) for ax, m in zip(axes, ("var", "es"), strict=True): res = euler_allocation(w, cov, 0.99, m) plot_allocation(res, labels, ax=ax) print(f"{m}: " + ", ".join(f"{lbl} {c:.3f}" for lbl, c in zip(labels, res.contributions, strict=True)) + f"; total {res.total:.3f}") fig.tight_layout() .. image-sg:: /api/gallery/risk/expected_shortfall/images/sphx_glr_plot_02_euler_allocation_001.png :alt: plot 02 euler allocation :srcset: /api/gallery/risk/expected_shortfall/images/sphx_glr_plot_02_euler_allocation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none var: equities 0.977, credit 0.447, rates 0.083, FX hedge -0.027; total 1.480 es: equities 1.120, credit 0.513, rates 0.095, FX hedge -0.031; total 1.696 .. GENERATED FROM PYTHON SOURCE LINES 43-49 Scenario-based ES contributions ------------------------------- With fat-tailed scenarios, each position's contribution is minus its average P&L in the portfolio's worst 1% of days, and they still add up to the portfolio's ES. .. GENERATED FROM PYTHON SOURCE LINES 49-59 .. code-block:: Python rng = np.random.default_rng(0) nu = 4 Z = rng.multivariate_normal(np.zeros(4), corr, 20_000) / np.sqrt(rng.chisquare(nu, (20_000, 1)) / (nu - 2)) pnl = Z * vols * w res = scenario_es_allocation(pnl, 0.99) print(f"scenario ES {res.total:.3f} (portfolio ES {expected_shortfall(pnl.sum(axis=1), 0.99):.3f})") print("shares: " + ", ".join(f"{lbl} {s:.0%}" for lbl, s in zip(labels, res.shares, strict=True))) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none scenario ES 2.275 (portfolio ES 2.275) shares: equities 68%, credit 29%, rates 5%, FX hedge -2% .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.063 seconds) .. _sphx_glr_download_api_gallery_risk_expected_shortfall_plot_02_euler_allocation.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_02_euler_allocation.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_euler_allocation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_euler_allocation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_euler_allocation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_