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 α\alpha 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 σ\sigma (how much they disagree). For a Gaussian, the bell curve, mean ±2σ\pm 2\sigma 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 function k(x,x′)k(x, x') that gives the covariance, under the prior, of the outputs at two designs xx and x′x': how strongly they are related, often from their distance.

A common one, the squared-exponential or RBF kernel, has a length scale ℓ\ell: designs much closer than ℓ\ell have nearly equal outputs, designs much farther apart are unrelated.

k(x,x′)=σf2exp⁡ ⁣(−∥x−x′∥22ℓ2)k(x, x') = \sigma_f^2 \exp\!\left( -\frac{\lVert x - x' \rVert^2}{2\ell^2} \right)
(3-4)
σf2\sigma_f^2: the prior variance of the output. ℓ\ell: the length scale, one per input in practice.
-2-10120246810design variable xoutput y
runsGP meanmean ± 2σtrue function

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.

Figure 3-2. A Gaussian process through a few runs of a toy function (not the aircraft’s solver): the mean and the band of mean ±2σ\pm 2\sigma.

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 ℓ\ell, σf\sigma_f 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 σ\sigma 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 O(N3)O(N^3) time and O(N2)O(N^2) memory in the number of runs NN, 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 α\alpha 4°, and one outside it, at 12°.

Example 3-4. A Gaussian process for the lift coefficient, with scikit-learn
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})")
  1. The factor 1.0 is σf2\sigma_f^2 of Equation 3-4, and the list gives one length scale per input, so α\alpha and sweep can vary on different scales. Both are only starting values.
  2. WhiteKernel is the noise term. "fixed" keeps it at 10−610^{-6} 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)) and fit() tunes it.
  3. normalize_y=True normalises the outputs (subtracts their mean, divides by their standard deviation) before the fit. n_restarts_optimizer=3 maximises the marginal likelihood from three more random starting points, since it can have several maxima.
  4. return_std=True returns σ\sigma 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 α\alpha 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 α\alpha and 77° in sweep, are much longer than the box: across it, CLC_L 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 CpC_p.

Check yourself

  1. 1A GP with an RBF kernel and a length scale of 2° in α is fitted to runs with α from −2° to 8°. It is queried at α = 14°, three length scales past the last run. What comes back?
  2. 2With both inputs normalised to the same range, the fitted length scale for sweep comes out much longer than the one for α. What does that mean?
  3. 3A dataset grows from 2,000 to 20,000 runs. Roughly how much longer does an exact GP take to fit, and how much more memory does it need?

Exercises

  1. Exercise 3-4. With the GP of the lesson’s listing, check for each of the 12 test runs whether the solver’s CL falls inside mean ± 2σ. How many do, inside the box and outside?

    Answer

    All 12: the 4 inside and the 8 outside. The band runs from ± 0.0005 inside to ± 0.0027 at α 12°, sweep 40°. Twelve runs are a small sample for a 95 % band; calibration is checked in the uncertainty lesson.

  2. Exercise 3-5. Pass optimizer=None so that fit() keeps the given length scales, then try RBF(length_scale=[0.5, 2.0]) and [30.0, 100.0], and watch the ± 2σ at both test runs.

    Answer

    With [0.5, 2.0] the runs look unrelated: 0.4488 ± 0.3128 at α 4° and 0.3922 ± 0.4335 at 12°, where the solver’s 1.0816 lies outside even that wide band. With [30.0, 100.0]: 0.4566 ± 0.0005 and 1.0752 ± 0.0023, a band too narrow to hold 1.0816. Neither fixed choice covers the solver at 12°; the fitted length scales do.

Go further