Singular Value Decomposition
For every $m \times n$ matrix $A$ of rank $r$,
$A = U \Sigma V^T = \sigma_1 u_1 v_1^T + \cdots + \sigma_r u_r v_r^T$
In [1]:
import matplotlib.pyplot as plt
import numpy as np
In [2]:
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
In [3]:
from plot_helper import *
M is not a symmetric matrix ¶
In [4]:
M = np.array([[2, 0.1],[1, 2]])
In [5]:
evals, C = np.linalg.eig(M)
In [6]:
D = np.diag(evals)
In [7]:
D
Out[7]:
In [8]:
C
Out[8]:
Check the eigenvectors below ¶
- Orthogonal?
- Along the major & minor axes of the ellipse?
In [9]:
plot_linear_transformation(M, C[:,0], C[:,1], unit_vector=False, unit_circle=True)
Find the vectors along the major / minor axes of ellipse (i.e. after transformation) ¶
In [37]:
alpha = np.linspace(0, 2*numpy.pi, 5001)
circle = np.vstack((np.cos(alpha), np.sin(alpha)))
ellipse = M @ circle
In [38]:
lengths = np.linalg.norm(ellipse, axis=0)
major = ellipse[:,np.argmax(lengths)]
minor = ellipse[:,np.argmin(lengths)]
Get major / minor representations before transformation ¶
In [63]:
E = np.column_stack((major,minor))
In [64]:
M_inv = np.linalg.inv(M)
V = M_inv @ E
In [65]:
plot_linear_transformation(M, V[:,0], V[:,1], unit_vector=False, unit_circle=True)
In [77]:
V.T @ V
Out[77]:
In [78]:
E.T @ E
Out[78]:
In [81]:
s = np.linalg.norm(E,axis=0)
In [85]:
s
Out[85]:
$MV = \Sigma U \equiv M = \Sigma U V^{-1} \equiv M = \Sigma U V^T$ ¶
In [82]:
U = E / s
In [83]:
M @ V
Out[83]:
In [84]:
s * U
Out[84]:
In [104]:
S = np.diag(s)
S
Out[104]:
In [105]:
U @ S @ V.T
Out[105]:
In [109]:
plot_linear_transformations(V.T,S,U, unit_circle=True)
In [92]:
np.linalg.svd(M)
Out[92]:
In [93]:
s
Out[93]:
In [94]:
U
Out[94]:
In [95]:
V
Out[95]:
Eigendecomposition of $M^T M$ vs. SVD of $M$ ¶
\begin{align} M =& U \Sigma V^T \\ M^TM =& (U \Sigma V^T)^T (U \Sigma V^T) \\ =& (V \Sigma^2 V^T) \end{align}
In [115]:
evals, C = np.linalg.eig(M.T@M)
In [116]:
evals
Out[116]:
In [117]:
s**2
Out[117]:
In [118]:
C
Out[118]:
In [119]:
V
Out[119]:
In [120]:
S
Out[120]:
In [ ]: