Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Sensitivity analysis of a discovered model#
An explicit formula can be probed exactly. On the Friedman-1 benchmark, \(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 \(x_0\) and \(x_1\).
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)
The model and its sensitivities#
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)
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
<Figure size 960x520 with 6 Axes>
Parameter study: sample size of the Sobol estimate#
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()

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]
Total running time of the script: (0 minutes 3.497 seconds)

