.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_dynamics/plot_25_symbolic_ode.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_25_symbolic_ode.py: Differential equations by genetic programming ============================================= When the right-hand side is not a sparse combination of library terms, any symbolic regressor can be fitted to the estimated derivatives, one state at a time (:class:`~eqdisco.SymbolicODE`). The predator--prey model of Lotka and Volterra, :math:`\dot x=ax-bxy`, :math:`\dot y=dxy-cy`, is identified from a noisy trajectory: the derivatives are estimated by a smoothing spline, the equations by genetic programming with parameter optimization. .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq from eqdisco.datasets import lotka_volterra t, Z = lotka_volterra(T=30, dt=0.05, noise=0.005) .. GENERATED FROM PYTHON SOURCE LINES 22-24 Identification -------------- .. GENERATED FROM PYTHON SOURCE LINES 24-37 .. code-block:: Python gp = eq.SymbolicRegressor(population_size=250, generations=25, functions=("add", "sub", "mul"), optimize="lm", random_state=0) ode = eq.SymbolicODE(gp, derivative_method="spline", feature_names=["prey", "predator"]).fit(Z, t) print(ode.model_) Zs = ode.simulate(Z[0], t) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) for j, name in enumerate(["prey", "predator"]): axs[0].plot(t, Z[:, j], color=eq.viz.OKABE_ITO[j], lw=1, alpha=0.6, label=f"{name} (data)") axs[0].plot(t, Zs[:, j], color=eq.viz.OKABE_ITO[j], lw=1.5, ls="--", label=f"{name} (model)") axs[0].set_xlabel("time") axs[0].legend(frameon=False, fontsize=7, ncol=2) eq.viz.plot_phase_portrait(Z, Zs, ax=axs[1], names=["prey", "predator"]) fig.tight_layout() .. image-sg:: /auto_examples/06_dynamics/images/sphx_glr_plot_25_symbolic_ode_001.png :alt: plot 25 symbolic ode :srcset: /auto_examples/06_dynamics/images/sphx_glr_plot_25_symbolic_ode_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none dprey/dt = 0.0005015 - 0.2001 * (predator - 2.5) * (prey + prey) dpredator/dt = -0.0003407 - 0.0997 * predator * (4.004 - prey) .. GENERATED FROM PYTHON SOURCE LINES 38-40 Parameter study: derivative estimators under noise -------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-59 .. code-block:: Python from eqdisco.dynamics import derivative # noqa: E402 true = np.column_stack([1.0 * Z[:, 0] - 0.4 * Z[:, 0] * Z[:, 1], 0.1 * Z[:, 0] * Z[:, 1] - 0.4 * Z[:, 1]]) noises = [0.0, 0.005, 0.02, 0.05] fig, ax = plt.subplots(figsize=(4.8, 3.2)) for k, m in enumerate(("finite_difference", "savgol", "spline")): err = [] for nz in noises: tn, Zn = lotka_volterra(T=30, dt=0.05, noise=nz, random_state=2) _, Z0 = lotka_volterra(T=30, dt=0.05) tr = np.column_stack([1.0 * Z0[:, 0] - 0.4 * Z0[:, 0] * Z0[:, 1], 0.1 * Z0[:, 0] * Z0[:, 1] - 0.4 * Z0[:, 1]]) d = derivative(Zn, tn, m, window=15) err.append(np.sqrt(np.mean((d - tr) ** 2)) / np.std(tr)) print(m, "noise", noises, "relative RMS derivative error", np.round(err, 3).tolist()) ax.plot(noises, err, "o-", color=eq.viz.OKABE_ITO[k], label=m) ax.set_xlabel("noise level") ax.set_ylabel("relative RMS error of the derivative") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/06_dynamics/images/sphx_glr_plot_25_symbolic_ode_002.png :alt: plot 25 symbolic ode :srcset: /auto_examples/06_dynamics/images/sphx_glr_plot_25_symbolic_ode_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none finite_difference noise [0.0, 0.005, 0.02, 0.05] relative RMS derivative error [0.001, 0.089, 0.357, 0.892] savgol noise [0.0, 0.005, 0.02, 0.05] relative RMS derivative error [0.004, 0.02, 0.079, 0.196] spline noise [0.0, 0.005, 0.02, 0.05] relative RMS derivative error [0.001, 0.016, 0.038, 0.066] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.351 seconds) .. _sphx_glr_download_auto_examples_06_dynamics_plot_25_symbolic_ode.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_25_symbolic_ode.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_25_symbolic_ode.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_25_symbolic_ode.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_25_symbolic_ode.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_