Visual explainer
Polynomial Regression
Squared and cubed columns let a linear solver draw curves, until extra degrees wobble.
When you fit a first-degree linear regression line through non-linear data, the errors do not scatter randomly. In a diagnostic residual plot ( versus ), the residuals trace a distinct, systematic U-shaped arc:
This non-random residual curvature is proof of structural model underfitting. The hypothesis space is too rigid to express the underlying curvature of the data-generating process. Gathering more training rows cannot resolve this failure; the feature space itself must be expanded.
Square Your Inputs
Polynomial regression bends the hypothesis curve without altering the linear solver. By applying a non-linear basis expansion , a scalar input is mapped into higher-order powers:
For an input scalar , the expanded feature vector evaluates to . The regression model computes:
While the relationship between and is non-linear and curved, the model remains strictly linear in its parameters . Consequently, the exact closed-form Ordinary Least Squares (OLS) solver remains fully intact:
Parabola Nails Three
Consider a data-generating function governed by observed at three sample points: , , and . Constructing the Vandermonde matrix and solving the normal equations yields exact parameter weights:
Evaluating the fitted parabola at each knot:
The training mean squared error collapses to exactly . The linear solver has drawn a curved quadratic manifold through every point without requiring iterative numerical optimization.
Degrees Wobble Wildly
Increasing polynomial order expands model capacity, but unconstrained flexibility introduces severe instability. Across a ten-point dataset:
- Degree 1 (Stiff): Underfits, missing the true arc ().
- Degree 2 (Glides): Matches the true quadratic signal, smoothing out random noise ().
- Degree 9 (Waves): Threads every single training point with zero training error (), but oscillates violently between the knots.
This oscillatory pathology is known as Runge's phenomenon. Near the boundaries and sample intervals, high-degree polynomial derivatives swing wildly, turning interpolation into high-variance noise.
Sweet Degree Wins
Evaluating candidate polynomial degrees on an untouched held-out test set reveals the empirical cost of excess capacity:
Degree 2 achieves a error reduction over the rigid linear model (). Conversely, degree 9 suffers a error explosion (). Despite achieving a perfect training score of , degree 9 collapses on fresh test data because its oscillatory excursions fail to generalize.
Pick Smallest Winner
Model complexity selection should follow the validation elbow criterion governed by Occam's razor:
- The Validation Elbow: Trace validation loss across degree . Validation loss drops steeply from () to (), where marginal error gains flatten out ( yields ).
- Occam's Tiebreaker: When two candidate degrees yield nearly identical validation risk, always select the smaller degree ().
- Conditioning Safeguard: Higher powers of severely inflate the condition number , causing collinearity and numeric instability in matrix inversion.
Where It Breaks
Polynomial models are strictly local interpolators. Outside the training domain , the leading power term dominates the function's asymptotic behavior:
When evaluating a degree-9 model at beyond a training horizon of , the true physical trend continues a gentle linear drift (). However, the polynomial rockets vertically to , creating a catastrophic error shock. Never extrapolate high-degree polynomials beyond their observed domain boundaries; clamp inputs or transition to splines with linear boundary constraints.
The Quick Version
- Curved residual plots prove that linear models suffer from missing non-linear shape.
- Non-linear basis expansion maps into while preserving OLS linearity in .
- A degree-2 parabola fits points with zero residual error.
- Unconstrained degree-9 polynomials suffer from Runge's phenomenon, oscillating wildly between dots.
- Degree 2 achieves the optimal test error () while degree 9 explodes ().
- Choose the smallest degree at the validation curve elbow following Occam's razor.
- Never extrapolate polynomials beyond their training boundaries as higher powers rocket to infinity.