Polynomial Regression › Formula

Michael BrenndoerferJune 9, 202510 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 32Polynomial Regression › Formula
Reading section 2 of 8 · Formula · 10 minSection 2 of 8 · 10 min

Book contents

Section 2 of 8 · 10 min read

Polynomial Regression › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The polynomial regression model extends linear regression by including polynomial terms of the independent variable. For a single variable xx, the polynomial regression model of degree dd is:

y=β0+β1x+β2x2+β3x3+⋯+βdxd+ϵy = \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 + \cdots + \beta_d x^d + \epsilon

where:

  • yy is the dependent variable (response variable we want to predict)
  • xx is the independent variable (predictor or feature)
  • β0\beta_0 is the intercept (the value of yy when x=0x = 0)
  • β1,β2,…,βd\beta_1, \beta_2, \ldots, \beta_d are the regression coefficients for the polynomial terms x,x2,…,xdx, x^2, \ldots, x^d
  • ϵ\epsilon is the error term (random noise, assumed to be normally distributed with mean 0 and constant variance σ2\sigma^2)
  • dd is the degree of the polynomial (the highest power of xx in the model)

Matrix Notation

The polynomial regression model can be written in matrix form as:

y=Xβ+ϵ\mathbf{y} = \mathbf{X}\boldsymbol{\beta} + \boldsymbol{\epsilon}

where:

  • y=[y1y2⋮yn]\mathbf{y} = \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{bmatrix} is the n×1n \times 1 vector of response values (observed outcomes)

  • X=[1x1x12⋯x1d1x2x22⋯x2d⋮⋮⋮⋱⋮1xnxn2⋯xnd]\mathbf{X} = \begin{bmatrix} 1 & x_1 & x_1^2 & \cdots & x_1^d \\ 1 & x_2 & x_2^2 & \cdots & x_2^d \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_n & x_n^2 & \cdots & x_n^d \end{bmatrix} is the n×(d+1)n \times (d+1) design matrix containing polynomial features up to degree dd

  • β=[β0β1β2⋮βd]\boldsymbol{\beta} = \begin{bmatrix} \beta_0 \\ \beta_1 \\ \beta_2 \\ \vdots \\ \beta_d \end{bmatrix} is the (d+1)×1(d+1) \times 1 vector of coefficients to be estimated

  • ϵ=[ϵ1ϵ2⋮ϵn]\boldsymbol{\epsilon} = \begin{bmatrix} \epsilon_1 \\ \epsilon_2 \\ \vdots \\ \epsilon_n \end{bmatrix} is the n×1n \times 1 vector of error terms (assumed to be i.i.d. with mean 0 and constant variance σ2\sigma^2)

Multiple Variables

When you have more than one independent variable (feature), polynomial regression expands to include powers of each variable along with all possible products (interactions) between variables up to the chosen degree. This allows the model to capture much more complex relationships between your predictors and the response.

Suppose you have pp independent variables: x1,x2,…,xpx_1, x_2, \ldots, x_p. The general form of a polynomial regression model of degree dd is:

y=β0+∑i=1pβixi+∑i=1p∑j=ipβijxixj+∑i=1p∑j=ip∑k=jpβijkxixjxk+⋯+ϵy = \beta_0 + \sum_{i=1}^{p} \beta_i x_i + \sum_{i=1}^{p} \sum_{j=i}^{p} \beta_{ij} x_i x_j + \sum_{i=1}^{p} \sum_{j=i}^{p} \sum_{k=j}^{p} \beta_{ijk} x_i x_j x_k + \cdots + \epsilon

where:

  • yy is the response variable
  • pp is the number of predictor variables
  • xix_i is the ii-th predictor variable
  • β0\beta_0 is the intercept term
  • βi\beta_i are coefficients for linear terms
  • βij\beta_{ij} are coefficients for quadratic terms and two-way interactions
  • βijk\beta_{ijk} are coefficients for cubic terms and three-way interactions
  • ϵ\epsilon is the error term

Let's break down what this means:

  • Linear terms: Each variable appears by itself, just like in standard linear regression. For example, β1x1\beta_1 x_1, β2x2\beta_2 x_2, ..., βpxp\beta_p x_p.
  • Quadratic terms: These include both squared terms (like x12x_1^2, x22x_2^2) and products of two different variables (like x1x2x_1 x_2). The coefficients for these are βii\beta_{ii} for squares and βij\beta_{ij} for interactions where i≠ji \neq j.
  • Cubic and higher-order terms: These are all possible products of three or more variables, such as x1x2x3x_1 x_2 x_3, x22x3x_2^2 x_3, etc., up to the chosen degree dd.
  • Interaction terms: Any term that involves the product of two or more different variables (e.g., x1x2x_1 x_2) is called an interaction term. These allow the effect of one variable to depend on the value of another.

Example: Two Variables, Degree 2

If you have two variables (x1x_1 and x2x_2) and use a quadratic (degree 2) polynomial, your model will look like:

