Gaussian processes
A smooth fit that also estimates how unsure it is, usually more so away from the data.
Outcome: Explain a Gaussian process as a distribution over smooth functions; read the kernel, the length scale and the noise term; fit one with scikit-learn and plot the mean ± 2σ.
Builds on Measuring error
Example 3-1 answers at 12° as readily as at 4°, and nothing in its output says that 12° lies outside its runs. A Gaussian process fitted to the same 36 runs answers 1.0812 ± 0.0018 at 12° and 0.4566 ± 0.0005 at 4° (mean ± two standard deviations, Example 3-4): its band is about four times wider outside the box.
A polynomial commits to one curve. A Gaussian process (GP) starts from a whole family of smooth curves, a probability distribution over functions called the prior, and keeps those consistent with the runs, called the posterior. At every new design it returns the mean of the remaining curves (the prediction) and their standard deviation (how much they disagree). For a Gaussian, the bell curve, mean covers about 95 %. In geostatistics, where it became an engineering tool, the same method is called kriging. The standard text is Rasmussen and Williams 2006.
What “smooth” means is set by the kernel, in a GP the covariance function (convolutions and neural operators use the word for other objects).
DefinitionKernel
A common one, the squared-exponential or RBF kernel, has a length scale : designs much closer than have nearly equal outputs, designs much farther apart are unrelated.
The band closes at each run and widens away from them; click to add a run. 3 runs, largest uncertainty ±1.52 at x = 6.3, the thin line.
In Figure 3-2 the band collapses at each run and widens between them. Beyond the last run, the mean drifts back towards the prior and the band widens towards its prior size: with this kernel, the band shows it is far from the data. A short length scale makes the curve wiggly and the uncertainty grow quickly away from data; a long one does the opposite. In practice , and a noise term (for solver or mesh noise, so the curve need not pass exactly through every run) are fitted automatically by maximising the marginal likelihood, how probable the runs are under the prior with those values.
The button that adds a run where is largest is the simplest form of active learning: the surrogate chooses the next simulation where it is least sure.
GPs suit scalar outputs, few inputs and few runs. On inputs: Bayesian optimisation, a design search that usually rests on GPs, is “best-suited” to under 20 dimensions (Frazier 2018). On runs: exact GPs cost time and memory in the number of runs , so 10,000 runs are already heavy.
The GP on the aircraft
Example 3-4 fits a GP to the lift coefficient of the 36 training runs of the download, then predicts two test runs at sweep 28°: one inside the training box, at 4°, and one outside it, at 12°.
import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, WhiteKernel
from aircraft import load_runs, split_runs
d = load_runs()
train, val, test, in_box = split_runs(d)
X = np.column_stack([d["alpha"], d["sweep"]])
y = d["CL"]
kernel = 1.0 * RBF(length_scale=[3.0, 10.0])1
kernel += WhiteKernel(1e-6, "fixed")2
gp = GaussianProcessRegressor(kernel=kernel, normalize_y=True,3
n_restarts_optimizer=3,
random_state=0)
# fit() tunes the variance and the length scales.
gp.fit(X[train], y[train])
print(gp.kernel_)
# Two test runs at sweep 28: alpha 4 (inside), 12 (outside).
for i in test[[2, 6]]:
m, s = gp.predict(X[[i]], return_std=True)4
print(f"alpha {X[i, 0]:4.1f}: {m[0]:.4f} ± {2 * s[0]:.4f}"
f" (solver {y[i]:.4f})")
- The factor 1.0 is of Equation 3-4, and the list gives one length scale per input, so and sweep can vary on different scales. Both are only starting values.
WhiteKernelis the noise term."fixed"keeps it at instead of fitting it: this solver is deterministic, and a fitted noise level runs to its lower bound, with a warning. The small fixed value keeps the fit numerically stable. With a noisy solver it is left free (WhiteKernel(1e-4)) andfit()tunes it.normalize_y=Truenormalises the outputs (subtracts their mean, divides by their standard deviation) before the fit.n_restarts_optimizer=3maximises the marginal likelihood from three more random starting points, since it can have several maxima.return_std=Truereturns next to the mean, here including the fixed noise.
Example 3-4 prints the fitted kernel, 13**2 * RBF(length_scale=[87.8, 76.7]) + WhiteKernel(noise_level=1e-06), then 0.4566 ± 0.0005 at 4° (solver 0.4566) and 1.0812 ± 0.0018 at 12° (solver 1.0816). Both solver values lie inside the band. The fitted length scales, about 88° in and 77° in sweep, are much longer than the box: across it, varies smoothly, with little curvature.
A plain GP predicts one number at a time. Combined with a compressed description of the field, it predicts a whole field: the next lesson, where Example 3-5 does this on the aircraft’s .