Recommended resources
The freely available resources this lesson will direct you to.
- Least Squares Solutions and Deriving the Normal Equation | Linear Algebrawww.youtube.com
- Normal equation solution of the least-squares problem | Lecture 27 | Matrix Algebra for Engineerswww.youtube.com
- OLS in Matrix Formweb.stanford.edu
- 7.4. Least-squares solutions — Linear algebrainteractivetextbooks.tudelft.nl
Hello! Welcome to your next lesson.
In our previous session, you successfully derived the Normal Equations by applying multivariate calculus to the least squares cost function. We concluded that the optimal parameters must satisfy a system of linear equations, where the gradient of the cost function is zero.
Today, we will tackle the same problem from a different and arguably more elegant angle: linear algebra. Your learning outcome is to derive the OLS solution using linear algebra, which will culminate in the famous closed-form solution for the Ordinary Least Squares estimator. This approach not only provides a direct method for solving the system but also reveals a powerful geometric interpretation of what linear regression is actually doing.
Let's begin by reformulating the problem in the language of vectors and matrices.
1. The Linear Regression Problem in Matrix Form
Recall the multiple linear regression model for observations and features:
We can express this entire system of equations using matrix notation. This simplifies the representation and allows us to use the powerful tools of linear algebra.
To learn how to set this up, please read the first section of the following document.
These notes, titled 'OLS in Matrix Form', clearly define the vectors and matrices we'll use. This notation is standard in machine learning and econometrics.
Read Section 1, 'The True Model'. Focus on how the individual observations are stacked to form the response vector \mathbf{y}, the design matrix \mathbf{X}, the parameter vector oldsymbol{eta}, and the error vector oldsymbol{\epsilon}. Pay attention to the dimensions of each matrix/vector.
To summarize, we define:
- Response vector : An vector of the observed dependent variable values.
- Design Matrix : An matrix of independent variables. The first column is all ones to account for the intercept term .
- Parameter Vector : A vector of the unknown parameters we want to estimate.
With this notation, the entire system of equations condenses into the compact form:
Our goal in OLS is to find the estimated parameters, , that minimize the sum of squared residuals. The vector of residuals, , is given by . The sum of squared residuals (SSR) is the squared Euclidean norm of this residual vector:
Finding the that minimizes this quantity is the core of the least squares problem.
2. A Geometric Perspective: Orthogonal Projection
The equation asks if the vector can be written as a linear combination of the columns of . In other words, is in the column space of , denoted ?
For real-world data with noise, will almost never be perfectly in . The system is inconsistent and has no exact solution. The least squares problem then becomes: find the vector within that is closest to . This will be our model's prediction, so .
From linear algebra, we know that the closest vector in a subspace to an outside vector is its orthogonal projection.
This next video provides an excellent geometric intuition for this concept.
Least Squares Solutions and Deriving the Normal Equation | Linear Algebra
The video 'Least Squares Solutions and Deriving the Normal Equation' from Wrath of Math introduces the geometric view of least squares as a projection problem.
Watch from 03:01 to 08:48. This segment explains why we are trying to find a vector \mathbf{X}oldsymbol{eta} in the column space of \mathbf{X} that is closest to \mathbf{y}. It culminates in the key insight that this closest vector is the projection of \mathbf{y} onto the column space.
This geometric intuition is the foundation of our derivation.

The key property we will use is that the residual vector, , is orthogonal to the column space of .
3. Derivation of the Normal Equations
Now, let's formalize this geometric insight into a rigorous algebraic derivation. If the residual vector is orthogonal to , then its dot product with every vector in must be zero. This means it must be orthogonal to each of the columns of .
Let the columns of be . Orthogonality means:
We can stack these equations together. The set of column vectors forms the rows of the matrix . Therefore, we can write all orthogonality conditions in a single matrix equation:
Distributing , we get:
Rearranging this gives us the Normal Equations in matrix form:
Notice that this is the same system of equations you derived using calculus in the last lesson, but now expressed in the compact language of linear algebra.
For a formal, text-based derivation of this result, the following reading is highly recommended.
7.4. Least-squares solutions — Linear algebra
The interactive textbook from TU Delft provides a concise and rigorous proof of the Normal Equations Theorem, which is exactly what we have just discussed.
Read the subsection 'Normal equations', starting from 'Theorem 7.4.1 (Normal Equations)' and its proof. This section formalizes the argument that a least-squares solution must satisfy the Normal Equations based on the properties of orthogonal projections.
4. Solving for the OLS Estimator
We now have a single matrix equation to solve for . The terms and are known quantities computed from our data.
The matrix is a square matrix. If this matrix is invertible, we can solve for by pre-multiplying both sides by its inverse:
Since (the identity matrix), this simplifies to the closed-form solution for the OLS estimator:
This elegant equation is one of the most fundamental results in classical machine learning and statistics.
When is Invertible?
The inverse exists if and only if the columns of the design matrix are linearly independent. In the context of regression, this means that no feature can be written as a perfect linear combination of other features. This condition is known as the absence of perfect multicollinearity. The proof for this is provided in the TU Delft resource under "Proposition 7.4.2".
Test your understanding!
Given data points and features, let's verify the dimensions in the OLS solution .
What are the dimensions of:
Does the final dimension of make sense?
Show answer
- is
- is
- is
- is (\underbrace{(k+1) \times n}_{X^T}})(\underbrace{n \times (k+1)}_{X}) \rightarrow (k+1) \times (k+1)
- is also
- is (\underbrace{(k+1) \times n}_{X^T}})(\underbrace{n \times 1}_{y}) \rightarrow (k+1) \times 1
- is (\underbrace{(k+1) \times (k+1)}_{(X^T X)^{-1}}})(\underbrace{(k+1) \times 1}_{X^T y}) \rightarrow (k+1) \times 1
Yes, the final dimension of is , which matches the definition of our parameter vector containing feature coefficients and one intercept term.
Conclusion
In this lesson, you have derived the closed-form solution for Ordinary Least Squares using the principles of linear algebra. This approach provides a powerful complement to the calculus-based derivation from the previous lesson, grounding the solution in clear geometric intuition.
Key Takeaways:
- The linear regression problem can be expressed compactly as .
- Geometrically, OLS finds the vector in the column space of that is closest to the actual observation vector .
- This closest vector, , is the orthogonal projection of onto .
- The orthogonality of the residual vector () to directly yields the Normal Equations: .
- Assuming the columns of are linearly independent, the Normal Equations have a unique solution: .
Preview of the Next Lesson:
We have now established the OLS solution from both a calculus (optimization) and linear algebra (geometric) perspective. Both of these derivations required no assumptions about the nature of the error term . In the next lesson, we will introduce a third, probabilistic approach. By assuming the errors follow a Gaussian distribution, we will derive the same OLS solution using the powerful principle of Maximum Likelihood Estimation (MLE). This will provide a statistical justification for why minimizing the sum of squared errors is a reasonable thing to do.