MML204

Polynomial Regression

⏱7 min read👁️Eye-Care Reading Mode

What you'll learn

  • Why Polynomial Regression is classified as linear in parameters despite modeling curved relationships.
  • How to construct the Vandermonde Design Matrix X\mathbf{X} to map scalar inputs into higher-dimensional feature spaces.
  • Step-by-step first-principles derivation of the polynomial Normal Equations XTXβ^=XTy\mathbf{X}^T\mathbf{X}\hat{\boldsymbol{\beta}} = \mathbf{X}^T\mathbf{y}.
  • A complete, hand-calculated worked numerical example (n=4n=4) from raw coordinates to matrix inversion and zero-sum residual verification.
  • How interaction terms (X1X2X_1 X_2) capture synergistic rates of change through partial derivatives.
  • The combinatorial explosion of feature space ((p+dd)\binom{p+d}{d}) and why high degrees cause rank deficiency.
  • How Runge's Phenomenon creates catastrophic boundary oscillations in high-degree fits.
  • Why feature standardization (zz-score scaling) is mathematically mandatory before regularizing with Ridge (L2L_2) or Lasso (L1L_1).

Explained in detail

1. The Big Picture & Motivation

In standard Ordinary Least Squares (OLS) Linear Regression, we enforce a strict structural constraint: the relationship between predictor xx and continuous response yy must be a straight line:

y=β0+β1x+εε∼N(0,σ2)\begin{aligned} y &= \beta_0 + \beta_1 x + \varepsilon \\ \varepsilon &\sim \mathcal{N}(0, \sigma^2) \end{aligned}

This constraint dictates that the marginal rate of change is constant everywhere: ∂y∂x=β1\frac{\partial y}{\partial x} = \beta_1. Whether x=1x = 1 or x=1,000x = 1{,}000, a 1-unit increase in xx produces the exact same expected change in yy.

However, natural, physical, and socio-economic systems rarely follow rigid straight lines:

  • Kinetic Physics: The braking distance of an automobile grows quadratically with vehicle velocity (d∝v2d \propto v^2).
  • Economics: Diminishing marginal utility, where each additional dollar of capital yields progressively smaller marginal output.
  • Pharmacology: Dose-response curves, where drug efficacy rises rapidly, plateaus, and eventually becomes toxic at high dosages.
  • Education & Cognition: Exam scores versus study hours, where initial study hours provide substantial gains followed by fatigue and performance saturation.

When we fit a straight line to curved data, the model suffers from severe systematic bias (underfitting). The residuals display distinct, non-random U-shaped or cyclical patterns rather than uncorrelated white noise.

Comparison of Linear Regression vs Polynomial Regression on nonlinear data

💡

Core Motivation: Polynomial Regression broadens the hypothesis class of linear models to fit nonlinear curves by adding powers of original predictors (x2,x3,…,xdx^2, x^3, \dots, x^d) as new input features, while keeping the estimation problem entirely linear with respect to the unknown weights β\boldsymbol{\beta}.


2. What Exactly is a Polynomial?

A polynomial in a single real variable xx is a mathematical expression composed of variables and coefficients involving only addition, subtraction, multiplication, and non-negative integer exponents:

P(x)=β0+β1x+⋯+βdxd=∑j=0dβjxj\begin{aligned} P(x) &= \beta_0 + \beta_1 x + \dots + \beta_d x^d \\ &= \sum_{j=0}^d \beta_j x^j \end{aligned}

The highest exponent d∈N0d \in \mathbb{N}_0 associated with a non-zero coefficient βd≠0\beta_d \ne 0 is called the degree of the polynomial.

  • Degree 0 (Constant): y=β0y = \beta_0 — a horizontal flat line (0 turning points).
  • Degree 1 (Linear): y=β0+β1xy = \beta_0 + \beta_1 x — a sloped straight line (0 turning points).
  • Degree 2 (Quadratic): y=β0+β1x+β2x2y = \beta_0 + \beta_1 x + \beta_2 x^2 — a parabola with exactly 1 turning point (vertex / extremum).
  • Degree 3 (Cubic): y=β0+β1x+β2x2+β3x3y = \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 — an S-shaped curve with 1 inflection point and up to 2 turning points.
  • Degree 4 (Quartic): y=β0+β1x+⋯+β4x4y = \beta_0 + \beta_1 x + \dots + \beta_4 x^4 — a W or M-shaped curve with up to 3 turning points.

