Ordinary Least Squares (OLS)
What you'll learn
- Why representing linear regression in matrix notation simplifies multidimensional estimation.
- How to expand the matrix Sum of Squared Residuals () step by step.
- How to differentiate quadratic matrix forms to derive the Normal Equations .
- The closed-form analytical OLS estimator and its full-rank requirements.
- The fundamental geometric property of OLS: residual orthogonality () and why residuals sum to zero when an intercept is present.
Explained in detail
1. The True Model in Matrix Form
In multiple linear regression, we relate a continuous dependent target variable to explanatory feature variables across observations. In scalar summation notation, this equation quickly becomes unwieldy. Matrix algebra provides a clean, rigorous, and computationally efficient representation.
The population regression model in matrix notation is:
Let's break down each component and its exact dimensions:
- is an Design Matrix: Contains the observations on independent variables across training instances. When the model includes an intercept (constant term), the first column of consists entirely of ones ().
- is an Vector: The observed values of the dependent target variable.
- is an Vector of Disturbances: The unobserved random errors or influences not captured by the features.
- is a Vector of True Parameters: The fixed but unknown population coefficients we wish to estimate.
The model decomposes into a systematic component () and a stochastic component (). Our goal in estimation is to infer the unknown population vector using sample observations.
Matrix Dimensions Check
Matrix dimensions must align under standard matrix multiplication:
Thus, has dimensions , which matches the dimension of and .

2. Estimates, Fitted Values, and Residuals
Let denote the estimated parameter vector calculated from our sample data.
The fitted (predicted) values are obtained by projecting the design matrix through our parameter estimate:
The residual vector is the sample difference between the actual observations and the fitted predictions:
Disturbances () vs. Residuals ()
Students often conflate the theoretical error term with the empirical residual. They represent fundamentally different concepts:
Crucial Distinction: In general, . Never infer properties of the unobserved population disturbances solely because sample residuals happen to satisfy mathematical identities by construction!
3. The OLS Criterion: Minimizing the Sum of Squared Residuals (RSS)
The Ordinary Least Squares criterion selects the estimate that minimizes the scalar sum of squared residuals:
Why Does ?
To see why the sum of squared residuals equals the vector dot product , write out the vectors explicitly:
-
Residual Column Vector ():
-
Transposed Row Vector ():
-
Inner Product (Row Column):
In geometric terms, is the squared Euclidean norm (length) of the residual vector in observation space .
Substituting the residual definition :
Recall the transpose property for matrix products and differences:
Applying this to our residual expression:
Substituting this back into RSS and distributing terms:
Why is a Scalar?
Let's verify the dimensions of the middle term:
Multiplying from left to right:
A matrix is a single real number (scalar). Because every scalar equals its own transpose:
The two cross-terms in the expansion are identical! Combining them yields the Master Expanded RSS:
4. Differentiating RSS with Respect to
To find the coefficient vector that minimizes RSS, we take the vector derivative of RSS with respect to and equate it to the zero vector.
Useful Matrix Differentiation Rules
- Linear Form: For constant vectors and variable vector :
- Quadratic Form: For a square matrix : If is symmetric (), this simplifies to:
Deriving the Quadratic-Form Rule via Differentials
Let . Using the product rule in differential form:
Because the second term is a scalar, it equals its transpose:
Factoring out :
Therefore, the gradient is .
Differentiating the Three RSS Terms
Differentiating each term of :
- Term 1 (): Does not depend on :
- Term 2 (): Applying the linear rule where :
- Term 3 (): Here . Since , the matrix is symmetric:
Combining all three derivatives gives the RSS Gradient Vector:
5. Normal Equations and the OLS Estimator
At the minimum, the gradient must equal the zero vector:
Dividing by and rearranging gives the celebrated Normal Equations:
If the matrix is invertible (non-singular), we premultiply both sides by :
Since , we arrive at the Closed-Form OLS Estimator:
When Does Fail to Exist?
The matrix is non-invertible if:
- (Underdetermined system): Fewer observations than independent features.
- Perfect Multicollinearity: One or more columns of are exact linear combinations of other columns (e.g., redundant or duplicate features).
The full column rank condition () guarantees that is strictly positive definite and invertible.
6. Verifying the Stationary Point is a Minimum
To confirm that the stationary point is a strict local minimum rather than a maximum or saddle point, we evaluate the second derivative (Hessian matrix):
If has full column rank, then for any non-zero vector :
Since the quadratic form is strictly positive for all non-zero vectors, the Hessian matrix is positive definite. Consequently, the stationary point is guaranteed to be a unique global minimum.
7. Fundamental Property: Residual Orthogonality ()
Starting from the normal equations and substituting the residual definition :
This equation demonstrates that the residual vector is strictly orthogonal to every single column of the design matrix !

