.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/09_analysis/plot_34_sensitivity.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_09_analysis_plot_34_sensitivity.py: Sensitivity analysis of a discovered model ========================================== An explicit formula can be probed exactly. On the Friedman-1 benchmark, :math:`10\sin(\pi x_0x_1)+20(x_2-0.5)^2+10x_3+5x_4` plus five irrelevant inputs, a multi-gene model is identified and analysed: partial dependence curves, derivative-based sensitivities, and variance-based Sobol indices (Saltelli et al., 2010; Jansen, 1999), whose gap between total and first-order index reveals the interaction of :math:`x_0` and :math:`x_1`. .. 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.analysis import local_sensitivity, partial_derivatives, sobol_indices prob = eq.datasets.get_problem("friedman-1", n_train=400) .. GENERATED FROM PYTHON SOURCE LINES 22-24 The model and its sensitivities ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-37 .. code-block:: Python model = eq.MultiGeneRegressor(population_size=300, generations=40, max_genes=5, functions=("add", "sub", "mul", "sin", "square"), random_state=0) model.fit(prob.X_train, prob.y_train) eqn = model.equation_ print(eqn, "| test R2", round(eqn.score(prob.X_test, prob.y_test), 4)) print(partial_derivatives(eqn).get("x3")) S = sobol_indices(eqn, bounds=[(0, 1)] * 10, n=8192) print(S.round(3)) fig, axs = plt.subplots(1, 2, figsize=(10, 3.6)) eq.viz.plot_sobol(S, ax=axs[0]) eq.viz.plot_sensitivity(local_sensitivity(eqn, prob.X_test), ax=axs[1]) fig.tight_layout() eq.viz.plot_partial_dependence(eqn, prob.X_test, features=["x0", "x1", "x2", "x3", "x4", "x5"], ncols=3) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/09_analysis/images/sphx_glr_plot_34_sensitivity_001.png :alt: plot 34 sensitivity :srcset: /auto_examples/09_analysis/images/sphx_glr_plot_34_sensitivity_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/09_analysis/images/sphx_glr_plot_34_sensitivity_002.png :alt: plot 34 sensitivity :srcset: /auto_examples/09_analysis/images/sphx_glr_plot_34_sensitivity_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none 149.6 - 6.582 * ((x0 + 3.834)^2 - sin(x0^2 + x1)) - 6.113 * (sin(x0 + 3.834) - (2.733 + x3 + sin(x2 * (-3.065)))) + 35.37 * (x4 + x1) + 30.9 * (sin(sin(x1)) + sin(x0) + x0 + x3 - x4) - 27.73 * (2.65 + x3 + x1 + x1) | test R2 0.912 9.28697035330208 variable S1 ST 0 x0 0.175 0.208 1 x1 0.247 0.257 2 x2 0.145 0.146 3 x3 0.332 0.318 4 x4 0.076 0.072 5 x5 0.000 0.000 6 x6 0.000 0.000 7 x7 0.000 0.000 8 x8 0.000 0.000 9 x9 0.000 0.000
.. GENERATED FROM PYTHON SOURCE LINES 38-40 Parameter study: sample size of the Sobol estimate -------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-49 .. code-block:: Python ns = [256, 1024, 4096, 16384] est = np.array([sobol_indices(eqn, bounds=[(0, 1)] * 10, n=n, random_state=s).set_index("variable").loc["x0", "ST"] for n in ns for s in range(5)]).reshape(len(ns), 5) print("n", ns, "ST of x0: mean", np.round(est.mean(1), 3).tolist(), "sd", np.round(est.std(1), 3).tolist()) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.errorbar(ns, est.mean(1), yerr=est.std(1), fmt="o-", color="#285780", capsize=4) ax.set_xscale("log") ax.set_xlabel("base sample size n") ax.set_ylabel("total index of $x_0$ (mean ± sd, 5 seeds)") plt.show() .. image-sg:: /auto_examples/09_analysis/images/sphx_glr_plot_34_sensitivity_003.png :alt: plot 34 sensitivity :srcset: /auto_examples/09_analysis/images/sphx_glr_plot_34_sensitivity_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none n [256, 1024, 4096, 16384] ST of x0: mean [0.213, 0.212, 0.211, 0.213] sd [0.015, 0.009, 0.003, 0.003] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.497 seconds) .. _sphx_glr_download_auto_examples_09_analysis_plot_34_sensitivity.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/09_analysis/plot_34_sensitivity.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_34_sensitivity.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_34_sensitivity.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_34_sensitivity.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_