In Singular Value Decomposition (SVD) Part 1 , I introduce one way to calculate SVD. And in this post, I would talk about other ideas about solving SVD.
Matrix A A A is symmetric
A ⋅ ( q 1 , … q n ) = ( λ 1 q 1 … λ n q n ) = ( q 1 ⋯ q n ) ⋅ ( λ 1 ⋯ ⋯ ⋮ ⋱ ⋮ ⋯ λ n ⋯ ) \begin{aligned}
A \cdot\left(q_{1}, \ldots q_{n}\right)&=\left(\lambda_{1} q_{1} \ldots \lambda_{n} q_{n}\right) \\
&=\left(\begin{array}{lll}q_{1} & \cdots & q_{n}\end{array}\right) \cdot\left(\begin{array}{ccc}\lambda_{1} & \cdots & \cdots \\ \vdots & \ddots & \vdots \\ \cdots & \lambda_{n} & \cdots\end{array}\right)
\end{aligned}
A ⋅ ( q 1 , … q n ) = ( λ 1 q 1 … λ n q n ) = ( q 1 ⋯ q n ) ⋅ ⎝ ⎜ ⎛ λ 1 ⋮ ⋯ ⋯ ⋱ λ n ⋯ ⋮ ⋯ ⎠ ⎟ ⎞
When the matrix A A A is symmetric, the eigenvectors Q Q Q of A A A would be orthogonal. And Q − 1 = Q ⊤ Q^{-1} = Q^{\top} Q − 1 = Q ⊤ for orthogonal matrix. Then we get the SVD as below:
A Q = Q λ → A = Q λ Q − 1 = Q λ Q ⊤ \begin{aligned}
AQ=Q \lambda \quad \rightarrow \quad A=Q \lambda Q^{-1}=Q \lambda Q^{\top}
\end{aligned}
A Q = Q λ → A = Q λ Q − 1 = Q λ Q ⊤
Matrix A A A isn’t symmetric
In big data world, A A A is a data matrix but it’s not always a square matrix. Under this situation, we can’t work directly with the eigenvectors of A A A , A ⊤ A A^{\top}A A ⊤ A and A A ⊤ AA^{\top} A A ⊤ are required for SVD. Then we get “sort of” λ 2 \lambda ^{2} λ 2 , which are two sets of eigenvectors:
(1) n right singular vectors, v 1 , ⋯ , v n v_{1}, \cdots , v_{n} v 1 , ⋯ , v n orthogonal in R n R^n R n
(2) m left singular vectors, u 1 , ⋯ , u n u_{1}, \cdots , u_{n} u 1 , ⋯ , u n orthogonal in R m R^m R m
If A = U Σ V ⊤ A=U \Sigma V^{\top} A = U Σ V ⊤ , then
A ⊤ A = ( U Σ V ⊤ ) ⊤ ( U Σ V ⊤ ) = V ⊤ Σ ⊤ U ⊤ U Σ V ⊤ = V Σ ⊤ Σ V ⊤ → n × n A^{\top} A=\left(U \Sigma V^{\top}\right)^{\top}\left(U \Sigma V^{\top}\right)=V^{\top} \Sigma^{\top} U^{\top} U \Sigma V^{\top}=V \Sigma^{\top} \Sigma V^{\top} \rightarrow n \times n A ⊤ A = ( U Σ V ⊤ ) ⊤ ( U Σ V ⊤ ) = V ⊤ Σ ⊤ U ⊤ U Σ V ⊤ = V Σ ⊤ Σ V ⊤ → n × n
Σ ⊤ Σ = ( σ 1 ⋱ σ n ) ( σ 1 ⋱ σ n ) = ( σ 1 2 ⋱ σ n 2 ) \Sigma^{\top} \Sigma=\left(\begin{array}{llll}
\sigma_{1} & & \\
& \ddots & \\
& & \sigma_{n}
\end{array}\right)\left(\begin{array}{lll}
\sigma_{1} & & \\
& \ddots & \\
& & \sigma_{n}
\end{array}\right)=\left(\begin{array}{lll}
\sigma_{1}^{2} & & \\
& \ddots & \\
& & \sigma_{n}^{2}
\end{array}\right) Σ ⊤ Σ = ⎝ ⎛ σ 1 ⋱ σ n ⎠ ⎞ ⎝ ⎛ σ 1 ⋱ σ n ⎠ ⎞ = ⎝ ⎛ σ 1 2 ⋱ σ n 2 ⎠ ⎞
→ ( σ 1 2 ⋯ σ n 2 ) \rightarrow\left(\sigma_{1}{ }^{2} \cdots \sigma_{n}{ }^{2}\right) → ( σ 1 2 ⋯ σ n 2 ) are eigenvalues of A ⊤ A A^{\top} A A ⊤ A
A A ⊤ = ( U Σ V ⊤ ) ( U Σ V ⊤ ) ⊤ = U Σ V ⊤ V ⊤ Σ ⊤ U ⊤ = U Σ Σ ⊤ U ⊤ → m × m A A^{\top}=\left(U \Sigma V^{\top}\right)\left(U \Sigma V^{\top}\right)^{\top}=U \Sigma V^{\top} V^{\top} \Sigma^{\top} U^{\top}=U \Sigma \Sigma^{\top} U^{\top} \rightarrow m \times m A A ⊤ = ( U Σ V ⊤ ) ( U Σ V ⊤ ) ⊤ = U Σ V ⊤ V ⊤ Σ ⊤ U ⊤ = U Σ Σ ⊤ U ⊤ → m × m
Σ Σ ⊤ = ( σ 1 ⋱ σ m ) ( σ 1 ⋱ σ m ) = ( σ 1 2 ⋱ σ m 2 ) \Sigma \Sigma^{\top}=\left(\begin{array}{llll}
\sigma_{1} & & \\
& \ddots & \\
& & \sigma_{m}
\end{array}\right)\left(\begin{array}{lll}
\sigma_{1} & & \\
& \ddots & \\
& & \sigma_{m}
\end{array}\right)=\left(\begin{array}{lll}
\sigma_{1}^{2} & & \\
& \ddots & \\
& & \sigma_{m}^{2}
\end{array}\right) Σ Σ ⊤ = ⎝ ⎛ σ 1 ⋱ σ m ⎠ ⎞ ⎝ ⎛ σ 1 ⋱ σ m ⎠ ⎞ = ⎝ ⎛ σ 1 2 ⋱ σ m 2 ⎠ ⎞
→ ( σ 1 2 ⋯ σ m 2 ) \rightarrow\left(\sigma_{1}^{2} \cdots \sigma_{m}^{2}\right) → ( σ 1 2 ⋯ σ m 2 ) are eigenvalues of A A ⊤ A A^{\top} A A ⊤
Then in A = U Σ V ⊤ A=U \Sigma V^{\top} A = U Σ V ⊤ , V V V is the eigenvector of A ⊤ A A^{\top}A A ⊤ A , U U U is the eigenvectors of A A ⊤ AA^{\top} A A ⊤ , the diagonal elements in Σ \Sigma Σ are the root of the first same n non-zero eigenvalues of A ⊤ A A^{\top}A A ⊤ A and A A ⊤ AA^{\top} A A ⊤ .
Example
A = ( 1 0 4 6 0 1 ) , A ⊤ A = ( 17 24 24 37 ) , A A ⊤ = ( 1 4 0 4 52 6 0 6 1 ) A=\left(\begin{array}{ll}
1 & 0 \\
4 & 6 \\
0 & 1
\end{array}\right) \quad, \quad A^{\top} A=\left(\begin{array}{ll}
17 & 24 \\
24 & 37
\end{array}\right) \quad, \quad A A^{\top}=\left(\begin{array}{ccc}1 & 4 & 0 \\ 4 & 52 & 6 \\ 0 & 6 & 1\end{array}\right) A = ⎝ ⎛ 1 4 0 0 6 1 ⎠ ⎞ , A ⊤ A = ( 1 7 2 4 2 4 3 7 ) , A A ⊤ = ⎝ ⎛ 1 4 0 4 5 2 6 0 6 1 ⎠ ⎞
For A ⊤ A A^{\top} A A ⊤ A
Eigenvalues: λ 1 = 53 , λ 2 = 1 \lambda_{1}=53, \lambda_{2}=1 λ 1 = 5 3 , λ 2 = 1
Eigenvectors: v 1 = ( 0.55 0.83 ) , v 2 = ( − 0.83 0.55 ) v_{1}=\left(\begin{array}{l}0.55 \\ 0.83\end{array}\right), \quad v_{2}=\left(\begin{array}{c}-0.83 \\ 0.55\end{array}\right) v 1 = ( 0 . 5 5 0 . 8 3 ) , v 2 = ( − 0 . 8 3 0 . 5 5 )
A ⊤ A ⋅ ( 0.55 − 0.83 0.83 0.55 ) = ( 0.55 − 0.83 0.83 0.55 ) ⋅ ( 53 0 0 1 ) A^{\top} A \cdot\left(\begin{array}{cc}0.55 & -0.83 \\ 0.83 & 0.55\end{array}\right)=\left(\begin{array}{cc}0.55 & -0.83 \\ 0.83 & 0.55\end{array}\right) \cdot\left(\begin{array}{cc}53 & 0 \\ 0 & 1\end{array}\right) A ⊤ A ⋅ ( 0 . 5 5 0 . 8 3 − 0 . 8 3 0 . 5 5 ) = ( 0 . 5 5 0 . 8 3 − 0 . 8 3 0 . 5 5 ) ⋅ ( 5 3 0 0 1 )
For A A ⊤ A A^{\top} A A ⊤
Eigenvalues: λ 1 = 53 , λ 2 = 1 , λ 3 = 0 \lambda_{1}=53, \lambda_{2}=1, \lambda_{3}=0 λ 1 = 5 3 , λ 2 = 1 , λ 3 = 0
Eigenvectors: u 1 = ( 0.08 0.99 0.11 ) , u 2 = ( − 0.83 0 0.56 ) , u 3 = ( − 0.55 0.14 − 0.82 ) u_{1}=\left(\begin{array}{l}0.08 \\ 0.99 \\ 0.11\end{array}\right), \quad u_{2}=\left(\begin{array}{c}-0.83 \\ 0 \\ 0.56\end{array}\right), \quad u_{3}=\left(\begin{array}{c}-0.55 \\ 0.14 \\ -0.82\end{array}\right) u 1 = ⎝ ⎛ 0 . 0 8 0 . 9 9 0 . 1 1 ⎠ ⎞ , u 2 = ⎝ ⎛ − 0 . 8 3 0 0 . 5 6 ⎠ ⎞ , u 3 = ⎝ ⎛ − 0 . 5 5 0 . 1 4 − 0 . 8 2 ⎠ ⎞
Order the same eigenvalues of A ⊤ A A^{\top}A A ⊤ A and A A ⊤ AA^{\top} A A ⊤ from large to small: σ 1 = 53 , σ 2 = 1 \sigma_{1}=\sqrt{53}, \quad \sigma_{2}=1 σ 1 = 5 3 , σ 2 = 1
A = U Σ V ⊤ = ( 0.08 − 0.83 − 0.55 0.99 0 0.14 0.11 0.56 − 0.82 ) ⋅ ( 53 0 0 1 0 0 ) ⋅ ( 0.55 0.83 − 0.83 0.35 ) A=U \Sigma V^{\top}=\left(\begin{array}{ccc}
0.08 & -0.83 & -0.55 \\
0.99 & 0 & 0.14 \\
0.11 & 0.56 & -0.82
\end{array}\right) \cdot\left(\begin{array}{cc}
\sqrt{53} & 0 \\
0 & 1 \\
0 & 0
\end{array}\right) \cdot\left(\begin{array}{cc}
0.55 & 0.83 \\
-0.83 & 0.35
\end{array}\right) A = U Σ V ⊤ = ⎝ ⎛ 0 . 0 8 0 . 9 9 0 . 1 1 − 0 . 8 3 0 0 . 5 6 − 0 . 5 5 0 . 1 4 − 0 . 8 2 ⎠ ⎞ ⋅ ⎝ ⎛ 5 3 0 0 0 1 0 ⎠ ⎞ ⋅ ( 0 . 5 5 − 0 . 8 3 0 . 8 3 0 . 3 5 )
SVD in Python
We can use numpy to construct a matrix in Python. The module linalg in numpy offers many functions of matrix operation
1 2 3 4 import numpy as npA = np.array([[1 ,1 ],[0 ,3 ],[3 ,0 ]])U , Sigma, VT = np.linalg.svd(A, full_matrices=True)