Geometric behavior of polynomial functions across degrees 1 to 4

The Flexible Ruler Analogy: Think of polynomial degree dd as the number of flexible joints in a carpenter's folding ruler. A ruler with 1 segment (degree 1) can only be laid flat as a straight line. Adding joints allows it to bend at 1 point (quadratic), 2 points (cubic), or wiggle through complex shapes. However, too many joints make the ruler floppy and hypersensitive to tiny bumps (overfitting).

A polynomial of degree dd can have at most d−1d - 1 turning points (local extrema where the first derivative dPdx=0\frac{dP}{dx} = 0). Increasing degree dd directly dials the mathematical flexibility of the curve.


3. The Fundamental Definition of Linearity in Machine Learning

A common point of confusion for students is why Polynomial Regression is universally classified as a linear model when its graphical prediction curve is obviously curved.

⚠️

The Golden Rule of Linearity: In statistical machine learning, a model is defined as linear if it is linear in its parameters (coefficients) β\boldsymbol{\beta}, regardless of how nonlinear the feature transformations ϕ(x)\phi(\mathbf{x}) may be!

Consider the univariate quadratic model:

y=β0+β1x+β2x2+εy = \beta_0 + \beta_1 x + \beta_2 x^2 + \varepsilon

Let us define synthesized feature variables:

z1=x,z2=x2z_1 = x, \qquad z_2 = x^2

Rewriting the equation with these transformed features yields:

y=β0+β1z1+β2z2+εy = \beta_0 + \beta_1 z_1 + \beta_2 z_2 + \varepsilon

This is identical to a standard multiple linear regression model with two predictor variables z1z_1 and z2z_2!

Because no parameter is multiplied by another parameter (β1β2\beta_1 \beta_2), raised to a power (β12\beta_1^2), or trapped inside a nonlinear function (eβ1xe^{\beta_1 x} or sin⁡(β1x)\sin(\beta_1 x)), the loss surface with respect to β\boldsymbol{\beta} remains a strictly convex quadratic bowl. Consequently, the global minimum can be computed in a single step using the analytical OLS normal equations.

PropertySimple Linear RegressionPolynomial RegressionNon-Parametric (e.g., Splines / Kernels)
Hypothesis ClassStraight lines (d=1d=1)Global polynomials of degree ddLocalized piecewise polynomials
Linear in Parameters?YesYesVaries by algorithm
Optimization MethodClosed-form OLSClosed-form OLSQuadratic programming / kernels
Feature Space MappingR1→R1\mathbb{R}^1 \to \mathbb{R}^1R1→Rd+1\mathbb{R}^1 \to \mathbb{R}^{d+1} (powers)Basis expansions / infinite Hilbert space
Extrapolation RiskStable (constant slope)Catastrophic (Runge oscillations)Controlled boundary constraints
Complexity DialNone (fixed degree 1)Degree dd and regularization λ\lambdaKnot placement / bandwidth γ\gamma

4. Mathematical Formulation & The Vandermonde Matrix

Given a dataset of nn training pairs {(xi,yi)}i=1n\{(x_i, y_i)\}_{i=1}^n, a univariate polynomial regression model of degree dd expresses each observation as:

yi=β0+β1xi+⋯+βdxid+εi(i=1,…,n)\begin{aligned} y_i &= \beta_0 + \beta_1 x_i + \dots + \beta_d x_i^d + \varepsilon_i \\ &\quad (i = 1, \dots, n) \end{aligned}

We assemble all nn equations into compact matrix-vector notation:

y=Xβ+ε\mathbf{y} = \mathbf{X}\boldsymbol{\beta} + \boldsymbol{\varepsilon}

Let us inspect the dimensions of every component:

