Ordinary Least Squares (OLS) › Example
Machine Learning from ScratchAll chapters 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:
| Point | ||
|---|---|---|
| 1 | 1 | 3 |
| 2 | 2 | 5 |
| 3 | 3 | 7 |
| 4 | 4 | 9 |
We want to fit a straight line of the form to this data using OLS.
Step 1: Construct the Design Matrix and Target Vector
The design matrix includes a column of ones (for the intercept) and a column for the values:
The target vector is:
Step 2: Compute
First, we take the transpose of :
Now, multiply by :
- The entry is the sum of the first column of :
- The and entries are the sum of the values:
- The entry is the sum of the squares of :
Step 3: Compute
For a matrix , the inverse is:
Plugging in our values:
- , , ,
So,
Step 4: Compute
Multiply by :
Step 5: Compute the OLS Coefficients
Now, apply the OLS formula:
Multiply the matrices:
So:
- (intercept)
- (slope)
Step 6: Interpret the Results
The OLS solution gives us the best-fitting line:
- The intercept () is
- The slope () is
This means for every unit increase in , increases by units, and when , is predicted to be .
Step 7: Verify the Fit
Let's check that this line fits all the data points exactly:
- For :
- For :
- For :
- For :
All predicted values match the observed values perfectly.
Why Does the Fit Work Perfectly Here?
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 values and the predicted values. Perfect fits like this example are extremely rare in practice and may indicate overfitting or data issues.
Summary of the OLS Calculation Steps
- Build the design matrix and target vector
- Compute and
- Find the inverse
- Multiply to get
- Interpret the coefficients in the context of the problem
- (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 , 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.