.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/time_series/unit_roots/plot_01_dickey_fuller.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_unit_roots_plot_01_dickey_fuller.py: Dickey-Fuller unit roots ======================== Is a series a random walk (:math:`\rho = 1`) or does it revert to a mean (:math:`\rho < 1`)? Regressing :math:`\Delta y_t` on :math:`y_{t-1}` and looking at the t-ratio of the slope seems natural, but under a unit root that ratio does not have a t distribution: it is skewed to the left, and more so when a constant or trend is included. David Dickey and Wayne Fuller (1979) tabulated its distribution by simulation. Their test, with lagged differences added (Said and Dickey 1984) and MacKinnon's critical values, is still the first thing run on an interest rate, a price-dividend ratio or a pairs-trading spread. .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from financialkit.time_series import adf_test, mackinnon_critical_values, simulate_arma from financialkit.time_series.utils import simulate_dickey_fuller .. GENERATED FROM PYTHON SOURCE LINES 25-30 The Dickey-Fuller distributions ------------------------------- Ten thousand random walks of length 250, tested with no deterministic terms, a constant, and a constant and trend. The 5% critical values (vertical lines) lie far to the left of the normal's :math:`-1.645`. .. GENERATED FROM PYTHON SOURCE LINES 30-44 .. code-block:: Python T = 250 fig, ax = plt.subplots() g = np.linspace(-5, 3, 300) ax.plot(g, stats.norm.pdf(g), "k", lw=1, label="N(0, 1)") for i, (reg, label) in enumerate((("n", "none"), ("c", "constant"), ("ct", "constant + trend"))): tau = simulate_dickey_fuller(T, 10_000, reg, rng=i) ax.hist(tau, bins=60, density=True, histtype="step", lw=1.5, color=f"C{i}", label=label) cv = mackinnon_critical_values(T, reg)["5%"] ax.axvline(cv, color=f"C{i}", ls="--", lw=1) print(f"{label:16s} simulated 5% quantile {np.quantile(tau, 0.05):.3f}, MacKinnon {cv:.3f}") ax.set_xlabel("t-ratio of gamma") ax.legend() .. image-sg:: /api/gallery/time_series/unit_roots/images/sphx_glr_plot_01_dickey_fuller_001.png :alt: plot 01 dickey fuller :srcset: /api/gallery/time_series/unit_roots/images/sphx_glr_plot_01_dickey_fuller_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none none simulated 5% quantile -1.924, MacKinnon -1.942 constant simulated 5% quantile -2.861, MacKinnon -2.873 constant + trend simulated 5% quantile -3.457, MacKinnon -3.428 .. GENERATED FROM PYTHON SOURCE LINES 45-53 Testing two rates ----------------- A short rate that mean-reverts (:math:`\rho = 0.95` a month) and one that is a random walk look alike over 30 years. The augmented test rejects a unit root for the first and not for the second. At this sample size it detects :math:`\rho = 0.95` only about four times in five, and its power falls fast as :math:`\rho \to 1`, so failing to reject is weak evidence of a unit root. .. GENERATED FROM PYTHON SOURCE LINES 53-67 .. code-block:: Python rng = np.random.default_rng(0) months = 360 reverting = 0.04 + simulate_arma(ar=[0.95], n=months, sigma=0.002, rng=rng) walk = 0.04 + np.cumsum(0.002 * rng.standard_normal(months)) fig, ax = plt.subplots(figsize=(8, 3)) for name, x in (("mean-reverting", reverting), ("random walk", walk)): res = adf_test(x, regression="c") ax.plot(100 * x, label=f"{name}: ADF = {res.statistic:.2f}") print(f"{name:15s} ADF = {res.statistic:.2f} (5% cv {res.critical_values['5%']:.2f}), lags {res.lags}, reject: {res.rejects()}") ax.set_xlabel("month") ax.set_ylabel("rate (%)") ax.legend() plt.show() .. image-sg:: /api/gallery/time_series/unit_roots/images/sphx_glr_plot_01_dickey_fuller_002.png :alt: plot 01 dickey fuller :srcset: /api/gallery/time_series/unit_roots/images/sphx_glr_plot_01_dickey_fuller_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none mean-reverting ADF = -3.86 (5% cv -2.87), lags 3, reject: True random walk ADF = -2.08 (5% cv -2.87), lags 0, reject: False .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.144 seconds) .. _sphx_glr_download_api_gallery_time_series_unit_roots_plot_01_dickey_fuller.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/unit_roots/plot_01_dickey_fuller.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_dickey_fuller.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_dickey_fuller.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_dickey_fuller.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_