y⏟n×1=X⏟n×(d+1)×β⏟(d+1)×1+ε⏟n×1\begin{aligned} \underbrace{\mathbf{y}}_{n \times 1} &= \underbrace{\mathbf{X}}_{n \times (d+1)} \times \underbrace{\boldsymbol{\beta}}_{(d+1) \times 1} \\ &\quad + \underbrace{\boldsymbol{\varepsilon}}_{n \times 1} \end{aligned}

where the design matrix X\mathbf{X} is the celebrated Vandermonde Design Matrix:

X=[1x1x12…x1d1x2x22…x2d⋮⋮⋮⋱⋮1xnxn2…xnd]∈Rn×(d+1)\mathbf{X} = \begin{bmatrix} 1 & x_1 & x_1^2 & \dots & x_1^d \\ 1 & x_2 & x_2^2 & \dots & x_2^d \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_n & x_n^2 & \dots & x_n^d \end{bmatrix} \in \mathbb{R}^{n \times (d+1)}

Each row represents an individual sample ii whose raw scalar value xix_i has been mapped into a (d+1)(d+1)-dimensional basis vector:

ϕ(xi)=[1xixi2…xid]∈R1×(d+1)\begin{aligned} \phi(x_i) &= \begin{bmatrix} 1 & x_i & x_i^2 & \dots & x_i^d \end{bmatrix} \\ &\in \mathbb{R}^{1 \times (d+1)} \end{aligned}

5. First-Principles Derivation of the OLS Normal Equations

We seek the parameter vector β^\hat{\boldsymbol{\beta}} that minimizes the Residual Sum of Squares (RSS):

RSS(β)=∑i=1n(yi−y^i)2=∥y−Xβ∥22\begin{aligned} \text{RSS}(\boldsymbol{\beta}) &= \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \\ &= \|\mathbf{y} - \mathbf{X}\boldsymbol{\beta}\|_2^2 \end{aligned}

Expressing the L2L_2 norm as a vector inner product and expanding:

J(β)=(y−Xβ)T(y−Xβ)=yTy−yTXβ−βTXTy+βTXTXβ=yTy−2βTXTy+βTXTXβ\begin{aligned} J(\boldsymbol{\beta}) &= (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) \\ &= \mathbf{y}^T\mathbf{y} - \mathbf{y}^T\mathbf{X}\boldsymbol{\beta} \\ &\quad - \boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y} + \boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \\ &= \mathbf{y}^T\mathbf{y} - 2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y} \\ &\quad + \boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \end{aligned}

Differentiating with respect to the vector β\boldsymbol{\beta}:

∇βJ=∂J∂β=−2XTy+2XTXβ\begin{aligned} \nabla_{\boldsymbol{\beta}} J &= \frac{\partial J}{\partial \boldsymbol{\beta}} \\ &= -2\mathbf{X}^T\mathbf{y} + 2\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \end{aligned}

Setting the gradient to zero vector 0\mathbf{0}:

−2XTy+2XTXβ=0  ⟹  XTXβ=XTy\begin{aligned} -2\mathbf{X}^T\mathbf{y} + 2\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} &= \mathbf{0} \\ \implies \mathbf{X}^T\mathbf{X}\boldsymbol{\beta} &= \mathbf{X}^T\mathbf{y} \end{aligned}

This system is the Polynomial Normal Equations. As long as the dataset contains at least d+1d+1 distinct xix_i coordinates, the columns of the Vandermonde matrix are linearly independent. The square Gram matrix XTX∈R(d+1)×(d+1)\mathbf{X}^T\mathbf{X} \in \mathbb{R}^{(d+1) \times (d+1)} is strictly positive-definite and invertible:

β^=(XTX)−1XTy\hat{\boldsymbol{\beta}} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}

Geometric Projection & Residual Orthogonality

The fitted vector y^=Xβ^=X(XTX)−1XTy=Hy\hat{\mathbf{y}} = \mathbf{X}\hat{\boldsymbol{\beta}} = \mathbf{X}(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y} = \mathbf{H}\mathbf{y} is the orthogonal projection of target vector y\mathbf{y} onto the polynomial column subspace col(X)\text{col}(\mathbf{X}).

