.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_gpols/plot_11_ols_err.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_03_gpols_plot_11_ols_err.py: Orthogonal least squares and the error reduction ratio ====================================================== A linear-in-parameters model :math:`y=w_0+\sum_i w_iF_i(\mathbf x)` can be assessed term by term. Orthogonal least squares (Chen, Billings and Luo, 1989) orthogonalizes the regressors and assigns each term its *error reduction ratio* (ERR): the share of the output variance it explains beyond the terms before it. Forward selection by ERR (FROLS) builds a model term by term; the ERR values also show that a term's contribution depends on the order of orthogonalization when regressors are correlated. .. GENERATED FROM PYTHON SOURCE LINES 13-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq from eqdisco.models import library_matrix, polynomial_library from eqdisco.ols import forward_ols, ols_err rng = np.random.default_rng(0) X = rng.uniform(-1, 1, (300, 3)) X[:, 2] = 0.9 * X[:, 0] + 0.2 * X[:, 2] # x2 strongly correlated with x0 y = 1.2 * X[:, 0] + 0.9 * X[:, 2] - 0.8 * X[:, 0] * X[:, 1] + 0.02 * rng.normal(size=300) .. GENERATED FROM PYTHON SOURCE LINES 27-29 Forward selection over a polynomial library ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-39 .. code-block:: Python terms = polynomial_library(3, degree=2) Phi = library_matrix(terms, X) res = forward_ols(Phi, y, criterion="bic") labels = [t.to_string() for t in terms] for j in res.order: print(f"{labels[j]:10s} ERR = {res.err[j]:.4f} weight = {res.coef[j]: .3f}") fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) eq.viz.plot_err_path(res, ax=axs[0], labels=labels) axs[0].set_title("FROLS on 9 candidate terms", fontsize=9) .. image-sg:: /auto_examples/03_gpols/images/sphx_glr_plot_11_ols_err_001.png :alt: FROLS on 9 candidate terms :srcset: /auto_examples/03_gpols/images/sphx_glr_plot_11_ols_err_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none x0 ERR = 0.9439 weight = 1.190 x0 * x1 ERR = 0.0478 weight = -0.801 x2 ERR = 0.0080 weight = 0.912 Text(0.5, 1.0, 'FROLS on 9 candidate terms') .. GENERATED FROM PYTHON SOURCE LINES 40-42 The ERR depends on the order ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-54 .. code-block:: Python sel = res.order[:3] orders = [sel, sel[::-1], [sel[1], sel[0], sel[2]]] E = np.array([ols_err(Phi[:, sel], y, order=[sel.index(o) for o in od]).err for od in orders]) w = 0.25 for k, od in enumerate(orders): axs[1].bar(np.arange(3) + (k - 1) * w, E[k], width=w, color=eq.viz.OKABE_ITO[k], label="order: " + ", ".join(labels[o] for o in od)) axs[1].set_xticks(range(3)) axs[1].set_xticklabels([labels[s] for s in sel]) axs[1].set_ylabel("ERR") axs[1].legend(frameon=False, fontsize=7) fig.tight_layout() .. GENERATED FROM PYTHON SOURCE LINES 55-57 Parameter study: noise and the number of selected terms ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 57-73 .. code-block:: Python noises = [0.0, 0.05, 0.2, 0.5] n_terms = {c: [] for c in ("bic", "aic", "err")} for nz in noises: yn = y + nz * rng.normal(size=len(y)) for c in n_terms: n_terms[c].append(len(forward_ols(Phi, yn, criterion=c, err_tol=1e-3).order)) print("ERR of the three terms in three orders:", np.round(E, 4).tolist()) print("terms selected by noise", noises, n_terms) fig, ax = plt.subplots(figsize=(4.5, 3)) for k, (c, v) in enumerate(n_terms.items()): ax.plot(noises, v, "o-", color=eq.viz.OKABE_ITO[k], label=c.upper() if c != "err" else "ERR > 0.001") ax.axhline(3, color="#6B7785", ls="--", lw=1) ax.set_xlabel("noise standard deviation") ax.set_ylabel("selected terms") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/03_gpols/images/sphx_glr_plot_11_ols_err_002.png :alt: plot 11 ols err :srcset: /auto_examples/03_gpols/images/sphx_glr_plot_11_ols_err_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ERR of the three terms in three orders: [[0.9439, 0.0478, 0.008], [0.0159, 0.0432, 0.9407], [0.9823, 0.0094, 0.008]] terms selected by noise [0.0, 0.05, 0.2, 0.5] {'bic': [3, 3, 3, 3], 'aic': [3, 3, 3, 3], 'err': [3, 3, 3, 3]} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.145 seconds) .. _sphx_glr_download_auto_examples_03_gpols_plot_11_ols_err.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/03_gpols/plot_11_ols_err.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_11_ols_err.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_11_ols_err.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_11_ols_err.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_