y=β0+β1x1+β2x2+β11x12+β22x22+β12x1x2+ϵy = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_{11} x_1^2 + \beta_{22} x_2^2 + \beta_{12} x_1 x_2 + \epsilon

where:

  • β0\beta_0 is the intercept
  • β1\beta_1 and β2\beta_2 are coefficients for the linear terms x1x_1 and x2x_2
  • β11\beta_{11} and β22\beta_{22} are coefficients for the squared terms x12x_1^2 and x22x_2^2
  • β12\beta_{12} is the coefficient for the interaction term x1x2x_1 x_2
  • ϵ\epsilon is the error term

Here, you have a total of 6 terms (including the intercept):

  • Two linear terms: x1x_1, x2x_2
  • Two quadratic (squared) terms: x12x_1^2, x22x_2^2
  • One interaction term: x1x2x_1 x_2

General Structure

In general, for pp variables and degree dd, the number of terms in the model (including the intercept) is given by the binomial coefficient:

Number of terms=(p+dd)=(p+d)!d!⋅p!\text{Number of terms} = \binom{p + d}{d} = \frac{(p + d)!}{d! \cdot p!}

where:

  • pp is the number of predictor variables
  • dd is the polynomial degree
  • (p+dd)\binom{p + d}{d} is the binomial coefficient (also written as "p+d choose d")
  • The formula counts all possible combinations of variables multiplied together up to degree dd, including the intercept

For example, with p=2p = 2 variables and d=2d = 2 degree: (2+22)=(42)=6\binom{2+2}{2} = \binom{4}{2} = 6 terms (which matches our example above: intercept, x1x_1, x2x_2, x12x_1^2, x22x_2^2, x1x2x_1 x_2).

This rapid combinatorial growth is why polynomial regression with multiple variables can quickly become computationally expensive and prone to overfitting.

Out[2]:
Visualization
Heatmap showing exponential growth of polynomial features with increasing variables and degree.
Exponential growth of polynomial features as the number of variables and polynomial degree increase. The heatmap shows how quickly the feature space explodes - with just 5 variables and degree 4, you have 126 features to estimate. This visualization demonstrates why polynomial regression is typically limited to low degrees and few variables in practice.

Summary Table of Terms

DegreeExample Terms (for x1x_1, x2x_2)Description
1x1x_1, x2x_2Linear
2x12x_1^2, x22x_2^2, x1x2x_1 x_2Quadratic & Interactions
3x13x_1^3, x23x_2^3, x12x2x_1^2 x_2, x1x22x_1 x_2^2Cubic & Higher Interactions

In Summary, with multiple variables, polynomial regression models include all combinations of variables multiplied together up to the specified degree. This flexibility allows you to model complex, non-linear relationships, but it also means the number of terms can grow very quickly as you add more variables or increase the degree.

Out[3]:
Visualization
3D surface plot showing quadratic polynomial regression with two variables.
3D visualization of a quadratic polynomial regression surface with two variables. The surface shows how the model captures both individual variable effects (x₁² and x₂²) and interaction effects (x₁x₂). The curvature in multiple directions demonstrates the model's ability to capture complex non-linear relationships between multiple predictors and the response variable.

Mathematical Properties

The polynomial regression model maintains several important mathematical properties from linear regression. You can estimate the coefficients using the least squares method, which minimizes the sum of squared residuals:

RSS=∑i=1n(yi−y^i)2=∑i=1n(yi−∑j=0dβjxij)2\text{RSS} = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 = \sum_{i=1}^{n} \left(y_i - \sum_{j=0}^{d} \beta_j x_i^j\right)^2

where:

  • RSS\text{RSS} is the residual sum of squares (the objective function to minimize)
  • yiy_i is the observed response value for observation ii
  • y^i\hat{y}_i is the predicted response value for observation ii
  • nn is the number of observations
  • dd is the polynomial degree
  • βj\beta_j is the coefficient for the jj-th power term
  • xix_i is the predictor value for observation ii

The solution to this minimization problem is given by the normal equation:

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

where:

  • β^\hat{\boldsymbol{\beta}} is the (d+1)×1(d+1) \times 1 vector of estimated coefficients
  • XT\mathbf{X}^T is the (d+1)×n(d+1) \times n transpose of the design matrix
  • (XTX)−1(\mathbf{X}^T\mathbf{X})^{-1} is the (d+1)×(d+1)(d+1) \times (d+1) inverse of the Gram matrix
  • y\mathbf{y} is the n×1n \times 1 vector of observed response values

The model maintains the Gauss-Markov theorem properties when the error terms satisfy the standard assumptions (zero mean, constant variance, and uncorrelated errors), making the least squares estimator the best linear unbiased estimator (BLUE).

Mathematical Complexity

The matrix calculations shown in the example above are simplified for educational purposes. In practice, with larger datasets or higher polynomial degrees, the matrix inversion (XTX)−1(\mathbf{X}^T\mathbf{X})^{-1} can become numerically unstable. Modern implementations use more stable numerical methods like QR decomposition or singular value decomposition (SVD) to solve the least squares problem.