import numpy as np
import numpy.linalg as la
np.random.seed(15)
n = 5
A = np.random.randn(n, n)
Now compute the eigenvalues and eigenvectors of $A^TA$ as eigvals
and eigvecs
using la.eig
or la.eigh
(symmetric):
eigvals, eigvecs = la.eigh(A.T.dot(A))
eigvals
Eigenvalues are real and positive. Coincidence?
eigvecs.shape
Check that those are in fact eigenvectors and eigenvalues:
B = A.T.dot(A)
B - eigvecs.dot(np.diag(eigvals)).dot(la.inv(eigvecs))
eigvecs
are orthonormal! (Why?)
Check:
eigvecs.T.dot(eigvecs) - np.eye(n)
Now piece together the SVD:
Sigma = np.diag(np.sqrt(eigvals))
V = eigvecs
U = A.dot(V).dot(la.inv(Sigma))
Check orthogonality of U
:
U.dot(U.T) - np.eye(n)