Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Bootstrap ensembles: model uncertainty and stable structure#
Refitting the search on bootstrap samples of the data (Breiman, 1996) shows which
parts of the model the data really support. SymbolicEnsemble
aggregates the members’ predictions, reports their spread – largest where the
data are sparse – and counts how often each variable and building block appears.
import matplotlib.pyplot as plt
import numpy as np
import eqdisco as eq
rng = np.random.default_rng(2)
x = np.concatenate([rng.uniform(0, 2, 50), rng.uniform(3.5, 4, 8)]) # a gap in the data
z = rng.uniform(0, 1, len(x)) # irrelevant input
y = x * np.exp(-0.5 * x) + 0.03 * rng.normal(size=len(x))
X = np.column_stack([x, z])
Ten bootstrap members#
ens = eq.SymbolicEnsemble(eq.SymbolicRegressor(population_size=200, generations=20, functions=("add", "sub", "mul", "exp")), n_estimators=10, feature_names=["x", "z"], random_state=0)
ens.fit(X, y)
print(ens.variable_frequency())
print(ens.structure_frequency(min_support=0.4).head())
xs = np.linspace(0, 5, 300)
Xs = np.column_stack([xs, np.full_like(xs, 0.5)])
lo, hi = ens.predict_interval(Xs, coverage=0.9)
fig, axs = plt.subplots(1, 2, figsize=(10, 3.4))
for e in ens.equations_:
axs[0].plot(xs, e.predict(Xs), color="#0072B2", lw=0.6, alpha=0.4)
axs[0].fill_between(xs, lo, hi, color="#0072B2", alpha=0.15, label="90 % of the members")
axs[0].plot(xs, ens.predict(Xs), color="#285780", lw=2, label="median of the ensemble")
axs[0].scatter(x, y, s=10, color="#6B7785", label="data")
axs[0].set_xlabel("x")
axs[0].legend(frameon=False, fontsize=8)
vf = ens.variable_frequency()
axs[1].bar(vf.index, vf.to_numpy(), color="#285780")
axs[1].set_ylabel("share of members using the variable")
fig.tight_layout()

x 1.0
z 0.4
Name: frequency, dtype: float64
block size support rel_support
0 add(x,x) 3 6 0.6
1 mul(c,x) 3 6 0.6
2 add(c,x) 3 4 0.4
Parameter study: number of members#
ns = [3, 5, 10, 20]
width = []
for n in ns:
e = eq.SymbolicEnsemble(eq.SymbolicRegressor(population_size=120, generations=12, functions=("add", "sub", "mul", "exp")), n_estimators=n, random_state=1).fit(X, y)
a, b = e.predict_interval(Xs)
width.append(float(np.mean(b[(xs > 2) & (xs < 3.5)] - a[(xs > 2) & (xs < 3.5)])))
print("members", ns, "mean interval width in 2 < x < 3.5", np.round(width, 3).tolist())
fig, ax = plt.subplots(figsize=(4.5, 3))
ax.plot(ns, width, "o-", color="#285780")
ax.set_xlabel("ensemble members")
ax.set_ylabel("mean band width in the gap")
plt.show()

members [3, 5, 10, 20] mean interval width in 2 < x < 3.5 [0.075, 0.078, 0.076, 0.092]
Total running time of the script: (0 minutes 13.766 seconds)