Introduction
In our last lesson, you saw how Singular Value Decomposition (SVD) provides a robust and numerically stable engine for Principal Component Analysis (PCA). We established that SVD directly reveals the principal axes and variances of a dataset without the need to compute the potentially ill-conditioned covariance matrix. This idea—that SVD provides a more stable way to handle matrix operations—is a recurring theme in numerical linear algebra.
This lesson tackles another fundamental operation: matrix inversion. While you know that an inverse reverses a transformation, this only works for a special class of (invertible square) matrices. In machine learning, we constantly encounter systems of equations, , where is not square or is singular. Think of a linear regression problem with more data points than features, or a dataset with redundant, collinear features. In these cases, a standard inverse doesn't exist. This lesson introduces the Moore-Penrose pseudoinverse, a powerful generalization of the inverse that provides the "best possible" solution for any matrix. We will focus on how SVD provides a general and, crucially, a numerically stable method for its computation, addressing your interest in understanding and preventing model failure modes.
When the Standard Inverse Isn't Enough
A standard matrix inverse exists only if the matrix is square and non-singular (has a non-zero determinant). These conditions ensure the transformation is one-to-one and can be perfectly reversed. However, in data science, our matrices rarely fit this neat description.
The following video introduces the two most common scenarios where the standard inverse fails.
Linear Systems of Equations, Least Squares Regression, Pseudoinverse
This video from Steve Brunton's series on SVD clearly frames the problem that the pseudoinverse is designed to solve.
Watch the first two segments. The first introduces the problem of solving Ax=b for non-square matrices. The second part describes the two canonical cases you'll encounter constantly in ML: the underdetermined and overdetermined systems.
As the video explains, we face two main challenges:
-
Overdetermined Systems (Tall Matrices): This occurs when we have more equations than unknowns (e.g., more data points than features in a regression model). There is generally no exact solution that satisfies all equations simultaneously. Our goal is to find an that minimizes the error, specifically the squared difference . This is the classic least-squares problem.
-
Underdetermined Systems (Fat Matrices): This happens when we have fewer equations than unknowns (e.g., more features than data points). There are infinitely many solutions for . Our goal is to choose one in a principled way. A common choice is the solution with the smallest magnitude, the one that minimizes . This is the minimum-norm solution.
In both cases, we need a tool that goes beyond the standard inverse to find these "best-effort" solutions.
The Moore-Penrose Pseudoinverse: The Best Solution
The Moore-Penrose pseudoinverse, denoted , is the generalization of the matrix inverse that elegantly handles all these cases. For any matrix , the vector provides the optimal solution in the sense we just described.
Linear Systems of Equations, Least Squares Regression, Pseudoinverse
Let's return to Steve Brunton's video to see what properties this pseudoinverse solution has.
Please watch the segment from where he explains the properties of the solution x obtained using the pseudoinverse.
To summarize the key properties:
- If the system is overdetermined, is the least-squares solution.
- If the system is underdetermined, is the minimum-norm solution among all possible solutions.
- If is actually invertible, the pseudoinverse is identical to the true inverse, so .
This makes the pseudoinverse an incredibly versatile tool. The next question is, how do we compute it?
Computing the Pseudoinverse with SVD
This is where SVD's power truly shines. Recall that any matrix can be decomposed as . For an invertible square matrix, its inverse can be written as:
Here, is simple to compute: since is diagonal, is just a diagonal matrix with the reciprocals of the singular values.
The pseudoinverse formula extends this idea beautifully to any matrix.
Linear Systems of Equations, Least Squares Regression, Pseudoinverse
The derivation of the pseudoinverse from the SVD is quite straightforward. This segment of the video walks through it.
Watch the clip from where the pseudoinverse is derived by "inverting" the components of the SVD one by one. Pay close attention to how A^+ is constructed.
As derived in the video, the pseudoinverse is defined as:
This looks almost identical to the formula for the true inverse! The entire trick lies in the construction of .
To get from :
- Take the diagonal matrix .
- Create a new matrix, , by taking the reciprocal of all non-zero singular values.
- Leave any zeros on the diagonal as they are.
- If the matrix is non-square, transpose the resulting matrix so that its shape is the same as .
The following resource provides a formal definition.
2. Discrete Inverse Problems and Regularization
This document on inverse problems gives a precise, mathematical definition of the pseudoinverse using SVD.
Please read the short section with the heading Definition: Moore-Penrose pseudo inverse. It formally states the formula we just discussed. Note that their notation K is equivalent to our A, and the subscript k denotes that we only consider the non-zero singular values.
This definition is what makes SVD so powerful. For directions that the transformation scales (non-zero ), the pseudoinverse perfectly reverses the scaling (). For directions that collapses to zero (zero ), the pseudoinverse also maps them to zero. It doesn't attempt to "invent" information that was lost in the original transformation.
Numerical Stability: SVD's Greatest Advantage
We've now seen that SVD provides a general way to compute the pseudoinverse. But more importantly, it is also the most stable method, which is critical for real-world applications where data is noisy and computations have finite precision.
The core issue is distinguishing a singular value that is truly zero from one that is just very, very small due to floating-point representation errors. A matrix that is theoretically singular might, in a computer, have a singular value of instead of 0.
Let's explore why this is a problem and how the SVD approach solves it.
Inverses and pseudoinverses. Numerical issues, speed, symmetry.
This blog post provides an excellent discussion of the practical, numerical issues involved in computing inverses and why SVD is the preferred method.
Please read the sections titled Numerical rank vs. actual rank and the following short paragraph. The author perfectly explains why a very small, non-zero singular value can "blow up the whole computation" and how the pseudoinverse, computed via SVD, gracefully handles this by thresholding.
As the article highlights, naively inverting a matrix with a tiny singular value would involve calculating , an enormous number that corrupts the entire result. This is a classic example of numerical instability.
The SVD-based pseudoinverse method solves this by being pragmatic:
- It computes the SVD of the matrix .
- It examines the singular values in .
- It sets a small threshold (e.g., ). Any singular value smaller than this threshold is treated as if it were exactly zero.
- It then proceeds to construct by taking the reciprocal of the significant singular values and placing a zero where the tiny ones were.
This prevents division by near-zero numbers and ensures a stable, meaningful result. This is why numerical computing libraries like NumPy and SciPy use SVD under the hood for their pinv (pseudoinverse) functions. They are built for robustness in the face of the messy realities of floating-point arithmetic.
To see this in action, the following video demonstrates building the pseudoinverse from SVD components in Python and confirming the result with NumPy's built-in function.
The Moore-Penrose Pseudoinverse — Topic 37 of Machine Learning Foundations
This video by Jon Krohn provides a clear, hands-on demonstration of computing the pseudoinverse.
You can watch the following segments to connect the theory to code: The pseudoinverse formula is presented. A code demo walks through computing A^+ from the SVD components of a matrix A. Confirmation with pinv shows that NumPy's built-in function gives the same result.
Conclusion
In this lesson, you have added the pseudoinverse to your mathematical toolkit and, more importantly, understood why SVD is the right way to build it. You've seen that the challenges of inverting matrices in machine learning—non-square shapes and feature redundancy—are handled elegantly by this generalized inverse.
Key Takeaways:
- The Moore-Penrose pseudoinverse () generalizes the standard matrix inverse, providing the "best" solution to for any matrix .
- It yields the least-squares solution for overdetermined systems and the minimum L2-norm solution for underdetermined systems.
- SVD provides a direct and general formula for the pseudoinverse: .
- The key to its numerical stability is the construction of , where reciprocals of singular values below a certain threshold are set to zero, preventing the catastrophic effects of dividing by near-zero numbers. This makes it robust to noise and floating-point errors.
You have now completed the final topic in our module on SVD, having seen its power in geometric interpretation, dimensionality reduction via PCA, and now robustly solving linear systems. We have completed the linear algebra portion of this course. In the next module, we will shift gears to calculus, starting with how to visualize and analyze the multi-dimensional functions that define the loss landscapes of machine learning models.