Consequently, the residual vector e=y−y^\mathbf{e} = \mathbf{y} - \hat{\mathbf{y}} is strictly perpendicular to every single polynomial power column:

XTe=0  ⟹  ∑i=1nxijei=0(∀j∈{0,1,…,d})\begin{aligned} \mathbf{X}^T\mathbf{e} &= \mathbf{0} \\ \implies \sum_{i=1}^n x_i^j e_i &= 0 \quad (\forall j \in \{0, 1, \dots, d\}) \end{aligned}

For j=0j=0, this guarantees that the sum of residuals is identically zero (∑ei=0\sum e_i = 0) whenever an intercept is included.


6. Model Fitting: Complete Worked Numerical Example

To make the matrix mechanics concrete, let us calculate a complete polynomial regression problem from scratch without rounding.

The Problem Dataset

Suppose an educational researcher examines study time (xx in hours) versus examination score (yy out of 15) for n=4n=4 students:

Student (ii)Study Hours (xix_i)Exam Score (yiy_i)Polynomial Basis [1,xi,xi2][1, x_i, x_i^2]
102[1,0,0][1, 0, 0]
215[1,1,1][1, 1, 1]
326[1,2,4][1, 2, 4]
4312[1,3,9][1, 3, 9]

We wish to fit a degree-2 quadratic polynomial: y^=β0+β1x+β2x2\hat{y} = \beta_0 + \beta_1 x + \beta_2 x^2.

Step 1: Form the Vandermonde Design Matrix

X=[100111124139]∈R4×3y=[25612]∈R4×1\begin{aligned} \mathbf{X} &= \begin{bmatrix} 1 & 0 & 0 \\ 1 & 1 & 1 \\ 1 & 2 & 4 \\ 1 & 3 & 9 \end{bmatrix} \in \mathbb{R}^{4 \times 3} \\[6pt] \mathbf{y} &= \begin{bmatrix} 2 \\ 5 \\ 6 \\ 12 \end{bmatrix} \in \mathbb{R}^{4 \times 1} \end{aligned}

Step 2: Compute the Gram Matrix XTX\mathbf{X}^T\mathbf{X}

Multiplying XT\mathbf{X}^T (3×43 \times 4) by X\mathbf{X} (4×34 \times 3):

XTX=[111101230149][100111124139]=[461461436143698]\begin{aligned} \mathbf{X}^T\mathbf{X} &= \begin{bmatrix} 1 & 1 & 1 & 1 \\ 0 & 1 & 2 & 3 \\ 0 & 1 & 4 & 9 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 1 & 1 & 1 \\ 1 & 2 & 4 \\ 1 & 3 & 9 \end{bmatrix} \\[6pt] &= \begin{bmatrix} 4 & 6 & 14 \\ 6 & 14 & 36 \\ 14 & 36 & 98 \end{bmatrix} \end{aligned}

Let's verify the diagonal entries:

  • [XTX]11=1+1+1+1=4[\mathbf{X}^T\mathbf{X}]_{11} = 1+1+1+1 = 4
  • [XTX]22=02+12+22+32=0+1+4+9=14[\mathbf{X}^T\mathbf{X}]_{22} = 0^2 + 1^2 + 2^2 + 3^2 = 0 + 1 + 4 + 9 = 14
  • [XTX]33=02+12+42+92=0+1+16+81=98[\mathbf{X}^T\mathbf{X}]_{33} = 0^2 + 1^2 + 4^2 + 9^2 = 0 + 1 + 16 + 81 = 98

Step 3: Compute the Moment Vector XTy\mathbf{X}^T\mathbf{y}

