.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/risk/var/plot_05_hybrid_var.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_var_plot_05_hybrid_var.py: Boudoukh-Richardson-Whitelaw hybrid VaR ======================================= Historical simulation gives every day in the window the same weight, so after a jump in volatility it takes months to notice and then drops the crisis days abruptly when they leave the window. Boudoukh, Richardson and Whitelaw (1998) weighted the outcome :math:`i` days old by :math:`\lambda^i`, combining RiskMetrics' exponential smoothing with historical simulation's distribution-free tail: "the best of both worlds". .. GENERATED FROM PYTHON SOURCE LINES 14-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.risk import historical_var, hybrid_var from financialkit.risk.visualizers import plot_var rng = np.random.default_rng(11) vol = np.r_[np.full(500, 0.008), np.full(150, 0.025), np.full(350, 0.008)] pnl = vol * rng.standard_t(5, vol.size) / np.sqrt(5 / 3) window = 250 .. GENERATED FROM PYTHON SOURCE LINES 26-28 Rolling forecasts through a turbulent spell ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-42 .. code-block:: Python days = np.arange(window, pnl.size) hs = [historical_var(pnl[t - window : t], 0.99).var for t in days] bw = [hybrid_var(pnl[t - window : t], 0.99, lam=0.98).var for t in days] fig, ax = plt.subplots(figsize=(8, 3.8)) ax.plot(np.arange(pnl.size), pnl, color="0.75", lw=0.6) ax.plot(days, -np.array(hs), label="historical (equal weights)") ax.plot(days, -np.array(bw), label="hybrid, lambda = 0.98") ax.plot(np.arange(pnl.size), -2.326 * vol, "k:", lw=1, label="true 99% normal-equivalent") ax.set_xlabel("day") ax.set_ylabel("P&L and minus VaR") ax.legend(loc="lower left", fontsize=8) fig.tight_layout() .. image-sg:: /api/gallery/risk/var/images/sphx_glr_plot_05_hybrid_var_001.png :alt: plot 05 hybrid var :srcset: /api/gallery/risk/var/images/sphx_glr_plot_05_hybrid_var_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-48 The weighted distribution on the first turbulent days ----------------------------------------------------- Twenty days into the turbulent period the hybrid method already puts a third of its probability mass on them. .. GENERATED FROM PYTHON SOURCE LINES 48-55 .. code-block:: Python t = 520 res = hybrid_var(pnl[t - window : t], 0.99, 0.98) print(f"weight on last 20 days: {res.weights[-20:].sum():.2f} (equal weighting: {20 / window:.2f})") plot_var(res) plt.show() .. image-sg:: /api/gallery/risk/var/images/sphx_glr_plot_05_hybrid_var_002.png :alt: hybrid :srcset: /api/gallery/risk/var/images/sphx_glr_plot_05_hybrid_var_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none weight on last 20 days: 0.33 (equal weighting: 0.08) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.099 seconds) .. _sphx_glr_download_api_gallery_risk_var_plot_05_hybrid_var.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/var/plot_05_hybrid_var.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_hybrid_var.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_hybrid_var.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_hybrid_var.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_