.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/time_series/long_memory/plot_01_hurst_exponent.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_long_memory_plot_01_hurst_exponent.py: Hurst exponent ============== To size the Aswan reservoirs, Harold Edwin Hurst (1951) studied the *range* of cumulative departures of the Nile's annual flow from its mean: the storage a dam would need to deliver the average flow every year. Divided by the standard deviation, the range of independent inflows grows like :math:`n^{1/2}`; Hurst found :math:`n^{0.73}` for the Nile and many other records. Wet years cluster with wet years. Mandelbrot later linked the exponent :math:`H` to fractional Brownian motion: :math:`H > 1/2` means persistence, :math:`H < 1/2` mean reversion, and in markets a reliable :math:`H \ne 1/2` would make returns predictable. .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.time_series import hurst_exponent, simulate_fgn from financialkit.time_series.visualizers import plot_rescaled_range .. GENERATED FROM PYTHON SOURCE LINES 24-27 Persistent, independent and anti-persistent noise ------------------------------------------------- Fractional Gaussian noise with three exponents, cumulated into "flows". .. GENERATED FROM PYTHON SOURCE LINES 27-37 .. code-block:: Python rng = np.random.default_rng(1951) series = {H: simulate_fgn(4096, H, rng=rng) for H in (0.3, 0.5, 0.75)} fig, ax = plt.subplots(figsize=(8, 3.5)) for H, x in series.items(): ax.plot(np.cumsum(x), lw=0.8, label=f"H = {H}") ax.set_xlabel("year") ax.set_ylabel("cumulative departure") ax.legend() .. image-sg:: /api/gallery/time_series/long_memory/images/sphx_glr_plot_01_hurst_exponent_001.png :alt: plot 01 hurst exponent :srcset: /api/gallery/time_series/long_memory/images/sphx_glr_plot_01_hurst_exponent_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-44 Rescaled-range analysis ----------------------- The slope of :math:`\log(R/S)_n` on :math:`\log n` estimates :math:`H`. The dashed line is Anis and Lloyd's exact expectation for independent data; its slope exceeds 1/2 in short windows, which is why the corrected estimate measures the excess over it. .. GENERATED FROM PYTHON SOURCE LINES 44-53 .. code-block:: Python fig, ax = plt.subplots() for H, x in series.items(): res = hurst_exponent(x) plot_rescaled_range(res, ax=ax, label=f"true {H}:") print(f"true H = {H}: raw slope {res.hurst:.2f}, corrected {res.hurst_corrected:.2f}") ax.legend(fontsize=8) plt.show() .. image-sg:: /api/gallery/time_series/long_memory/images/sphx_glr_plot_01_hurst_exponent_002.png :alt: plot 01 hurst exponent :srcset: /api/gallery/time_series/long_memory/images/sphx_glr_plot_01_hurst_exponent_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none true H = 0.3: raw slope 0.38, corrected 0.34 true H = 0.5: raw slope 0.52, corrected 0.48 true H = 0.75: raw slope 0.72, corrected 0.69 .. GENERATED FROM PYTHON SOURCE LINES 54-56 The returns of a random-walk price have :math:`H \approx 1/2` once the small-sample bias is removed. .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python returns = 0.01 * np.random.default_rng(0).standard_normal(4096) print(f"random-walk returns: corrected H = {hurst_exponent(returns).hurst_corrected:.2f}") .. rst-class:: sphx-glr-script-out .. code-block:: none random-walk returns: corrected H = 0.52 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.077 seconds) .. _sphx_glr_download_api_gallery_time_series_long_memory_plot_01_hurst_exponent.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/long_memory/plot_01_hurst_exponent.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_hurst_exponent.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_hurst_exponent.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_hurst_exponent.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_