XTy=[111101230149][25612]=[2+5+6+120(2)+1(5)+2(6)+3(12)0(2)+1(5)+4(6)+9(12)]=[2553137]\begin{aligned} \mathbf{X}^T\mathbf{y} &= \begin{bmatrix} 1 & 1 & 1 & 1 \\ 0 & 1 & 2 & 3 \\ 0 & 1 & 4 & 9 \end{bmatrix} \begin{bmatrix} 2 \\ 5 \\ 6 \\ 12 \end{bmatrix} \\[6pt] &= \begin{bmatrix} 2 + 5 + 6 + 12 \\ 0(2) + 1(5) + 2(6) + 3(12) \\ 0(2) + 1(5) + 4(6) + 9(12) \end{bmatrix} \\[6pt] &= \begin{bmatrix} 25 \\ 53 \\ 137 \end{bmatrix} \end{aligned}

Step 4: Invert XTX\mathbf{X}^T\mathbf{X}

First, compute the determinant of the 3×33 \times 3 Gram matrix:

det⁡(XTX)=4(14⋅98−362)−6(6⋅98−36⋅14)+14(6⋅36−14⋅14)=4(1372−1296)−6(588−504)+14(216−196)=4(76)−6(84)+14(20)=304−504+280=80\begin{aligned} \det(\mathbf{X}^T\mathbf{X}) &= 4(14 \cdot 98 - 36^2) - 6(6 \cdot 98 - 36 \cdot 14) \\ &\quad + 14(6 \cdot 36 - 14 \cdot 14) \\ &= 4(1372 - 1296) - 6(588 - 504) \\ &\quad + 14(216 - 196) \\ &= 4(76) - 6(84) + 14(20) \\ &= 304 - 504 + 280 = 80 \end{aligned}

Because det⁡=80≠0\det = 80 \ne 0, the inverse exists. Computing adjugate cofactors and dividing by 8080:

(XTX)−1=180[76−8420−84196−6020−6020]=[1920−212014−21204920−3414−3414]\begin{aligned} (\mathbf{X}^T\mathbf{X})^{-1} &= \frac{1}{80} \begin{bmatrix} 76 & -84 & 20 \\ -84 & 196 & -60 \\ 20 & -60 & 20 \end{bmatrix} \\[6pt] &= \begin{bmatrix} \frac{19}{20} & -\frac{21}{20} & \frac{1}{4} \\[4pt] -\frac{21}{20} & \frac{49}{20} & -\frac{3}{4} \\[4pt] \frac{1}{4} & -\frac{3}{4} & \frac{1}{4} \end{bmatrix} \end{aligned}

Step 5: Solve for Optimal Coefficients β^\hat{\boldsymbol{\beta}}

β^=(XTX)−1XTy=[1920−212014−21204920−3414−3414][2553137]\begin{aligned} \hat{\boldsymbol{\beta}} &= (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y} \\[4pt] &= \begin{bmatrix} \frac{19}{20} & -\frac{21}{20} & \frac{1}{4} \\[4pt] -\frac{21}{20} & \frac{49}{20} & -\frac{3}{4} \\[4pt] \frac{1}{4} & -\frac{3}{4} & \frac{1}{4} \end{bmatrix} \begin{bmatrix} 25 \\ 53 \\ 137 \end{bmatrix} \end{aligned}

Multiplying row by column:

β^0=19(25)−21(53)+5(137)20=475−1113+68520=4720=2.35β^1=−21(25)+49(53)−15(137)20=−525+2597−205520=1720=0.85β^2=1(25)−3(53)+1(137)4=25−159+1374=34=0.75\begin{aligned} \hat{\beta}_0 &= \frac{19(25) - 21(53) + 5(137)}{20} \\ &= \frac{475 - 1113 + 685}{20} = \frac{47}{20} = \mathbf{2.35} \\[6pt] \hat{\beta}_1 &= \frac{-21(25) + 49(53) - 15(137)}{20} \\ &= \frac{-525 + 2597 - 2055}{20} = \frac{17}{20} = \mathbf{0.85} \\[6pt] \hat{\beta}_2 &= \frac{1(25) - 3(53) + 1(137)}{4} \\ &= \frac{25 - 159 + 137}{4} = \frac{3}{4} = \mathbf{0.75} \end{aligned}

The exact OLS fitted quadratic regression equation is:

y^=2.35+0.85x+0.75x2\hat{y} = 2.35 + 0.85x + 0.75x^2

Step 6: Prediction, Residual Analysis, and RSS Verification

