.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/time_series/linear_models/plot_02_box_jenkins_arma.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_linear_models_plot_02_box_jenkins_arma.py: Box-Jenkins ARMA ================ George Box and Gwilym Jenkins (1970) turned the mixed autoregressive moving-average model .. math:: \phi(B)(x_t - \mu) = \theta(B)\,\varepsilon_t into a three-stage method. *Identify* the orders from the autocorrelation function (which cuts off after lag :math:`q` for a pure MA) and the partial autocorrelation function (which cuts off after lag :math:`p` for a pure AR); *estimate* by maximum likelihood; *check* that the residuals are white noise; then forecast. Here the cycle is run on a simulated ARMA(1,1), such as a spread or a monthly growth rate might follow. .. GENERATED FROM PYTHON SOURCE LINES 19-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.time_series import fit_arma, ljung_box, simulate_arma from financialkit.time_series.visualizers import plot_acf x = simulate_arma(ar=[0.75], ma=[0.4], n=600, mean=2.0, rng=3) .. GENERATED FROM PYTHON SOURCE LINES 28-31 Identification -------------- Neither function cuts off: both decay, which points to a mixed model. .. GENERATED FROM PYTHON SOURCE LINES 31-39 .. code-block:: Python fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 3.5)) plot_acf(x, 15, ax=ax1) plot_acf(x, 15, partial=True, ax=ax2) ax1.set_title("ACF: geometric decay") ax2.set_title("PACF: decays too") fig.tight_layout() .. image-sg:: /api/gallery/time_series/linear_models/images/sphx_glr_plot_02_box_jenkins_arma_001.png :alt: ACF: geometric decay, PACF: decays too :srcset: /api/gallery/time_series/linear_models/images/sphx_glr_plot_02_box_jenkins_arma_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 40-45 Estimation and diagnostic checking ---------------------------------- Conditional maximum likelihood recovers :math:`\phi = 0.75` and :math:`\theta = 0.4`, and the Ljung-Box test finds no correlation left in the residuals (two degrees of freedom are used up by the fit). .. GENERATED FROM PYTHON SOURCE LINES 45-51 .. code-block:: Python res = fit_arma(x, 1, 1) lb = ljung_box(res.residuals, lags=12, fitted_params=2) print(f"phi = {res.ar[0]:.3f}, theta = {res.ma[0]:.3f}, mu = {res.mean:.3f}, sigma^2 = {res.sigma2:.3f}") print(f"Ljung-Box on residuals: Q = {lb.statistic:.1f}, p = {lb.pvalue:.2f}") .. rst-class:: sphx-glr-script-out .. code-block:: none phi = 0.724, theta = 0.447, mu = 2.110, sigma^2 = 0.990 Ljung-Box on residuals: Q = 11.8, p = 0.30 .. GENERATED FROM PYTHON SOURCE LINES 52-56 Forecasting ----------- Forecasts decay geometrically back to the mean, and the 95% interval widens to the unconditional spread. .. GENERATED FROM PYTHON SOURCE LINES 56-69 .. code-block:: Python h = 30 mean, se = res.forecast(h) t = np.arange(x.size) tf = np.arange(x.size, x.size + h) fig, ax = plt.subplots(figsize=(8, 3.5)) ax.plot(t[-120:], x[-120:], color="C0", label="observed") ax.plot(tf, mean, color="C3", label="forecast") ax.fill_between(tf, mean - 1.96 * se, mean + 1.96 * se, color="C3", alpha=0.2, label="95% interval") ax.axhline(res.mean, color="k", ls=":", lw=1) ax.set_xlabel("period") ax.legend(loc="upper left") plt.show() .. image-sg:: /api/gallery/time_series/linear_models/images/sphx_glr_plot_02_box_jenkins_arma_002.png :alt: plot 02 box jenkins arma :srcset: /api/gallery/time_series/linear_models/images/sphx_glr_plot_02_box_jenkins_arma_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.082 seconds) .. _sphx_glr_download_api_gallery_time_series_linear_models_plot_02_box_jenkins_arma.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/linear_models/plot_02_box_jenkins_arma.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_box_jenkins_arma.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_box_jenkins_arma.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_box_jenkins_arma.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_