.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/time_series/stylized_facts/plot_02_hill_tail_index.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_stylized_facts_plot_02_hill_tail_index.py: Hill tail index =============== If the tail is a power law, :math:`P(X > u) \approx C u^{-\alpha}`, the log-excesses of the largest observations over a threshold are exponential with mean :math:`1/\alpha`. Bruce Hill (1975) turned this into the estimator .. math:: \hat\alpha_k = \Bigl[\frac1k\sum_{i=1}^{k}\ln X_{(i)} - \ln X_{(k+1)}\Bigr]^{-1}, using only the :math:`k` largest order statistics. The tail index decides which moments exist (those of order below :math:`\alpha`), and so whether the variance and kurtosis that risk models rely on are finite. .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.time_series import EGARCH, hill_estimator from financialkit.time_series.visualizers import plot_hill .. GENERATED FROM PYTHON SOURCE LINES 25-31 The Hill plot ------------- For an exact Pareto tail the estimate is flat in :math:`k`. For a Student t with :math:`\nu = 4`, only the far tail is a power law, so the estimate drifts once :math:`k` reaches into the body: choosing :math:`k` trades variance (small :math:`k`) against bias (large :math:`k`). .. GENERATED FROM PYTHON SOURCE LINES 31-44 .. code-block:: Python rng = np.random.default_rng(1975) n = 20_000 pareto = rng.pareto(3.0, n) + 1.0 student = np.abs(rng.standard_t(4, n)) fig, ax = plt.subplots() plot_hill(hill_estimator(pareto, k=np.arange(20, 3000)), ax=ax, label=r"Pareto, $\alpha$ = 3") plot_hill(hill_estimator(student, k=np.arange(20, 3000)), ax=ax, label=r"|t(4)|, $\alpha$ = 4") for a in (3, 4): ax.axhline(a, color="k", ls="--", lw=0.8) ax.set_ylim(1, 7) .. image-sg:: /api/gallery/time_series/stylized_facts/images/sphx_glr_plot_02_hill_tail_index_001.png :alt: plot 02 hill tail index :srcset: /api/gallery/time_series/stylized_facts/images/sphx_glr_plot_02_hill_tail_index_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (1.0, 7.0) .. GENERATED FROM PYTHON SOURCE LINES 45-50 Fat tails from volatility clustering ------------------------------------ Returns with Gaussian innovations but a changing variance also have power-law tails. The left and right tails of an EGARCH model with a leverage effect differ: the left one is fatter. .. GENERATED FROM PYTHON SOURCE LINES 50-60 .. code-block:: Python r = EGARCH(omega=0.0, alpha=0.25, gamma=-0.12, beta=0.97).simulate(n, rng=1).returns k = np.arange(50, 1500) fig, ax = plt.subplots() plot_hill(hill_estimator(r, k=k, side="lower"), ax=ax, label="losses (left tail)") plot_hill(hill_estimator(r, k=k, side="upper"), ax=ax, label="gains (right tail)") ax.set_title("EGARCH returns with normal innovations") for side in ("lower", "upper"): print(f"{side} tail: alpha at k = 500 is {hill_estimator(r, k=500, side=side).alpha[0]:.2f}") plt.show() .. image-sg:: /api/gallery/time_series/stylized_facts/images/sphx_glr_plot_02_hill_tail_index_002.png :alt: EGARCH returns with normal innovations :srcset: /api/gallery/time_series/stylized_facts/images/sphx_glr_plot_02_hill_tail_index_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none lower tail: alpha at k = 500 is 3.50 upper tail: alpha at k = 500 is 3.68 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.118 seconds) .. _sphx_glr_download_api_gallery_time_series_stylized_facts_plot_02_hill_tail_index.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/stylized_facts/plot_02_hill_tail_index.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_hill_tail_index.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_hill_tail_index.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_hill_tail_index.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_