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 from the angle of attack and the sweep . A model is a formula with parameters (also called weights) that can be chosen freely. A plane is one such formula: , with three weights. The hat on marks a prediction, as opposed to the solver’s value .
More features computed from the inputs bend the plane into a curved surface: . The model is still a weighted sum, , where is the design and 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:
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 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
It is still a sensible first model for a handful of parameters and smooth scalar outputs. For the site’s aircraft at 28°, the solver’s lift coefficient is 0.140 at 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 to the 36 training runs of the download, then measures its error on the 12 test runs.
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
- The training box: from −2° to 8°, from 20° to 36°. Four of the 12 test runs lie inside it, eight outside.
PolynomialFeaturesturns into the five features .include_bias=Falseleaves out the constant column, becauseLinearRegressionfits the intercept itself.fitruns 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.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 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 .
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.