Moore-Penrose Pseudo-Inverse
Overview
The Moore-Penrose pseudo-inverse is a mathematical tool that extends the concept of a matrix inverse to non-square matrices. It is particularly useful in solving linear systems that are either over-determined or under-determined. In such systems, the pseudo-inverse helps in finding a solution that minimizes the error, specifically the Euclidean norm of the residual, denoted as ||Ax - b||. This makes it an essential technique in various applications, including data fitting and machine learning.
Solving Overdetermined Systems
In practical scenarios, especially in data science and machine learning, we often encounter overdetermined systems where the number of equations exceeds the number of unknowns. The Moore-Penrose pseudo-inverse provides a way to find the "best fit" solution by minimizing the total error across all equations.
Example: Using the Pseudo-Inverse in Python
Below is a Python example using PyTorch to solve an overdetermined system with the pseudo-inverse:
Listing 2.12 Solving an overdetermined system using the pseudo-inverse
def pseudo_inverse(A):
return torch.matmul(torch.linalg.inv(torch.matmul(A.T, A)), A.T)
#1
X = torch.column_stack((X, torch.ones(15))) #2
#3
w = torch.matmul(pseudo_inverse(X), y) #4
print("The solution is {}".format(w)) #5
- Line 1: The function
pseudo_inversecomputes the Moore-Penrose pseudo-inverse of matrixA. - Line 2: The PyTorch
column_stackoperator adds a column of ones to the matrixX, which is a common practice in linear regression to account for the intercept term. - Line 3: The solution vector
wis computed by multiplying the pseudo-inverse ofXwith the target vectory. - Line 4: The expected solution is approximately
[1, 1, -1], indicating the weights and bias that best fit the data. - Line 5: The computed solution is
[1.08, 0.90, -0.96], which is a close approximation to the expected values, demonstrating the effectiveness of the pseudo-inverse in minimizing the error.
This example illustrates how the Moore-Penrose pseudo-inverse can be implemented to solve linear systems where a perfect solution does not exist, providing a practical approach to finding the most accurate approximation.