Least Squares Regression
A gentle explanation of simple linear regression by combining the mathematical derivation with a visual demo of best-fit lines.
What Does “Closest Line” Mean?
In simple linear regression, we fit a line of the form:
Here, is the intercept, is the slope, and is the errors.
Given data points , the goal of least squares is to find the line that is best “close” to all these points—in other words, the sum of squared vertical distances from each point to the line is minimized.
Defining the Objective Function
We measure quality by the sum of squared distances (errors):
Our task: find that minimize .
Derivation: Formula for Slope and Intercept
Step 1: Partial Derivatives for Minimization
Take partial derivatives with respect to and , and set them to zero.
With respect to :
With respect to :
Step 2: Normal Equations
From these two conditions we get:
Solving these, we obtain:
where and .
Visual Demo of the Intuition
In the visualization below, you can drag the points around and see:
- The regression line updating in real time
- Vertical red lines showing distances from each point to the line
- As you adjust the line, notice that these red distances—and particularly their sum of squares—change
When the sum of squared red distances becomes minimized, that line is the least squares solution.
Interactive Least Squares Regression
Key Insights:
- • The regression line always passes through the mean point (x̄, ȳ)
- • Red dashed lines show the vertical distances from each point to the line
- • The algorithm minimizes the sum of squared red distances
- • Try moving points to see how the line responds instantly
Summary
Least squares regression finds the line that is as close as possible to all data points, in the sense of minimizing the sum of squared vertical distances. The derivation may be algebraic, but the underlying concept is beautifully intuitive and easy to visualize with the demo above.