.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_dynamics/plot_23_order_selection.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 Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_06_dynamics_plot_23_order_selection.py: Model order selection for NARX models ===================================== Which lags matter? For every pair of orders :math:`(n_a, n_b)`, :func:`~eqdisco.dynamics.narx_order_scan` builds a second-degree polynomial NARX library and selects terms by forward orthogonal least squares with the Bayesian information criterion -- the classical FROLS approach of the NARMAX literature (Billings, 2013). The system is a second-order Hammerstein model with a quadratic input nonlinearity and equation-error noise, so the scan should point to :math:`n_a=2`. With measurement noise on the output instead, the equation error becomes colored and higher orders are selected to compensate -- the parameter study shows the effect. .. GENERATED FROM PYTHON SOURCE LINES 15-31 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq from eqdisco.dynamics import narx_order_scan rng = np.random.default_rng(1) T = 500 u = rng.uniform(-1, 1, T) v = u + 0.5 * u**2 e = 0.01 * rng.normal(size=T) y = np.zeros(T) for k in range(2, T): y[k] = 1.2 * y[k - 1] - 0.5 * y[k - 2] + 0.4 * v[k - 1] + e[k] .. GENERATED FROM PYTHON SOURCE LINES 32-34 The scan -------- .. GENERATED FROM PYTHON SOURCE LINES 34-51 .. code-block:: Python scan = narx_order_scan(u, y, max_na=4, max_nb=3, degree=2) print(scan.head(5)[["na", "nb", "n_terms", "r2", "bic", "terms"]]) M = scan.pivot(index="na", columns="nb", values="bic") fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) im = axs[0].imshow(M.to_numpy(), cmap="Blues_r", aspect="auto") axs[0].set_xticks(range(M.shape[1])) axs[0].set_xticklabels(M.columns) axs[0].set_yticks(range(M.shape[0])) axs[0].set_yticklabels(M.index) axs[0].set_xlabel("input order $n_b$") axs[0].set_ylabel("output order $n_a$") fig.colorbar(im, ax=axs[0], label="BIC") best = scan.iloc[0] m = eq.NARXRegressor(eq.SparseRegressor(degree=2, method="frols"), na=int(best["na"]), nb=int(best["nb"])).fit(u[:300], y[:300]) eq.viz.plot_simulation(np.arange(200), y[300:], m.simulate(u[300:], y[300:]), ax=axs[1], labels=("measured", f"free run, na={int(best['na'])}, nb={int(best['nb'])}")) fig.tight_layout() .. image-sg:: /auto_examples/06_dynamics/images/sphx_glr_plot_23_order_selection_001.png :alt: plot 23 order selection :srcset: /auto_examples/06_dynamics/images/sphx_glr_plot_23_order_selection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none na nb n_terms r2 bic terms 0 2 1 4 0.999503 -3089.171265 y(k-1), u(k-1), y(k-2), u(k-1)^2 1 2 2 4 0.999503 -3089.171265 y(k-1), u(k-1), y(k-2), u(k-1)^2 2 3 2 4 0.999503 -3082.252513 y(k-1), u(k-1), y(k-2), u(k-1)^2 3 3 3 4 0.999503 -3082.252513 y(k-1), u(k-1), y(k-2), u(k-1)^2 4 3 1 4 0.999503 -3082.252513 y(k-1), u(k-1), y(k-2), u(k-1)^2 .. GENERATED FROM PYTHON SOURCE LINES 52-54 Parameter study: measurement noise and the selected order --------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 54-65 .. code-block:: Python noises = [0.0, 0.05, 0.2] fig, ax = plt.subplots(figsize=(4.5, 3)) for k, nz in enumerate(noises): s = narx_order_scan(u, y + nz * rng.normal(size=T), max_na=4, max_nb=3) best_by_na = s.groupby("na")["bic"].min() print(f"noise {nz}: best na by BIC = {int(best_by_na.idxmin())}") ax.plot(best_by_na.index, best_by_na - best_by_na.min(), "o-", color=eq.viz.OKABE_ITO[k], label=f"measurement noise {nz}") ax.set_xlabel("output order $n_a$") ax.set_ylabel("BIC above the best") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/06_dynamics/images/sphx_glr_plot_23_order_selection_002.png :alt: plot 23 order selection :srcset: /auto_examples/06_dynamics/images/sphx_glr_plot_23_order_selection_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none noise 0.0: best na by BIC = 2 noise 0.05: best na by BIC = 4 noise 0.2: best na by BIC = 4 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.279 seconds) .. _sphx_glr_download_auto_examples_06_dynamics_plot_23_order_selection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/eqdisco/main?urlpath=lab/tree/notebooks/auto_examples/06_dynamics/plot_23_order_selection.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_23_order_selection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_23_order_selection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_23_order_selection.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_