Foundations to Frontier
Tests
D. Classical Machine Learning › D1 Linear Models for Regression

Polynomial Regression

Polynomial regression fits curved relationships with the same machinery as linear regression: expand each input into powers and products (x, x², x³, x₁x₂, …) and run least squares on these new features. The model is linear in its coefficients but non-linear in the inputs. This page covers feature construction for one and several inputs, choosing the degree by cross-validation, numerical conditioning (orthogonal polynomial bases), Runge's phenomenon, and the central weakness: polynomials extrapolate wildly. In the exercises you build polynomial features, fit with a Legendre basis, choose the degree by cross-validation and reproduce Runge's phenomenon; then you measure interpolation and extrapolation errors of polynomials of each degree against a physics-based model for aerodynamic drag and a saturating thrust curve.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Bending the ruler

A straight ruler can’t follow a curved path. A bendy one can: allow one bend (a curve like a bowl), two bends, and so on. That is polynomial regression. More bends fit the dots better, but too many bends wiggle through every noisy dot, and past the last dot the curve can fly off in any direction.

Polynomial fits inside and outside the data
truth:
data rangeextrapolation
training RMSE 0.129 · error vs truth inside 0.161 · beyond the data (1–1.4) 5.08
Training error always falls as the degree rises, but high degrees chase the noise and shoot off just outside the shaded range. Even a well-chosen degree has the wrong shape for a curve that levels off, so it extrapolates badly: the shape of the model, not just its fit, decides what happens beyond the data.
Why engineers care

Calibration curves, thrust maps and empirical drag polars are often polynomials, and they are safe only inside the tested range. Using such a curve beyond the test data, say at a higher speed or temperature, is a classic source of engineering error.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Polynomial regression can fit…
  2. 2. Too high a degree tends to…
  3. 3. In the demo, beyond the data the curve…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

A worked example by hand

Points (x, y): (−1, 2), (0, 1), (1, 2), (2, 5). Try y = c₀ + c₁x + c₂x².

The feature table has columns 1, x, x²:

1 x x² y
1 −1 1 2
1 0 0 1
1 1 1 2
1 2 4 5

Least squares on these three columns gives c = (1, 0, 1), that is y = 1 + x², which passes through all four points.

The steps in plain English:

  1. Choose a degree d.
  2. Add columns x², …, xᵈ (and products like x₁x₂ for several inputs).
  3. Run ordinary least squares.
  4. Choose d by validation error, not training error.

With p inputs, the number of features up to degree d is C(p + d, d). It explodes: 20 inputs at degree 3 already give 1 771 features.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Polynomial regression is linear in…
  2. 2. Features up to degree 2 for 2 inputs (with constant)?
  3. 3. The degree should be chosen by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Features, conditioning and selection

Model: y = Σⱼ cⱼ φⱼ(x) + ε, where the φⱼ are monomials up to degree d. Fit by least squares (D1.2).

Conditioning. Raw powers on [0, 1] or far from 0 are nearly collinear (Vandermonde matrices are notoriously ill-conditioned, D1.2). Use:

  • centred and scaled inputs (map to [−1, 1]);
  • orthogonal polynomial bases (Legendre, Chebyshev);
  • or a QR solver.

The fitted curve is the same; the coefficients are stable.

Degree selection. Use cross-validation, the one-standard-error rule, or regularisation (ridge on many polynomial features, D1.5).

Runge’s phenomenon. Interpolating a smooth function at equally spaced points with a high-degree polynomial oscillates wildly near the ends. Chebyshev nodes, which cluster near the ends, avoid it.

from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import Ridge
model = make_pipeline(StandardScaler(), PolynomialFeatures(degree=4), Ridge(alpha=1e-3)).fit(X, y)
import numpy as np; c = np.polynomial.Polynomial.fit(x, y, deg=4)   # 1-D, internally scaled

Hyperparameters: degree d, regularisation strength, interaction-only option. Use it for smooth, low-dimensional relationships within the data range. Avoid it for extrapolation, many inputs (feature explosion), and local features (use splines, D1.20, or trees).

🐍 Exercise 1: polynomial features, degree selection and Runge's phenomenon
Build multivariate polynomial features and count them; fit polynomials in a well-conditioned Legendre basis; choose the degree by 5-fold cross-validation for a noisy sine; interpolate Runge's function at 21 equispaced and 21 Chebyshev nodes and compare the maximum errors.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why use Legendre or Chebyshev bases?
  2. 2. Runge's phenomenon appears with…
  3. 3. Features for 10 inputs up to degree 3?

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory and alternatives

