.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/time_series/volatility/plot_03_bollerslev_garch.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_time_series_volatility_plot_03_bollerslev_garch.py: Bollerslev's GARCH ================== Pure ARCH models needed many lags to capture how slowly volatility decays. Tim Bollerslev (1986), Engle's student, added the lagged variance itself, .. math:: \sigma_t^2 = \omega + \alpha\,\varepsilon_{t-1}^2 + \beta\,\sigma_{t-1}^2, an ARCH(:math:`\infty`) with geometrically declining weights in three parameters. Fitted to daily returns almost anywhere, GARCH(1,1) gives :math:`\alpha \approx 0.05\text{-}0.10` and :math:`\alpha + \beta` just below one: shocks to volatility are small but long-lived. Variance forecasts revert to :math:`\bar\sigma^2 = \omega/(1-\alpha-\beta)` at rate :math:`(\alpha+\beta)^h`. .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.constants import TRADING_DAYS_PER_YEAR from financialkit.time_series import GARCH from financialkit.time_series.visualizers import plot_conditional_volatility .. GENERATED FROM PYTHON SOURCE LINES 27-31 Fitting GARCH(1,1) by maximum likelihood ---------------------------------------- Eight years of daily returns from a GARCH(1,1) with typical equity-index parameters (16% long-run volatility, persistence 0.98). .. GENERATED FROM PYTHON SOURCE LINES 31-44 .. code-block:: Python ann = TRADING_DAYS_PER_YEAR true = GARCH(omega=0.16**2 / ann * 0.02, alpha=0.08, beta=0.90, mu=3e-4) r = true.simulate(2000, rng=1986).returns fit = GARCH.fit(r) for k, v in fit.params.items(): print(f"{k:5s} true {true.params[k]:.3g} fitted {v:.3g} +/- {fit.std_errors[k]:.2g}") print(f"persistence {fit.model.persistence:.3f}, half-life {fit.model.half_life:.0f} days") fig, ax = plt.subplots(figsize=(8, 3.5)) plot_conditional_volatility(r, fit.variance, ax=ax) ax.set_title("Fitted conditional volatility") .. image-sg:: /api/gallery/time_series/volatility/images/sphx_glr_plot_03_bollerslev_garch_001.png :alt: Fitted conditional volatility :srcset: /api/gallery/time_series/volatility/images/sphx_glr_plot_03_bollerslev_garch_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none mu true 0.0003 fitted 0.000604 +/- 0.00021 omega true 2.03e-06 fitted 2.44e-06 +/- 8.6e-07 alpha true 0.08 fitted 0.0691 +/- 0.012 beta true 0.9 fitted 0.908 +/- 0.017 persistence 0.978, half-life 31 days Text(0.5, 1.0, 'Fitted conditional volatility') .. GENERATED FROM PYTHON SOURCE LINES 45-49 The term structure of volatility forecasts ------------------------------------------ From a calm day and from a turbulent day, forecasts converge to the long-run volatility at the rate set by the persistence. .. GENERATED FROM PYTHON SOURCE LINES 49-62 .. code-block:: Python h = 250 calm = int(np.argmin(fit.variance[500:])) + 500 wild = int(np.argmax(fit.variance[500:])) + 500 fig, ax = plt.subplots() for name, t in (("after a calm day", calm), ("after a turbulent day", wild)): f = fit.model.forecast(r[: t + 1], h) ax.plot(np.arange(1, h + 1), 100 * np.sqrt(ann * f), label=name) ax.axhline(100 * np.sqrt(ann * fit.model.unconditional_variance), color="k", ls="--", lw=1, label="long-run") ax.set_xlabel("horizon (days)") ax.set_ylabel("forecast volatility (% a year)") ax.legend() plt.show() .. image-sg:: /api/gallery/time_series/volatility/images/sphx_glr_plot_03_bollerslev_garch_002.png :alt: plot 03 bollerslev garch :srcset: /api/gallery/time_series/volatility/images/sphx_glr_plot_03_bollerslev_garch_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.071 seconds) .. _sphx_glr_download_api_gallery_time_series_volatility_plot_03_bollerslev_garch.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/time_series/volatility/plot_03_bollerslev_garch.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_bollerslev_garch.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_bollerslev_garch.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_bollerslev_garch.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_