.. 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_01_mandelbrot_fat_tails.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_01_mandelbrot_fat_tails.py: Mandelbrot: fat tails ===================== Benoit Mandelbrot (1963) plotted the frequency of large changes in cotton prices on doubly logarithmic paper and found straight lines: the tail probability :math:`P(|X| > u)` decays like a power :math:`u^{-\alpha}`, not like the Gaussian :math:`e^{-u^2/2}`. Five-sigma days, which a normal distribution makes once-in-several-millennia events, happen every few years. He proposed the stable (Lévy) laws, with :math:`\alpha \approx 1.7` for cotton; later work settled on tail indices between 3 and 5 for most returns, but the log-log plot remains the standard diagnostic. .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from financialkit.time_series import return_statistics from financialkit.time_series.visualizers import plot_tail .. GENERATED FROM PYTHON SOURCE LINES 24-28 Three samples with the same scale --------------------------------- Daily "returns" from a normal, a Student t with 3 degrees of freedom, and a symmetric stable law with Mandelbrot's :math:`\alpha = 1.7`. .. GENERATED FROM PYTHON SOURCE LINES 28-47 .. code-block:: Python rng = np.random.default_rng(1963) n = 20_000 samples = { "normal": rng.standard_normal(n), "Student t(3)": stats.t.rvs(3, size=n, random_state=rng) / np.sqrt(3), "stable (alpha 1.7)": stats.levy_stable.rvs(1.7, 0.0, size=n, random_state=rng) / np.sqrt(2), } fig, ax = plt.subplots() for name, x in samples.items(): plot_tail(x, ax=ax, label=name) u = np.logspace(0.5, 2, 20) ax.loglog(u, 0.6 * u**-1.7, "k--", lw=1, label=r"slope $-1.7$") ax.set_ylim(1 / n, 1) ax.set_xlim(0.1, 200) ax.legend() ax.set_title("Log-log tails: straight lines are power laws") .. image-sg:: /api/gallery/time_series/stylized_facts/images/sphx_glr_plot_01_mandelbrot_fat_tails_001.png :alt: Log-log tails: straight lines are power laws :srcset: /api/gallery/time_series/stylized_facts/images/sphx_glr_plot_01_mandelbrot_fat_tails_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Log-log tails: straight lines are power laws') .. GENERATED FROM PYTHON SOURCE LINES 48-53 Moments ------- The normal sample has zero excess kurtosis; the fat-tailed ones have huge, unstable sample kurtosis (the stable law's variance is infinite), and Jarque-Bera rejects normality at any level. .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: Python for name, x in samples.items(): s = return_statistics(x) print(f"{name:22s} excess kurtosis {s.excess_kurtosis:9.1f} JB p-value {s.jarque_bera_pvalue:.2g}") .. rst-class:: sphx-glr-script-out .. code-block:: none normal excess kurtosis 0.0 JB p-value 0.23 Student t(3) excess kurtosis 86.8 JB p-value 0 stable (alpha 1.7) excess kurtosis 1268.7 JB p-value 0 .. GENERATED FROM PYTHON SOURCE LINES 59-61 How often are there 5-sigma days? --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 61-68 .. code-block:: Python for name, x in samples.items(): freq = np.mean(np.abs(x) > 5 * x.std()) years = np.inf if freq == 0 else 1 / (freq * 252) print(f"{name:22s} one |move| > 5 sd every {years:,.1f} years") print(f"{'normal theory':22s} one every {1 / (2 * stats.norm.sf(5) * 252):,.0f} years") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none normal one |move| > 5 sd every inf years Student t(3) one |move| > 5 sd every 1.3 years stable (alpha 1.7) one |move| > 5 sd every 1.6 years normal theory one every 6,922 years .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.085 seconds) .. _sphx_glr_download_api_gallery_time_series_stylized_facts_plot_01_mandelbrot_fat_tails.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_01_mandelbrot_fat_tails.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_mandelbrot_fat_tails.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_mandelbrot_fat_tails.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_mandelbrot_fat_tails.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_