Let us verify the predictions and sample residuals:

xix_iObserved yiy_iFitted y^i=2.35+0.85xi+0.75xi2\hat{y}_i = 2.35 + 0.85x_i + 0.75x_i^2Residual ei=yi−y^ie_i = y_i - \hat{y}_iSquared Residual ei2e_i^2
022.35+0+0=2.352.35 + 0 + 0 = 2.352−2.35=−0.352 - 2.35 = \mathbf{-0.35}0.12250.1225
152.35+0.85(1)+0.75(1)=3.952.35 + 0.85(1) + 0.75(1) = 3.955−3.95=+1.055 - 3.95 = \mathbf{+1.05}1.10251.1025
262.35+0.85(2)+0.75(4)=7.052.35 + 0.85(2) + 0.75(4) = 7.056−7.05=−1.056 - 7.05 = \mathbf{-1.05}1.10251.1025
3122.35+0.85(3)+0.75(9)=11.652.35 + 0.85(3) + 0.75(9) = 11.6512−11.65=+0.3512 - 11.65 = \mathbf{+0.35}0.12250.1225
Sum∑ei=0.00\sum e_i = \mathbf{0.00}RSS=2.4500\text{RSS} = \mathbf{2.4500}

Fitted quadratic regression model showing observed points, predictions, and residuals

Both fundamental OLS properties hold to infinite precision:

  1. Sum of residuals is exactly zero: −0.35+1.05−1.05+0.35=0.00-0.35 + 1.05 - 1.05 + 0.35 = 0.00.
  2. Residuals are orthogonal to design columns:
    • ∑xiei=0(−0.35)+1(1.05)+2(−1.05)+3(0.35)=1.05−2.10+1.05=0.00\sum x_i e_i = 0(-0.35) + 1(1.05) + 2(-1.05) + 3(0.35) = 1.05 - 2.10 + 1.05 = 0.00.
    • ∑xi2ei=0(−0.35)+1(1.05)+4(−1.05)+9(0.35)=1.05−4.20+3.15=0.00\sum x_i^2 e_i = 0(-0.35) + 1(1.05) + 4(-1.05) + 9(0.35) = 1.05 - 4.20 + 3.15 = 0.00.

7. Interactive Simulation: Degree & Overfitting Explorer

Experiment with polynomial degree dd and regularization below to see how the hypothesis space transforms:

Figure 3.3: Polynomial Geometry & Bias-Variance Tradeoff

Capacity:d = 3 (4 params)
Training RSS:4.977
Est. Test MSE:1.47
Status:Optimal Fit (Balanced)
POLYNOMIAL DEGREE (d = 3)3 (Parameters: 4)
18

