.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/time_series/efficiency/plot_02_fama_runs_test.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_efficiency_plot_02_fama_runs_test.py: Fama's runs test ================ Correlation coefficients can be dominated by a few large changes. In his 1965 thesis Eugene Fama added a distribution-free check on the 30 Dow Jones stocks: count the *runs*, the unbroken sequences of price changes with the same sign. Positive dependence produces long runs and too few of them; reversals produce too many. Under independence the number of runs is approximately normal with a known mean and variance, and Fama found the observed counts close to the expected ones. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.signal import lfilter from financialkit.time_series import runs_test .. GENERATED FROM PYTHON SOURCE LINES 22-26 Runs in three series -------------------- Independent changes, positively autocorrelated changes (trends), and negatively autocorrelated changes (reversals, such as bid-ask bounce). .. GENERATED FROM PYTHON SOURCE LINES 26-44 .. code-block:: Python rng = np.random.default_rng(1965) e = rng.standard_normal(1000) cases = { "independent": e, "trending (phi = +0.3)": lfilter([1.0], [1.0, -0.3], e), "reverting (phi = -0.3)": lfilter([1.0], [1.0, 0.3], e), } fig, axes = plt.subplots(3, 1, figsize=(8, 5), sharex=True) for ax, (name, x) in zip(axes, cases.items(), strict=True): res = runs_test(x) ax.bar(np.arange(60), np.sign(x[:60]), color=np.where(x[:60] > 0, "C2", "C3"), width=1.0) ax.set_yticks([]) ax.set_title(f"{name}: z = {res.statistic:+.2f}, p = {res.pvalue:.3f}", fontsize=9) axes[-1].set_xlabel("day (first 60 signs shown)") fig.tight_layout() .. image-sg:: /api/gallery/time_series/efficiency/images/sphx_glr_plot_02_fama_runs_test_001.png :alt: independent: z = +0.54, p = 0.589, trending (phi = +0.3): z = -6.96, p = 0.000, reverting (phi = -0.3): z = +5.48, p = 0.000 :srcset: /api/gallery/time_series/efficiency/images/sphx_glr_plot_02_fama_runs_test_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-49 The test's size --------------- Under independence the statistic is close to standard normal, so the test rejects about 5% of the time. .. GENERATED FROM PYTHON SOURCE LINES 49-59 .. code-block:: Python z = np.array([runs_test(rng.standard_normal(250)).statistic for _ in range(1000)]) fig, ax = plt.subplots() ax.hist(z, bins=30, density=True, color="0.7", label="1000 independent samples") grid = np.linspace(-4, 4, 200) ax.plot(grid, np.exp(-(grid**2) / 2) / np.sqrt(2 * np.pi), "k", label="N(0, 1)") ax.set_xlabel("runs statistic") ax.legend() print(f"rejection rate at 5%: {np.mean(np.abs(z) > 1.96):.3f}") plt.show() .. image-sg:: /api/gallery/time_series/efficiency/images/sphx_glr_plot_02_fama_runs_test_002.png :alt: plot 02 fama runs test :srcset: /api/gallery/time_series/efficiency/images/sphx_glr_plot_02_fama_runs_test_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none rejection rate at 5%: 0.051 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.125 seconds) .. _sphx_glr_download_api_gallery_time_series_efficiency_plot_02_fama_runs_test.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/efficiency/plot_02_fama_runs_test.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_fama_runs_test.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_fama_runs_test.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_fama_runs_test.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_