try:
import executable_engineering as exe
except ImportError:
%pip install -q executable_engineering
import executable_engineering as exe
import numpy as npIn previous sections, we solved square matrix systems ( equations, unknowns). But what if we have more equations than unknowns?
where is an rectangular matrix (with ). Such systems are called overdetermined (more equations than variables), and we usually cannot find a single solution that perfectly satisfies every equation simultaneously. Instead, we can expand our notion of finding a solution to finding a best-fit.
Minimizing the Error¶
Define the residual, , of the system as the difference between the left and right side of the equation:
If an exact solution existed, (the equation is satisfied for each element).
In lieu of an exact solution we can find one that minimizes all the elements of , i.e. minimizes the quantity:
where the factor of is arbitrary but useful as seen below.
The Normal Equations¶
To find the minimum of our residul quantity , we take the derivative with respect to and set it to zero:
This final equation is called the Normal Equations. Notice that even though is rectangular (), the product is always a square matrix!
The Normal Equations linear system with a square matrix and can be solved using our usual toolkit for linear systems. However, we can go a step further.
The Moore-Penrose Pseudoinverse¶
Since is a square matrix, we can invert it to solve for :
We define the pseudoinverse, denoted , as:
So our solution is simply .
The pseudoinverse generalizes the inverse to rectangular matrices (both overdetermined and underdetermined). If the matrix actually is square and invertible, the pseudoinverse reduces mathematically to the standard exact inverse!
In practice, the matrix, while square, is ill-conditioned. The pseudoinverse is calculated using a matrix decomposition called singular value decomposition, a generalization of eigendecomposition for rectangular matricies, which is more stable.
The number function is ‘np.linalg.pinv(A)’
Terminology¶
Consistent System: A system that has at least one exact solution satisfying all equations.
Inconsistent System: A system with solution that satisfies all equations simultaneously.
Overdetermined: More equations than unknowns (). Usually inconsistent. We use the pseudoinverse to find the best fit solution that minimizes the error.
Underdetermined: Fewer equations than unknowns (). Has an infinite number of solutions. The pseudoinverse finds the solution with the smallest magnitude: .
Example: Consistent Overdetermined System¶
Let’s look at a system with 3 equations but only 2 variables.
(1)
(2)
(3)
Since it’s still a linear system, lets inspect this graphically:
A = [[20, 50], [1, 1], [50, 20]]
b = [700, 20, 700]
fig = exe.visual_solve_2d(A, b, method='pseudo')
fig.show()Example: Inconsistent Overdetermined System¶
Usually, 3 lines will not cross at exactly the same point.
(1)
(2)
(3)
A = [[2, 1], [1,-1], [1, 2]]
b = [15, 10, 14]
fig = exe.visual_solve_2d(A, b, method='pseudo')
fig.show()The solution found by the pseudoinverse minimizes the perpendicular distance from the point to all three lines simultaneously. It mathematically finds the “center of mass” of the intersection triangle!
Example: Inconsistent overdetermined system with linearly dependent rows¶
Let’s look at one more inconsistent system.
(1)
(2)
(3)
Now two equations are parallel (but not overlapping).
A = [[2, 1], [1,-1], [2, 1]]
b = [15, 10, 10]
fig = exe.visual_solve_2d(A, b, method='pseudo')
fig.show()Example: Inconsistent underdetermined system¶
Consider if we only had one equation but two unknowns!
(1)
Let’s see what ‘solution’ the pseudoinverse finds!
A = [[2, 1]]
b = [15]
fig = exe.visual_solve_2d(A, b, method='pseudo')
fig.show()Conditioning of a Rectangular Matrix¶
Just as we saw with square matrices, we can calculate the condition number of a rectangular matrix to understand how sensitive our best-fit solution is to noise in the data .
We simply extend the definition using the pseudoinverse:
Modern approach to pseduoinverse calculation¶
Baring a few special cases, the pseudoinverse is most efficiently solved through Singular Value Decomposition (the generalization of eigen-decomposition for rectangular matricies.)