.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/08_knowledge/plot_31_symmetry.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_08_knowledge_plot_31_symmetry.py: Separability and symmetries detected from data ============================================== Before searching, it pays to ask what form the law has. Fitting a smooth neural surrogate to the data and probing it (:func:`~eqdisco.knowledge.fit_surrogate`) reveals additive and multiplicative separability, translational symmetry (:math:`f` depends on :math:`x_i-x_j`) and scaling symmetry (:math:`x_i/x_j`) -- the tests of AI Feynman (Udrescu and Tegmark, 2020). The data follow the Feynman equation I.18.4, the centre of mass :math:`r=(m_1r_1+m_2r_2)/(m_1+m_2)`. .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq from eqdisco.knowledge import fit_surrogate, interaction_matrix, suggest_structure, symmetry_tests prob = eq.datasets.get_problem("feynman-I.18.4", n_train=800) names = prob.feature_names .. GENERATED FROM PYTHON SOURCE LINES 23-25 Tests on a surrogate -------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-45 .. code-block:: Python f = fit_surrogate(prob.X_train, prob.y_train, random_state=0) M = interaction_matrix(f, prob.X_train) st = symmetry_tests(f, prob.X_train, names) print(st.round(4)) for line in suggest_structure(f, prob.X_train, names, tol=0.03): print(line) fig, axs = plt.subplots(1, 2, figsize=(10, 3.6)) eq.viz.plot_interaction_matrix(M, names, ax=axs[0]) axs[0].set_title("additive interaction of variable pairs", fontsize=9) x = np.arange(len(st)) axs[1].bar(x - 0.2, st["scaling"], 0.4, color="#285780", label="scaling $x_i/x_j$") axs[1].bar(x + 0.2, st["translation"], 0.4, color="#D55E00", label="translation $x_i-x_j$") axs[1].plot(x, st["reference"], "k_", ms=18, label="change of one variable") axs[1].set_xticks(x) axs[1].set_xticklabels(st["pair"], rotation=30, fontsize=8) axs[1].set_yscale("log") axs[1].set_ylabel("relative change of f") axs[1].legend(frameon=False, fontsize=7) fig.tight_layout() .. image-sg:: /auto_examples/08_knowledge/images/sphx_glr_plot_31_symmetry_001.png :alt: additive interaction of variable pairs :srcset: /auto_examples/08_knowledge/images/sphx_glr_plot_31_symmetry_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none pair translation scaling reference 0 m1, m2 0.0114 0.0063 0.0207 1 m1, r1 0.0772 0.2193 0.0207 2 m1, r2 0.0745 0.1856 0.0207 3 m2, r1 0.0736 0.1841 0.0214 4 m2, r2 0.0777 0.2235 0.0216 5 r1, r2 0.1407 0.3697 0.0728 scaling symmetry: f depends on m1 / m2 [error 0.00634] .. GENERATED FROM PYTHON SOURCE LINES 46-48 Parameter study: surrogate quality and the scaling test ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-60 .. code-block:: Python sizes = [100, 200, 400, 800] err = [] for n in sizes: fs = fit_surrogate(prob.X_train[:n], prob.y_train[:n], random_state=0) s = symmetry_tests(fs, prob.X_train[:n], names) err.append(float(s.loc[s.pair == "m1, m2", "scaling"].iloc[0])) print("surrogate samples", sizes, "scaling-test error (m1, m2)", np.round(err, 4).tolist()) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.plot(sizes, err, "o-", color="#285780") ax.set_xlabel("samples for the surrogate") ax.set_ylabel("scaling-test error for (m1, m2)") plt.show() .. image-sg:: /auto_examples/08_knowledge/images/sphx_glr_plot_31_symmetry_002.png :alt: plot 31 symmetry :srcset: /auto_examples/08_knowledge/images/sphx_glr_plot_31_symmetry_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none surrogate samples [100, 200, 400, 800] scaling-test error (m1, m2) [0.0139, 0.0067, 0.0046, 0.0063] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 18.404 seconds) .. _sphx_glr_download_auto_examples_08_knowledge_plot_31_symmetry.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/08_knowledge/plot_31_symmetry.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_31_symmetry.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_31_symmetry.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_31_symmetry.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_