Interactive Polynomial Regression. Increase degree d to expand model capacity. Low degrees (d=1) underfit due to high bias; excessive degrees (d ≥ 6) cause wild boundary oscillations (Runge's Phenomenon) and high variance. Adding Ridge penalty (L₂) shrinks the polynomial coefficients and restores stability.


8. Multivariate Polynomials & Interaction Terms

In practical applications, targets depend on multiple features (e.g. predicting house price yy from living area X1X_1 and number of bedrooms X2X_2).

A standard multiple linear model assumes purely additive effects:

y^=β0+β1X1+β2X2\hat{y} = \beta_0 + \beta_1 X_1 + \beta_2 X_2

Expanding to degree 2 without interactions captures curvature along individual axes:

y^=β0+β1X1+β2X2+β3X12+β4X22\begin{aligned} \hat{y} &= \beta_0 + \beta_1 X_1 + \beta_2 X_2 \\ &\quad + \beta_3 X_1^2 + \beta_4 X_2^2 \end{aligned}

However, pure power terms assume that the effect of living area X1X_1 is completely independent of the number of bedrooms X2X_2. In reality, adding 500 square feet to a 1-bedroom apartment has a vastly different market impact than adding 500 square feet to a 5-bedroom house.

To model this synergy, we include the interaction term X1X2X_1 X_2:

y^=β0+β1X1+β2X2+β3X12+β4X22+β5X1X2\begin{aligned} \hat{y} &= \beta_0 + \beta_1 X_1 + \beta_2 X_2 \\ &\quad + \beta_3 X_1^2 + \beta_4 X_2^2 + \beta_5 X_1 X_2 \end{aligned}
💡

Partial Derivative Interpretation: Taking the partial derivative with respect to X1X_1:

∂y^∂X1=β1+2β3X1+β5X2\frac{\partial \hat{y}}{\partial X_1} = \beta_1 + 2\beta_3 X_1 + \beta_5 X_2

The marginal rate of change of y^\hat{y} with respect to X1X_1 now explicitly depends on the current value of X2X_2! When β5≠0\beta_5 \ne 0, the features interact synergistically.


9. The Combinatorial Explosion of Feature Space

How many features are created when applying a degree-dd polynomial expansion to pp input variables?

Including the constant intercept and all cross-product interaction terms ∏k=1pXkdk\prod_{k=1}^p X_k^{d_k} with ∑dk≤d\sum d_k \le d, the total number of features DD is given by the combination with repetition formula:

D=(p+dd)=(p+d)!p! d!D = \binom{p + d}{d} = \frac{(p + d)!}{p! \, d!}
Original Features (pp)Degree d=1d=1Degree d=2d=2Degree d=3d=3Degree d=4d=4Degree d=5d=5
123456
236101521
562156126252
1011662861,0013,003
20212311,77110,62653,130
⚠️

The Curse of Dimensionality: With only p=10p=10 original features and degree d=5d=5, the model expands into 3,003 columns! If your training sample size n<3,003n < 3{,}003, the matrix XTX\mathbf{X}^T\mathbf{X} is singular and non-invertible, making unregularized OLS mathematically impossible.


10. Bias-Variance Decomposition & Runge's Phenomenon

The polynomial degree dd acts as the direct complexity dial of the model:

  • Underfitting (High Bias, Low Variance, e.g. d=1d=1): The model is too rigid to capture the true underlying nonlinear curvature.
  • Optimal Fit (Balanced, e.g. d=3d=3): The curve captures the true underlying pattern while smoothing over random noise.
  • Overfitting (Low Bias, High Variance, e.g. d=11d=11): The curve interpolates every training point (RSS→0\text{RSS} \to 0), but oscillates violently between points.

Comparison of Underfitting d=1, Optimal Fit d=4, and Overfitting d=11

Runge's Phenomenon: The Extrapolation Hazard

When fitting high-degree polynomials on equally spaced points, the curve develops violent, high-frequency oscillations near the edges of the interval, known as Runge's Phenomenon.

Never use high-degree polynomials to extrapolate beyond the observed minimum and maximum training range!

Bias-Variance Trade-off curve showing training vs test error across polynomial degrees

Mathematically, the expected prediction error at any query point x0x_0 decomposes into three orthogonal components:

E[(y−f^(x0))2]=(f(x0)−E[f^(x0)])2⏟Bias2+E[(f^(x0)−E[f^(x0)])2]⏟Variance+σ2⏟Irreducible Noise\begin{aligned} &\mathbb{E}\left[ (y - \hat{f}(x_0))^2 \right] \\ &\quad = \underbrace{\left( f(x_0) - \mathbb{E}[\hat{f}(x_0)] \right)^2}_{\text{Bias}^2} \\ &\quad\quad + \underbrace{\mathbb{E}\left[ (\hat{f}(x_0) - \mathbb{E}[\hat{f}(x_0)])^2 \right]}_{\text{Variance}} \\ &\quad\quad + \underbrace{\sigma^2}_{\text{Irreducible Noise}} \end{aligned}

As degree dd rises:

  • Bias2\text{Bias}^2 decreases monotonically because higher-degree polynomials subsume lower-degree ones.
  • Variance\text{Variance} explodes because the coefficients become hypersensitive to minor shifts in the sample data.

11. Taming Polynomials: Regularization & Mandatory Scaling

Instead of guessing a single magic degree dd, a powerful modern strategy is to use a flexible degree (e.g., d=8d=8) combined with regularization to constrain coefficient magnitudes.

Comparison of Unregularized OLS vs Ridge vs Lasso on high-degree polynomials

Ridge Regression (L2L_2 Penalty)

JRidge(β)=∑i=1n(yi−y^i)2+λ∑j=1dβj2\begin{aligned} J_{\text{Ridge}}(\boldsymbol{\beta}) &= \sum_{i=1}^n (y_i - \hat{y}_i)^2 \\ &\quad + \lambda \sum_{j=1}^d \beta_j^2 \end{aligned}

The unpenalized intercept β0\beta_0 is preserved. In matrix form:

β^Ridge=(XTX+λP)−1XTyP=diag(0,1,1,…,1)\begin{aligned} \hat{\boldsymbol{\beta}}_{\text{Ridge}} &= (\mathbf{X}^T\mathbf{X} + \lambda \mathbf{P})^{-1}\mathbf{X}^T\mathbf{y} \\ \mathbf{P} &= \text{diag}(0, 1, 1, \dots, 1) \end{aligned}

Adding λ\lambda to the diagonal prevents singularity and dampens wild oscillations.

Lasso Regression (L1L_1 Penalty)

JLasso(β)=∑i=1n(yi−y^i)2+λ∑j=1d∣βj∣\begin{aligned} J_{\text{Lasso}}(\boldsymbol{\beta}) &= \sum_{i=1}^n (y_i - \hat{y}_i)^2 \\ &\quad + \lambda \sum_{j=1}^d |\beta_j| \end{aligned}

The diamond geometry of the L1L_1 penalty drives non-essential higher-order polynomial coefficients to exact zeros, performing automatic degree selection and interaction pruning!

Why Feature Scaling is Mandatory

If x∈[1,100]x \in [1, 100], then:

  • x1∈[1,100]x^1 \in [1, 100]
  • x2∈[1,10,000]x^2 \in [1, 10{,}000]
  • x4∈[1,108]x^4 \in [1, 10^8]

On unscaled features, the condition number κ(XTX)\kappa(\mathbf{X}^T\mathbf{X}) explodes past 101510^{15}, causing catastrophic arithmetic roundoff errors. Furthermore, regularizers like λ∑βj2\lambda \sum \beta_j^2 would unfairly penalize higher powers simply because their unscaled values are massive.

Always standardize features with zz-score normalization after polynomial expansion:

zj=xj−μjσjz_j = \frac{x^j - \mu_j}{\sigma_j}

Interactive Quiz

🧩

Ready to test yourself?

20 questions · explanations shown after


Teach & Solidify

🎓
Feynman Teaching Card

Teach This: Polynomial Regression

Core Intuition

Polynomial Regression is standard multiple linear regression applied to powers of original features, expanding straight lines into flexible curves while retaining closed-form convex OLS optimization.

Key Concepts to Explain

  • 1Linearity in machine learning refers strictly to parameters β, not features; polynomial curves remain linear models.
  • 2A polynomial of degree d introduces up to d - 1 local turning points (extrema).
  • 3The Vandermonde matrix X maps scalar inputs into higher powers [1, x, x², ..., xᵈ].
  • 4High-degree polynomials suffer from Runge's phenomenon (wild boundary oscillations) and catastrophic extrapolation errors.

⚠️ What Most Students Get Confused About

Extrapolating outside the training domain! High-degree polynomials explode toward ±∞ near interval edges. Always bound predictions within observed ranges, standardize features, and apply Ridge or Lasso regularization.

✍️
Active Recall Practice

Explain Polynomial Regression in your own words

Imagine you are explaining this idea to a friend or classmate with no math background. Writing it out forces your brain to bridge gaps in your intuition.

Saved locally on your device

Resources & GitHub

🎯

Mastered this concept?

Tick this box when you feel confident explaining it.

📁
Study Notes & Files2 files

Topic Resources

GitHub↗
📕

Polynomial_Regression.pdf

PDF Note

1.7 MB · Modified Latest

📐

Polynomial_Regression.tex

LaTeX Source

38.8 KB · Modified Latest

Was this clear?