Column-by-Column Interpretation
If is the -th column of , then implies:
In sample terms, every regressor has zero sample correlation with the OLS residuals.
Consequences When the Model Includes an Intercept
When the regression includes a constant intercept term, the first column of is a column of ones: .
Evaluating the first element of :
The sum of OLS residuals is always zero! Dividing by the sample size gives the sample mean of residuals:
Note on Intercept Requirement: The properties and hold only when the model includes an intercept column. In regressions forced through the origin without an intercept, the residuals generally do not sum to zero!
Figure 3.2: OLS Closed-Form Estimator & Orthogonality
Interactive matrix solution β̂ = (XᵀX)⁻¹Xᵀy. Drag any observation point vertically to inspect how the normal equations and residual orthogonality Xᵀe = 0 update in real time.
What to try
- Switch to the "Matrix Inspector" tab: Inspect each intermediate matrix (, , , ) computed live.
- Drag an observation vertically: Watch the OLS closed-form parameters and update instantly.
- Inspect the Orthogonality verification: Notice how and hold true regardless of where you move the data points.
- Click "Collinear Preset": Observe how a perfectly straight line yields and zero residuals.
Key ideas at a glance
Summary Reference Table
Check your understanding
🧩
Ready to test yourself?
20 questions · explanations shown after
Teach & Solidify
Teach This: OLS in Matrix Form
Core Intuition
Ordinary Least Squares finds β̂ = (XᵀX)⁻¹Xᵀy by orthogonally projecting target y onto the column space of design matrix X, guaranteeing Xᵀe = 0.
Key Concepts to Explain
- 1Matrix notation unifies multiple regression into compact equations: y = Xβ + ε.
- 2RSS = eᵀe expands into yᵀy - 2β̂ᵀXᵀy + β̂ᵀXᵀXβ̂ because yᵀXβ̂ is a 1×1 scalar.
- 3Setting the gradient ∂RSS/∂β̂ = -2Xᵀy + 2XᵀXβ̂ to zero yields the normal equations XᵀXβ̂ = Xᵀy.
- 4Residuals are strictly orthogonal to every regressor column: Xᵀe = 0.
- 5When an intercept column 1 is included, residuals always sum to zero (∑ eᵢ = 0, ē = 0).
⚠️ What Most Students Get Confused About
Confusing the unobservable population disturbances ε with the computable sample residuals e = y - Xβ̂. In general, ε ≠ e.
Explain Ordinary Least Squares (OLS) in your own words
Imagine you are explaining this idea to a friend or classmate with no math background. Writing it out forces your brain to bridge gaps in your intuition.
Resources & GitHub
Resources & Study Materials
Deepen your understanding with author-prepared study notes, derivation proofs, and curated educational lectures.
Author's comprehensive reference notes covering matrix dimensions, derivative proofs, normal equations, and residual orthogonality.
Complete editable LaTeX source file for the study notes with tcolorbox styling and mathematical derivations.
Visual walkthrough of multiple regression, matrix design, and least squares geometry.
Mastered this concept?
Tick this box when you feel confident explaining it.
Topic Resources
Was this clear?