import numpy as np
import numpy.linalg as la
import scipy.linalg as spla
m = 6
n = 4
A = np.random.randn(m, n)
b = np.random.randn(m)
Let's try solving that as a linear system using la.solve
:
la.solve(A, b)
OK, let's do QR-based least-squares then.
Q, R = la.qr(A)
What did we get? Full QR or reduced QR?
Q.shape
R.shape
Is that a problem?
OK, so find the minimizing $x$:
x = spla.solve_triangular(R, Q.T.dot(b), lower=False)
We predicted that $\|Ax-b\|_2$ would be the same as $\|Rx-Q^Tb\|_2$:
la.norm(A.dot(x)-b, 2)
la.norm(R.dot(x) - Q.T.dot(b))
Heh--reduced QR left out the right half of Q. Let's try again with complete QR:
Q2, R2 = la.qr(A, mode="complete")
x2 = spla.solve_triangular(R[:n], Q.T[:n].dot(b), lower=False)
la.norm(A.dot(x)-b, 2)
la.norm(R2.dot(x2) - Q2.T.dot(b))
Did we get the same x
both times?
x - x2
Finally, let's compare against the normal equations:
x3 = la.solve(A.T.dot(A), A.T.dot(b))
x3 - x