Linear Regression
via Least Squares
Learn how to find the best-fit line through data points by minimizing squared errors: the foundation of predictive modeling.
What Is Linear Regression?
Given a set of data points \( (x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n) \), we want to find the straight line \( y = mx + b \) that best fits the data. "Best fit" means the line is as close as possible to all points simultaneously.
Data Points and the Best-Fit Line
Understanding Residuals
For a line \( y = mx + b \), the residual at data point \( (x_i, y_i) \) is the difference between the actual \( y \)-value and the predicted value:
A positive residual means the point lies above the line; a negative residual means it lies below. The goal of least squares is to make all residuals as small as possible.
Why Squared Errors?
We minimize the sum of squared residuals (SSR), not just the sum of residuals. Why? Because positive and negative residuals would cancel each other out. Squaring ensures every error contributes positively:
The Normal Equation
To minimize \( S(m, b) \), we take partial derivatives with respect to \( m \) and \( b \), set them to zero, and solve. In matrix form, the solution is elegantly compact:
Building the Design Matrix
For a line \( y = mx + b \), each data point \( (x_i, y_i) \) gives us the equation \( mx_i + b = y_i \). Stack all equations into a matrix system \( A\hat{\mathbf{x}} = \mathbf{b} \):
Geometric Interpretation
The system \( A\hat{\mathbf{x}} = \mathbf{b} \) usually has no exact solution because the data points don't lie exactly on a line. The vector \( \mathbf{b} \) is not in the column space of \( A \).
Exact Solution vs. Least Squares
| Exact Solution | Least Squares | |
|---|---|---|
| System | \( A\mathbf{x} = \mathbf{b} \) | \( A^T A \hat{\mathbf{x}} = A^T \mathbf{b} \) |
| When it works | \( \mathbf{b} \in \text{Col}(A) \) | Always (if \( A^T A \) invertible) |
| Number of equations | \( n = \) number of unknowns | \( n \gg \) number of unknowns |
| Residual | \( \mathbf{e} = \mathbf{0} \) | \( \mathbf{e} \) minimized, not zero |
| Use case | Determined systems | Overdetermined (data fitting) |
How to Apply Least Squares
Example: Three Data Points
Find the best-fit line \( y = mx + b \) for the points \( (1, 1) \), \( (2, 3) \), \( (3, 3) \).
Example: Computing the Solution
Example: Four Data Points
Fit a line to \( (0, 1) \), \( (1, 3) \), \( (2, 4) \), \( (3, 4) \).
Verifying with Residuals
For Example 2 with \( y = x + \frac{3}{2} \), let's check the residuals at each point:
| Point | Predicted \( \hat{y} \) | Residual \( e_i \) | \( e_i^2 \) |
|---|---|---|---|
| \( (0, 1) \) | \( 1.5 \) | \( -0.5 \) | \( 0.25 \) |
| \( (1, 3) \) | \( 2.5 \) | \( 0.5 \) | \( 0.25 \) |
| \( (2, 4) \) | \( 3.5 \) | \( 0.5 \) | \( 0.25 \) |
| \( (3, 4) \) | \( 4.5 \) | \( -0.5 \) | \( 0.25 \) |
Common Mistakes
Key Formulas Summary
| Concept | Formula |
|---|---|
| Residual | \( e_i = y_i - (mx_i + b) \) |
| Objective | \( S = \sum e_i^2 \) |
| Normal Equation | \( A^T A \hat{\mathbf{x}} = A^T \mathbf{b} \) |
| Closed-Form Solution | \( \hat{\mathbf{x}} = (A^T A)^{-1} A^T \mathbf{b} \) |
| Slope (direct formula) | \( m = \frac{n\sum x_i y_i - \sum x_i \sum y_i}{n\sum x_i^2 - (\sum x_i)^2} \) |
| Intercept | \( b = \bar{y} - m\bar{x} \) |
Question 1 of 5
The least squares method minimizes which quantity?
Question 2 of 5
For fitting a line \( y = mx + b \) to \( n \) data points, the design matrix \( A \) has dimensions:
Question 3 of 5
Given data points \( (1, 2) \) and \( (3, 4) \), what is \( A^T A \)?
Question 4 of 5
In the geometric interpretation, the error vector \( \mathbf{e} = \mathbf{b} - A\hat{\mathbf{x}} \) is:
Question 5 of 5
If you forget the column of ones in the design matrix \( A \), your best-fit line will: