Polynomial Regression › Example

Michael BrenndoerferJune 9, 20255 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 32Polynomial Regression › Example
Reading section 4 of 8 · Example · 5 minSection 4 of 8 · 5 min

Book contents

Section 4 of 8 · 5 min read

Polynomial Regression › ExampleOne section · Sections in this chapter4 / 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

Let's work through a concrete example to understand how polynomial regression works step-by-step. You'll use a simple dataset and see the complete calculation process.

Step 1: Data Preparation

Consider the following dataset with 5 observations:

ObservationXY
112.1
223.9
338.1
4415.9
5525.1

Step 2: Setting Up the Quadratic Model

We'll fit a quadratic polynomial regression model: y=β0+β1x+β2x2y = \beta_0 + \beta_1 x + \beta_2 x^2

Step 3: Constructing the Design Matrix

For a quadratic model, our design matrix X\mathbf{X} includes columns for the intercept, xx, and x2x^2:

X=[11121222133214421552]=[11112413914161525]\mathbf{X} = \begin{bmatrix} 1 & 1 & 1^2 \\ 1 & 2 & 2^2 \\ 1 & 3 & 3^2 \\ 1 & 4 & 4^2 \\ 1 & 5 & 5^2 \end{bmatrix} = \begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 4 \\ 1 & 3 & 9 \\ 1 & 4 & 16 \\ 1 & 5 & 25 \end{bmatrix}

Step 4: Matrix Calculations

The response vector is:

y=[2.13.98.115.925.1]\mathbf{y} = \begin{bmatrix} 2.1 \\ 3.9 \\ 8.1 \\ 15.9 \\ 25.1 \end{bmatrix}

Now you calculate XTX\mathbf{X}^T\mathbf{X} (the Gram matrix). First, note that XT\mathbf{X}^T is the transpose of X\mathbf{X}:

XTX=[11111123451491625]3×5[11112413914161525]5×3=[51555155522555225979]3×3\mathbf{X}^T\mathbf{X} = \begin{bmatrix} 1 & 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 & 5 \\ 1 & 4 & 9 & 16 & 25 \end{bmatrix}_{3 \times 5} \begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 4 \\ 1 & 3 & 9 \\ 1 & 4 & 16 \\ 1 & 5 & 25 \end{bmatrix}_{5 \times 3} = \begin{bmatrix} 5 & 15 & 55 \\ 15 & 55 & 225 \\ 55 & 225 & 979 \end{bmatrix}_{3 \times 3}

For example, the element in row 1, column 2 is: (1)(1)+(1)(2)+(1)(3)+(1)(4)+(1)(5)=15(1)(1) + (1)(2) + (1)(3) + (1)(4) + (1)(5) = 15.

Similarly, calculate XTy\mathbf{X}^T\mathbf{y}:

XTy=[11111123451491625]3×5[2.13.98.115.925.1]5×1=[55.1223.3972.5]3×1\mathbf{X}^T\mathbf{y} = \begin{bmatrix} 1 & 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 & 5 \\ 1 & 4 & 9 & 16 & 25 \end{bmatrix}_{3 \times 5} \begin{bmatrix} 2.1 \\ 3.9 \\ 8.1 \\ 15.9 \\ 25.1 \end{bmatrix}_{5 \times 1} = \begin{bmatrix} 55.1 \\ 223.3 \\ 972.5 \end{bmatrix}_{3 \times 1}

For example, the first element is: (1)(2.1)+(1)(3.9)+(1)(8.1)+(1)(15.9)+(1)(25.1)=55.1(1)(2.1) + (1)(3.9) + (1)(8.1) + (1)(15.9) + (1)(25.1) = 55.1.

Step 5: Solving for Coefficients

Using the normal equation β^=(XTX)−1XTy\hat{\boldsymbol{\beta}} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}, we first compute the inverse of XTX\mathbf{X}^T\mathbf{X}:

(XTX)−1=[51555155522555225979]−1=[4.6−3.30.5−3.32.671−0.4290.5−0.4290.071](\mathbf{X}^T\mathbf{X})^{-1} = \begin{bmatrix} 5 & 15 & 55 \\ 15 & 55 & 225 \\ 55 & 225 & 979 \end{bmatrix}^{-1} = \begin{bmatrix} 4.6 & -3.3 & 0.5 \\ -3.3 & 2.671 & -0.429 \\ 0.5 & -0.429 & 0.071 \end{bmatrix}

Therefore, the coefficient estimates are:

β^=[4.6−3.30.5−3.32.671−0.4290.5−0.4290.071][55.1223.3972.5]=[2.82−2.0861.314]\hat{\boldsymbol{\beta}} = \begin{bmatrix} 4.6 & -3.3 & 0.5 \\ -3.3 & 2.671 & -0.429 \\ 0.5 & -0.429 & 0.071 \end{bmatrix} \begin{bmatrix} 55.1 \\ 223.3 \\ 972.5 \end{bmatrix} = \begin{bmatrix} 2.82 \\ -2.086 \\ 1.314 \end{bmatrix}

where:

  • β^0=2.82\hat{\beta}_0 = 2.82 is the estimated intercept
  • β^1=−2.086\hat{\beta}_1 = -2.086 is the estimated coefficient for the linear term xx
  • β^2=1.314\hat{\beta}_2 = 1.314 is the estimated coefficient for the quadratic term x2x^2

Step 6: Final Model

Our fitted quadratic polynomial regression model is:

y^=2.82−2.086x+1.314x2\hat{y} = 2.82 - 2.086x + 1.314x^2

where:

  • y^\hat{y} is the predicted response value
  • xx is the predictor variable
  • The coefficients indicate a quadratic relationship with both linear and quadratic components

This model shows that the relationship has a negative linear component (−2.086x-2.086x) and a positive quadratic component (1.314x21.314x^2), creating a U-shaped curve that increases more rapidly for larger values of xx.

Step 7: Verification

Let's verify our model by calculating predictions for each observation:

  • For x=1x = 1: y^=2.82−2.086(1)+1.314(1)2=2.82−2.086+1.314=2.05\hat{y} = 2.82 - 2.086(1) + 1.314(1)^2 = 2.82 - 2.086 + 1.314 = 2.05 (actual: 2.1, residual: −0.05-0.05)
  • For x=2x = 2: y^=2.82−2.086(2)+1.314(4)=2.82−4.172+5.256=3.90\hat{y} = 2.82 - 2.086(2) + 1.314(4) = 2.82 - 4.172 + 5.256 = 3.90 (actual: 3.9, residual: 0.000.00)
  • For x=3x = 3: y^=2.82−2.086(3)+1.314(9)=2.82−6.258+11.826=8.39\hat{y} = 2.82 - 2.086(3) + 1.314(9) = 2.82 - 6.258 + 11.826 = 8.39 (actual: 8.1, residual: 0.290.29)
  • For x=4x = 4: y^=2.82−2.086(4)+1.314(16)=2.82−8.344+21.024=15.50\hat{y} = 2.82 - 2.086(4) + 1.314(16) = 2.82 - 8.344 + 21.024 = 15.50 (actual: 15.9, residual: −0.40-0.40)
  • For x=5x = 5: y^=2.82−2.086(5)+1.314(25)=2.82−10.430+32.850=25.24\hat{y} = 2.82 - 2.086(5) + 1.314(25) = 2.82 - 10.430 + 32.850 = 25.24 (actual: 25.1, residual: 0.140.14)

where the residual is defined as ei=y^i−yie_i = \hat{y}_i - y_i (predicted minus actual).

The model captures the quadratic trend very well, with small residuals. The residual sum of squares is:

RSS=(−0.05)2+(0.00)2+(0.29)2+(−0.40)2+(0.14)2=0.265\text{RSS} = (-0.05)^2 + (0.00)^2 + (0.29)^2 + (-0.40)^2 + (0.14)^2 = 0.265

where:

  • RSS\text{RSS} is the residual sum of squares
  • Each term represents the squared difference between predicted and actual values
  • A smaller RSS indicates a better fit to the data

This small RSS value of 0.265 indicates an excellent fit to the data.