Approximation. By the Weierstrass theorem, polynomials can approximate any continuous function on a closed interval. But the degree needed may be large, and the error of interpolation at nodes x₀…x_d is f⁽ᵈ⁺¹⁾(ξ)/(d + 1)! · Π(x − xᵢ). Chebyshev nodes minimise the product term, hence their stability.

Extrapolation. Outside the data, a degree-d polynomial grows like |x|ᵈ, governed by its leading coefficient, which the data barely determine. Variance explodes with distance from the data, so prediction intervals widen polynomially.

Complexity. Fitting costs O(n·m²) with m = C(p + d, d) features. Feature count, not n, usually limits multivariate polynomials.

Alternatives:

  • splines and GAMs (D1.20): local flexibility, linear tails;
  • kernel ridge with a polynomial kernel (D5.54): implicit features;
  • physics-based models with few parameters: the best extrapolators when the physics is right;
  • trees and boosting for high dimensions.

Interview questions.

  • Why is polynomial regression still “linear regression”? The model is linear in the coefficients; non-linearity is in the fixed features.
  • How do you prevent a polynomial from overfitting? Choose the degree by cross-validation, add ridge regularisation, or switch to splines.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Beyond the data, a degree-d polynomial behaves like…
  2. 2. Best extrapolator when the physics is known:
  3. 3. Splines avoid Runge oscillations because…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Inside versus outside the data

History. Gergonne designed polynomial regression experiments in 1815. Runge (1901) showed that higher-degree interpolation can diverge. Chebyshev’s polynomials, from the 1850s, give near-optimal approximation nodes, and Trefethen’s Approximation Theory and Approximation Practice (2013) is the modern account.

Exercise 1.

  • Two inputs at degree 2 give 6 features; 10 inputs at degree 3 give 286.
  • Cross-validation chooses degree 7 for a noisy sine.
  • Interpolating Runge’s function with 21 equispaced nodes gives a maximum error of 59.8. Chebyshev nodes give 0.015.

Exercise 2: drag and thrust beyond the tested range. Setup: 40 noisy measurements (5% noise) at 5–30 m/s; predictions at 5–30 m/s (“inside”) and 30–45 m/s (“outside”); 200 datasets. The table reports median relative error:

Truth Model Inside Outside Outside, 90th percentile
drag ∝ v² physics model a·v² (1 parameter) 0.8% 0.8% 1.9%
degree 1 12.8% 24.6% 27.9%
degree 2 1.2% 1.9% 4.6%
degree 3 1.4% 5.7% 13.2%
degree 5 1.6% 51% 125%
degree 8 2.0% 1 360% 4 018%
saturating thrust degree 2 1.1% 14.4% 20.7%
degree 3 1.1% 12.2% 24.8%
degree 8 1.7% 2 775% 7 269%
  • Every degree from 2 to 8 is accurate inside the range (1–2%).
  • Outside, the errors differ by three orders of magnitude. The one-parameter physics model extrapolates as well as it interpolates.
  • For a curve that levels off, even the best-looking low-degree polynomial is 12–14% wrong just beyond the data: it has the wrong shape.

Cross-validation inside the range cannot detect any of this. Domain knowledge must.

🐍 Exercise 2 (research): interpolation versus extrapolation
Fit polynomials of degree 1–8 and a one-parameter physics model to noisy drag measurements at 5–30 m/s, and polynomials to a saturating thrust curve; compare median relative errors inside the measured range and at 30–45 m/s over 200 datasets.

Polynomial models in AI, robotics and aerospace

  • Drag polars: C_D = C_D0 + k·C_L², a physics-shaped quadratic.
  • Sensor calibration: thermocouple and strain-gauge polynomials (NIST ITS-90 reference functions), each valid only in a stated range.
  • Engine and motor maps: polynomial surfaces fitted to test-bench data.
  • Feature expansion: polynomial features for linear and kernel models.

References

  1. C. Runge (1901). “Über empirische Funktionen und die Interpolation zwischen äquidistanten Ordinaten.” Zeitschrift für Mathematik und Physik 46.
  2. J. D. Gergonne (1815/1974). “The application of the method of least squares to the interpolation of sequences.” Historia Mathematica 1(4) (translation).
  3. L. N. Trefethen (2013). Approximation Theory and Approximation Practice. SIAM.
  4. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., chapter 5. Springer.
  5. NIST (1993). Temperature-Electromotive Force Reference Functions and Tables for the Letter-Designated Thermocouple Types Based on the ITS-90. Monograph 175.