Fitting a response surface

A surrogate is a function with parameters, chosen so its predictions match the data.

Outcome: Write a model as a function with parameters; fit it by least squares, and read the residuals; fit a polynomial response surface with scikit-learn.

Builds on Sampling the design space

The simplest case: a few design parameters in, one number out, such as the lift coefficient CLC_L from the angle of attack α\alpha and the sweep Λ\Lambda. A model is a formula with parameters (also called weights) that can be chosen freely. A plane is one such formula: y^=w0+w1α+w2Λ\hat{y} = w_0 + w_1 \alpha + w_2 \Lambda, with three weights. The hat on y^\hat{y} marks a prediction, as opposed to the solver’s value yy.

More features computed from the inputs bend the plane into a curved surface: α2,αΛ,Λ2\alpha^2, \alpha\Lambda, \Lambda^2. The model is still a weighted sum, y^=w⋅ϕ(x)\hat{y} = w \cdot \phi(x), where x=(α,Λ)x = (\alpha, \Lambda) is the design and ϕ(x)=(1,α,Λ,α2,αΛ,Λ2)\phi(x) = (1, \alpha, \Lambda, \alpha^2, \alpha\Lambda, \Lambda^2) is the list of features, the constant 1 included.

The best weights are usually defined as those that make the average squared difference between prediction and data as small as possible. That average is the loss, here the mean squared error:

L(w)=1N∑i=1N(yi−w⋅ϕ(xi))2L(w) = \frac{1}{N} \sum_{i=1}^{N} \bigl( y_i - w \cdot \phi(x_i) \bigr)^2
(3-1)
NN runs, yiy_i the solver’s result for run ii, ϕ(xi)\phi(x_i) its features, ww the weights to choose.

Choosing the weights this way is least squares. Because the model is linear in the weights, the best ones come from solving one linear system, with no search. A neural network minimises the same kind of loss, but it is not linear in its weights, so it must search step by step (see loss and gradient descent).

Residuals and response surfaces

The differences yi−y^iy_i - \hat{y}_i are the residuals. Plotted against each input, they look like noise when the model has captured the trend, and rise and fall smoothly when the model is too simple for the data.

A polynomial fitted this way is a response surface.

DefinitionResponse surface

A polynomial in the inputs, fitted by least squares: the classical surrogate of engineering design since Box and Wilson’s work on experimental design in 1951.

It is still a sensible first model for a handful of parameters and smooth scalar outputs. For the site’s aircraft at Λ\Lambda 28°, the solver’s lift coefficient is 0.140 at α\alpha 0°, 0.457 at 4° and 0.771 at 8°: almost a straight line, as lifting-line theory predicts for attached flow at small angles. One number per design cannot describe a pressure field.

In scikit-learn most models follow the same pattern: create it, call fit(X, y) on the training data, then predict(X_new). A pipeline chains the feature step and the regression into one model. Example 3-1 fits a six-weight quadratic in (α,Λ)(\alpha, \Lambda) to the 36 training runs of the download, then measures its error on the 12 test runs.

Example 3-1. A quadratic response surface for the lift coefficient, with scikit-learn
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from aircraft import load_runs

d = load_runs()
X = np.column_stack([d["alpha"], d["sweep"]])  # (56, 2)
y = d["CL"]
train = d["split"] == 0  # the 36 training runs
test = d["split"] == 2   # the 12 test runs
in_box = (X[:, 0] >= -2) & (X[:, 0] <= 8)1
in_box &= (X[:, 1] >= 20) & (X[:, 1] <= 36)

model = make_pipeline(
    PolynomialFeatures(degree=2, include_bias=False),2
    LinearRegression())
# Least squares on the training runs only.
model.fit(X[train], y[train])3
res = y[train] - model.predict(X[train])
print(f"training RMSE {np.sqrt(np.mean(res**2)):.4f}")

# The largest error on the test runs, inside and outside.
err = np.abs(model.predict(X) - y)
print(f"test, inside the box  {err[test & in_box].max():.3f}")
print(f"test, outside the box {err[test & ~in_box].max():.3f}")
# The six fitted weights: w0, then w1..w5.
print(model[-1].intercept_.round(4), model[-1].coef_.round(4))4
  1. The training box: α\alpha from −2° to 8°, Λ\Lambda from 20° to 36°. Four of the 12 test runs lie inside it, eight outside.
  2. PolynomialFeatures turns (α,Λ)(\alpha, \Lambda) into the five features α,Λ,α2,αΛ,Λ2\alpha, \Lambda, \alpha^2, \alpha\Lambda, \Lambda^2. include_bias=False leaves out the constant column, because LinearRegression fits the intercept w0w_0 itself.
  3. fit runs both steps on the training runs only: it builds the features, then solves the least-squares problem of Equation 3-1 directly, with no iterations.
  4. model[-1] is the last step of the pipeline, the regression, which holds the weights.

Example 3-1 prints a training root mean squared error (RMSE), the square root of the loss of Equation 3-1, of 0.0007. On the test runs the largest error in CLC_L is 0.001 inside the training box and 0.015 outside it: more than ten times larger. Its last line prints the six weights: the intercept 0.1086, then 0.0906, 0.003, −0.0001, −0.0004 and −0.0001 for α,Λ,α2,αΛ,Λ2\alpha, \Lambda, \alpha^2, \alpha\Lambda, \Lambda^2.

A higher degree can always fit the training runs at least as well, and eventually passes through every one of them, while between and beyond them it can oscillate strongly. The training error therefore cannot select the degree, and the test runs, which only report the final error, cannot either: the choice needs runs of its own.

Check yourself

  1. 1ŷ = w₀ + w₁α + w₂Λ + w₃α² + w₄αΛ + w₅Λ². Can least squares give the best weights in one step, or is gradient descent needed?
  2. 2A plane is fitted to CL(α, Λ). Plotted against α, the residuals form a smooth arch. What does that indicate?
  3. 3A full quadratic response surface in two inputs, α and Λ. How many weights does it have?

Exercises

  1. Exercise 3-1. In the lesson’s listing, fit degrees 1, 2, 3 and 5 instead of 2. Report the training RMSE and the largest error on the 4 test runs inside the box and the 8 outside. Which degree should be kept, and on what evidence?

    Answer

    Degree 1: training RMSE 0.0059, largest test error 0.010 inside and 0.071 outside. Degree 2: 0.0007, 0.001 and 0.015. Degree 3: training RMSE below 0.0001, largest test error 0.000 inside (to three decimals) and 0.001 outside. Degree 5: 0.0026, 0.004 and 0.294; its training error rises only because the raw degrees make an ill-conditioned problem (sweep⁵ is about 6 × 10⁷), which normalising the inputs fixes. Degree 3, but the evidence belongs to validation runs, not to the test runs used here: choosing on them turns them into validation runs (next lesson).

Go further