Ordinary Least Squares (OLS) › Example

Michael BrenndoerferApril 10, 20255 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 30Ordinary Least Squares (OLS) › Example
Reading section 4 of 8 · Example · 5 minSection 4 of 8 · 5 min

Book contents

Section 4 of 8 · 5 min read

Ordinary Least Squares (OLS) › 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 step-by-step example to see exactly how the Ordinary Least Squares (OLS) solution is computed in practice. We'll use a tiny dataset so that every calculation can be shown explicitly, and you'll see how the matrix algebra connects to the underlying numbers.

Step-by-Step Calculation

Suppose we have the following dataset with four points:

Pointxxyy
113
225
337
449

We want to fit a straight line of the form y=β0+β1xy = \beta_0 + \beta_1 x to this data using OLS.

Step 1: Construct the Design Matrix and Target Vector

The design matrix XX includes a column of ones (for the intercept) and a column for the xx values:

X=[11121314]X = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \\ 1 & 4 \\ \end{bmatrix}

The target vector yy is:

y=[3579]y = \begin{bmatrix} 3 \\ 5 \\ 7 \\ 9 \\ \end{bmatrix}

Step 2: Compute XTXX^T X

First, we take the transpose of XX:

XT=[11111234]X^T = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 \\ \end{bmatrix}

Now, multiply XTX^T by XX:

XTX=[11111234][11121314]=[1+1+1+11+2+3+41+2+3+412+22+32+42]=[4101030]X^T X = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 \\ \end{bmatrix} \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \\ 1 & 4 \\ \end{bmatrix} = \begin{bmatrix} 1+1+1+1 & 1+2+3+4 \\ 1+2+3+4 & 1^2+2^2+3^2+4^2 \\ \end{bmatrix} = \begin{bmatrix} 4 & 10 \\ 10 & 30 \\ \end{bmatrix}
  • The (1,1)(1,1) entry is the sum of the first column of XX: 1+1+1+1=41+1+1+1=4
  • The (1,2)(1,2) and (2,1)(2,1) entries are the sum of the xx values: 1+2+3+4=101+2+3+4=10
  • The (2,2)(2,2) entry is the sum of the squares of xx: 12+22+32+42=1+4+9+16=301^2+2^2+3^2+4^2=1+4+9+16=30

Step 3: Compute (XTX)−1(X^T X)^{-1}

For a 2×22 \times 2 matrix [abcd]\begin{bmatrix} a & b \\ c & d \end{bmatrix}, the inverse is:

1ad−bc[d−b−ca]\frac{1}{ad-bc} \begin{bmatrix} d & -b \\ -c & a \\ \end{bmatrix}

Plugging in our values:

  • a=4a=4, b=10b=10, c=10c=10, d=30d=30
  • ad−bc=4×30−10×10=120−100=20ad-bc = 4 \times 30 - 10 \times 10 = 120 - 100 = 20

So,

(XTX)−1=120[30−10−104]=[1.5−0.5−0.50.2](X^T X)^{-1} = \frac{1}{20} \begin{bmatrix} 30 & -10 \\ -10 & 4 \\ \end{bmatrix} = \begin{bmatrix} 1.5 & -0.5 \\ -0.5 & 0.2 \\ \end{bmatrix}

Step 4: Compute XTyX^T y

Multiply XTX^T by yy:

XTy=[11111234][3579]=[3+5+7+91×3+2×5+3×7+4×9]=[243+10+21+36]=[2470]X^T y = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 \\ \end{bmatrix} \begin{bmatrix} 3 \\ 5 \\ 7 \\ 9 \\ \end{bmatrix} = \begin{bmatrix} 3+5+7+9 \\ 1 \times 3 + 2 \times 5 + 3 \times 7 + 4 \times 9 \\ \end{bmatrix} = \begin{bmatrix} 24 \\ 3+10+21+36 \\ \end{bmatrix} = \begin{bmatrix} 24 \\ 70 \\ \end{bmatrix}

Step 5: Compute the OLS Coefficients

Now, apply the OLS formula:

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

Multiply the matrices:

β^=[1.5−0.5−0.50.2][2470]=[1.5×24+(−0.5)×70−0.5×24+0.2×70]=[36−35−12+14]=[12]\hat{\beta} = \begin{bmatrix} 1.5 & -0.5 \\ -0.5 & 0.2 \\ \end{bmatrix} \begin{bmatrix} 24 \\ 70 \\ \end{bmatrix} = \begin{bmatrix} 1.5 \times 24 + (-0.5) \times 70 \\ -0.5 \times 24 + 0.2 \times 70 \\ \end{bmatrix} = \begin{bmatrix} 36 - 35 \\ -12 + 14 \\ \end{bmatrix} = \begin{bmatrix} 1 \\ 2 \\ \end{bmatrix}

So:

  • β^0=1\hat{\beta}_0 = 1 (intercept)
  • β^1=2\hat{\beta}_1 = 2 (slope)

Step 6: Interpret the Results

The OLS solution gives us the best-fitting line:

y=1+2xy = 1 + 2x
  • The intercept (β^0\hat{\beta}_0) is 11
  • The slope (β^1\hat{\beta}_1) is 22

This means for every unit increase in xx, yy increases by 22 units, and when x=0x=0, yy is predicted to be 11.

Step 7: Verify the Fit

Let's check that this line fits all the data points exactly:

  • For x=1x=1: y^1=1+2×1=3\hat{y}_1 = 1 + 2 \times 1 = 3
  • For x=2x=2: y^2=1+2×2=5\hat{y}_2 = 1 + 2 \times 2 = 5
  • For x=3x=3: y^3=1+2×3=7\hat{y}_3 = 1 + 2 \times 3 = 7
  • For x=4x=4: y^4=1+2×4=9\hat{y}_4 = 1 + 2 \times 4 = 9

All predicted values match the observed yy values perfectly.

Why Does the Fit Work Perfectly Here?

Perfect Fit Example

In this example, the data points all lie exactly on a straight line, so OLS finds a line that passes through every point with zero error. This is an idealized case used for pedagogical purposes.

Important: In real-world data, there is usually noise, measurement error, and other sources of variation, so the OLS line will not pass through every point. Instead, it will minimize the total squared vertical distance (error) between the observed yy values and the predicted y^\hat{y} values. Perfect fits like this example are extremely rare in practice and may indicate overfitting or data issues.

Summary of the OLS Calculation Steps

  1. Build the design matrix XX and target vector yy
  2. Compute XTXX^T X and XTyX^T y
  3. Find the inverse (XTX)−1(X^T X)^{-1}
  4. Multiply to get β^=(XTX)−1XTy\hat{\beta} = (X^T X)^{-1} X^T y
  5. Interpret the coefficients in the context of the problem
  6. (Optional) Verify predictions and fit

This step-by-step process is the foundation for all OLS regression, whether you have two points or two million. For larger datasets, the calculations are the same, but are handled by computers using efficient matrix operations. In particular, the calculation of the matrix inverse (XTX)−1(X^T X)^{-1}, which is required to solve for the coefficients, can be computationally intensive for very large or high-dimensional datasets. Modern numerical libraries use optimized algorithms (such as LU or Cholesky decomposition) to compute this inverse efficiently and accurately, or may use more stable techniques like solving the equivalent system of linear equations directly without explicitly forming the inverse.