Singular value decomposition
clear all
format short
set(0, 'defaultaxesfontsize', 12)
set(0, 'defaultlinelinewidth', 1.5)
set(0, 'defaultFunctionLinelinewidth', 1.5)
set(0, 'defaultscattermarkerfacecolor', 'flat')
gcf;
set(gcf, 'Position', [0 0 600 350], 'Theme', 'light')
addpath ../FNC_matlab7.3. Singular value decomposition ¶ We now introduce another factorization that is as fundamental as the EVD .
The singular value decomposition of an m × n m\times n m × n matrix A \mathbf{A} A is
A = U S V ∗ , \mathbf{A} = \mathbf{U} \mathbf{S} \mathbf{V}^*, A = US V ∗ , where U ∈ C m × m \mathbf{U}\in\mathbb{C}^{m\times m} U ∈ C m × m and V ∈ C n × n \mathbf{V}\in\mathbb{C}^{n\times n} V ∈ C n × n are unitary and S ∈ R m × n \mathbf{S}\in\real^{m\times n} S ∈ R m × n is real and diagonal with nonnegative elements.
The columns of U \mathbf{U} U and V \mathbf{V} V are called left and right singular vectors , respectively. The diagonal elements of S \mathbf{S} S , written σ 1 , … , σ r \sigma_1,\ldots,\sigma_r σ 1 , … , σ r , for r = min { m , n } r=\min\{m,n\} r = min { m , n } , are called the singular values of A \mathbf{A} A and are ordered so that
σ 1 ≥ σ 2 ≥ ⋯ ≥ σ r ≥ 0 , r = min { m , n } . \sigma_1 \ge \sigma_2 \ge \cdots \ge \sigma_r\ge 0, \qquad r=\min\{m,n\}. σ 1 ≥ σ 2 ≥ ⋯ ≥ σ r ≥ 0 , r = min { m , n } . We call σ 1 \sigma_1 σ 1 the principal singular value and u 1 \mathbf{u}_{1} u 1 and v 1 \mathbf{v}_{1} v 1 the principal singular vectors .
Every m × n m\times n m × n matrix has an SVD . The singular values of a matrix are unique, but the singular vectors are not. If the matrix is real, then U \mathbf{U} U and V \mathbf{V} V in (7.3.1) can be chosen to be real, orthogonal matrices.
It is easy to check that
[ 3 4 ] = ( 1 5 [ 3 − 4 4 3 ] ) ⏟ U ⋅ [ 5 0 ] ⏟ S ⋅ [ 1 ] ⏟ V T \begin{bmatrix} 3 \\ 4 \end{bmatrix}
= \underbrace{\left(\frac{1}{5} \begin{bmatrix}
3 & -4 \\ 4 & 3
\end{bmatrix}\,\right)}_{\mathbf{U}} \cdot
\underbrace{\begin{bmatrix}
5 \\ 0
\end{bmatrix}}_{\mathbf{S}} \cdot
\underbrace{\begin{bmatrix}
1
\end{bmatrix}}_{\mathbf{V}^T} [ 3 4 ] = U ( 5 1 [ 3 4 − 4 3 ] ) ⋅ S [ 5 0 ] ⋅ V T [ 1 ] meets all the requirements of an SVD . Hence, interpreted as a 2 × 1 2\times 1 2 × 1 matrix, the vector [ 3 , 4 ] [3,4] [ 3 , 4 ] has the lone singular value 5.
Suppose A \mathbf{A} A is a real matrix and that A = U S V T \mathbf{A}=\mathbf{U}\mathbf{S}\mathbf{V}^T A = US V T is an SVD . Then A T = V S T U T \mathbf{A}^T=\mathbf{V}\mathbf{S}^T\mathbf{U}^T A T = V S T U T meets all the requirements of an SVD for A T \mathbf{A}^T A T : the first and last matrices are orthogonal, and the middle matrix is diagonal with nonnegative elements. Hence A \mathbf{A} A and A T \mathbf{A}^T A T have the same singular values.
7.3.1 Connections to the EVD ¶ The eigenvalues of A ∗ A \mathbf{A}^*\mathbf{A} A ∗ A are real and nonnegative, and the min { m , n } \min\{m,n\} min { m , n } largest of them are the squares of the singular values of A \mathbf{A} A .
Let A = U S V ∗ \mathbf{A}=\mathbf{U}\mathbf{S}\mathbf{V}^* A = US V ∗ be m × n m\times n m × n . Because S \mathbf{S} S is real, S ∗ = S T \mathbf{S}^* = \mathbf{S}^T S ∗ = S T . We express the square hermitian matrix B = A ∗ A \mathbf{B}=\mathbf{A}^*\mathbf{A} B = A ∗ A as
B = ( V S ∗ U ∗ ) ( U S V ∗ ) = V S ∗ S V ∗ = V ( S T S ) V − 1 , \mathbf{B} = (\mathbf{V}\mathbf{S}^*\mathbf{U}^*) (\mathbf{U}\mathbf{S}\mathbf{V}^*) = \mathbf{V}\mathbf{S}^*\mathbf{S}\mathbf{V}^* = \mathbf{V}(\mathbf{S}^T\mathbf{S})\mathbf{V}^{-1}, B = ( V S ∗ U ∗ ) ( US V ∗ ) = V S ∗ S V ∗ = V ( S T S ) V − 1 , where we have used the fact that V \mathbf{V} V is unitary. Note that S T S \mathbf{S}^T\mathbf{S} S T S is a diagonal n × n n \times n n × n matrix. There are two cases to consider. If m ≥ n m \ge n m ≥ n , then
S T S = [ σ 1 2 ⋱ σ n 2 ] . \mathbf{S}^T\mathbf{S} =
\begin{bmatrix}
\sigma_1^2 & & \\
& \ddots & \\
& & \sigma_n^2
\end{bmatrix}. S T S = ⎣ ⎡ σ 1 2 ⋱ σ n 2 ⎦ ⎤ . On the other hand, if m < n m<n m < n , then
S T S = [ σ 1 2 ⋱ σ m 2 0 ] . \mathbf{S}^T\mathbf{S} =
\begin{bmatrix}
\sigma_1^2 & & & \\
& \ddots & & \\
& & \sigma_m^2 & \\
& & & \boldsymbol{0}
\end{bmatrix}. S T S = ⎣ ⎡ σ 1 2 ⋱ σ m 2 0 ⎦ ⎤ . The proof also shows that the eigenvectors of A ∗ A \mathbf{A}^*\mathbf{A} A ∗ A , when appropriately ordered and normalized, are right singular vectors of A \mathbf{A} A . The left singular vectors could then be deduced from the identity A V = U S \mathbf{A}\mathbf{V} = \mathbf{U}\mathbf{S} AV = US .
Another close connection between EVD and SVD comes via the ( m + n ) × ( m + n ) (m+n)\times (m+n) ( m + n ) × ( m + n ) matrix
C = [ 0 A ∗ A 0 ] . \mathbf{C} =
\begin{bmatrix}
0 & \mathbf{A}^* \\ \mathbf{A} & 0
\end{bmatrix}. C = [ 0 A A ∗ 0 ] . If σ \sigma σ is a singular value of A \mathbf{A} A , then σ \sigma σ and − σ -\sigma − σ are eigenvalues of C \mathbf{C} C , and the associated eigenvector immediately reveals a left and a right singular vector (see Exercise 7.3.11 ). This connection is implicitly exploited by software to compute the SVD .
7.3.2 Interpreting the SVD ¶ Another way to write A = U S V ∗ \mathbf{A}=\mathbf{U}\mathbf{S}\mathbf{V}^* A = US V ∗ is
A V = U S . \mathbf{A}\mathbf{V}=\mathbf{U}\mathbf{S}. AV = US . Taken columnwise, this equation means
A v k = σ k u k , k = 1 , … , r = min { m , n } . \mathbf{A} \mathbf{v}_{k} = \sigma_k \mathbf{u}_{k}, \qquad k=1,\ldots,r=\min\{m,n\}. A v k = σ k u k , k = 1 , … , r = min { m , n } . In words, each right singular vector is mapped by A \mathbf{A} A to a scaled version of its corresponding left singular vector; the magnitude of scaling is its singular value.
Both the SVD and the EVD describe a matrix in terms of some special vectors and a few scalars. Table 7.3.1 summarizes the key differences. The SVD sacrifices having the same basis in both source and image spaces—after all, they may not even have the same dimension—but as a result gains orthogonality in both spaces.
Table 7.3.1: Comparison of the EVD and SVD
EVD SVD exists for most square matrices exists for all rectangular and square matrices A x k = λ k x k \mathbf{A}\mathbf{x}_k = \lambda_k \mathbf{x}_k A x k = λ k x k A v k = σ k u k \mathbf{A} \mathbf{v}_k = \sigma_k \mathbf{u}_k A v k = σ k u k same basis for domain and range of A \mathbf{A} A two orthonormal bases may have poor conditioning perfectly conditioned
In The QR factorization we saw that a matrix has both full and thin forms of the QR factorization. A similar situation holds with the SVD .
Suppose A \mathbf{A} A is m × n m\times n m × n with m > n m > n m > n and let A = U S V ∗ \mathbf{A}=\mathbf{U}\mathbf{S}\mathbf{V}^* A = US V ∗ be an SVD . The last m − n m-n m − n rows of S \mathbf{S} S are all zero due to the fact that S \mathbf{S} S is diagonal. Hence
U S = [ u 1 ⋯ u n u n + 1 ⋯ u m ] [ σ 1 ⋱ σ n 0 ] = [ u 1 ⋯ u n ] [ σ 1 ⋱ σ n ] = U ^ S ^ , \begin{align*}
\mathbf{U} \mathbf{S} & =
\begin{bmatrix}
\mathbf{u}_1 & \cdots & \mathbf{u}_n & \mathbf{u}_{n+1} & \cdots & \mathbf{u}_m
\end{bmatrix}
\begin{bmatrix}
\sigma_1 & & \\
& \ddots & \\
& & \sigma_n \\
& & \\
& \boldsymbol{0} & \\
& &
\end{bmatrix} \\
&=
\begin{bmatrix}
\mathbf{u}_1 & \cdots & \mathbf{u}_n
\end{bmatrix}
\begin{bmatrix}
\sigma_1 & & \\
& \ddots & \\
& & \sigma_n
\end{bmatrix} = \hat{\mathbf{U}} \hat{\mathbf{S}},
\end{align*} US = [ u 1 ⋯ u n u n + 1 ⋯ u m ] ⎣ ⎡ σ 1 ⋱ 0 σ n ⎦ ⎤ = [ u 1 ⋯ u n ] ⎣ ⎡ σ 1 ⋱ σ n ⎦ ⎤ = U ^ S ^ , in which U ^ \hat{\mathbf{U}} U ^ is m × n m\times n m × n and S ^ \hat{\mathbf{S}} S ^ is n × n n\times n n × n . This allows us to define the thin SVD
A = U ^ S ^ V ∗ , \mathbf{A}=\hat{\mathbf{U}}\hat{\mathbf{S}}\mathbf{V}^*, A = U ^ S ^ V ∗ , in which S ^ \hat{\mathbf{S}} S ^ is square and diagonal and U ^ \hat{\mathbf{U}} U ^ is ONC but not square.
So, in sketch form, a full SVD of a matrix that is taller than it is wide looks like
= ⋅ ⋅ \rule{1cm}{2.4cm} \; \raisebox{11mm}{=} \; \rule{2.4cm}{2.4cm} \; \raisebox{11mm}{$\centerdot$} \; \rule{1cm}{2.4cm}\; \raisebox{11mm}{$\centerdot$} \; \rule[6mm]{1cm}{1cm}\quad = ⋅ ⋅ while a thin SVD looks like
= ⋅ ⋅ \rule{1cm}{2.4cm} \; \raisebox{11mm}{=} \; \rule{1cm}{2.4cm} \; \raisebox{11mm}{$\centerdot$} \; \rule[6mm]{1cm}{1cm} \; \raisebox{11mm}{$\centerdot$} \; \rule[6mm]{1cm}{1cm} = ⋅ ⋅ The thin form retains all the information about A \mathbf{A} A from the SVD ; the factorization is still an equality, not an approximation. It is computationally preferable when m ≫ n m \gg n m ≫ n , since it requires far less storage than a full SVD . For a matrix with more columns than rows, one can derive a thin form by taking the adjoint of the thin SVD of A ∗ \mathbf{A}^* A ∗ .
7.3.4 SVD and the 2-norm¶ The SVD is intimately connected to the 2-norm, as the following theorem describes.
The conclusion (7.3.14) can be proved by vector calculus. In the square case m = n m=n m = n , A \mathbf{A} A having full rank is identical to being invertible. The SVD is the usual means for computing the 2-norm and condition number of a matrix.
We verify some of the fundamental SVD properties using the built-in svd function.
A = vander(1:5);
A = A(:, 1:4)[U, S, V] = svd(A);
disp(sprintf("U is %d by %d. S is %d by %d. V is %d by %d.\n", size(U), size(S), size(V)))U is 5 by 5. S is 5 by 4. V is 4 by 4.
We verify the orthogonality of the singular vectors as follows:
norm(U' * U - eye(5))
norm(V' * V - eye(4))Here is verification of the connections between the singular values, norm, and condition number.
s = diag(S);
norm_A = norm(A)
sigma_max = s(1)cond_A = cond(A)
sigma_ratio = s(1) / s(end)7.3.5 Exercises ¶ ✍ Each factorization below is algebraically correct. The notation I n \mathbf{I}_n I n means an n × n n\times n n × n identity. In each case, determine whether it is an SVD . If it is, write down σ 1 \sigma_1 σ 1 , u 1 \mathbf{u}_1 u 1 , and v 1 \mathbf{v}_1 v 1 . If it is not, state all of the ways in which it fails the required properties.
(a)
[ 0 0 0 − 1 ] = [ 0 1 1 0 ] [ 1 0 0 0 ] [ 0 1 − 1 0 ] \begin{bmatrix}
0 & 0 \\ 0 & -1
\end{bmatrix} = \begin{bmatrix}
0 & 1 \\ 1 & 0
\end{bmatrix} \begin{bmatrix}
1 & 0 \\ 0 & 0
\end{bmatrix} \begin{bmatrix}
0 & 1 \\ -1 & 0
\end{bmatrix} [ 0 0 0 − 1 ] = [ 0 1 1 0 ] [ 1 0 0 0 ] [ 0 − 1 1 0 ]
(b)
[ 0 0 0 − 1 ] = I 2 [ 0 0 0 − 1 ] I 2 \begin{bmatrix}
0 & 0 \\ 0 & -1
\end{bmatrix} =
\mathbf{I}_2 \begin{bmatrix}
0 & 0 \\ 0 & -1
\end{bmatrix}
\mathbf{I}_2 [ 0 0 0 − 1 ] = I 2 [ 0 0 0 − 1 ] I 2
(c)
[ 1 0 0 2 1 0 ] = [ α 0 − α 0 1 0 α 0 − α ] [ 2 0 0 2 0 0 ] [ 0 1 1 0 ] , α = 1 / 2 \begin{bmatrix}
1 & 0\\ 0 & \sqrt{2}\\ 1 & 0
\end{bmatrix} = \begin{bmatrix}
\alpha & 0 & -\alpha \\ 0 & 1 & 0 \\ \alpha & 0 & -\alpha
\end{bmatrix} \begin{bmatrix}
\sqrt{2} & 0 \\ 0 & \sqrt{2} \\ 0 & 0
\end{bmatrix} \begin{bmatrix}
0 & 1 \\ 1 & 0
\end{bmatrix}, \quad \alpha=1/\sqrt{2} ⎣ ⎡ 1 0 1 0 2 0 ⎦ ⎤ = ⎣ ⎡ α 0 α 0 1 0 − α 0 − α ⎦ ⎤ ⎣ ⎡ 2 0 0 0 2 0 ⎦ ⎤ [ 0 1 1 0 ] , α = 1/ 2
(d)
[ 2 2 − 1 1 0 0 ] = I 3 [ 2 0 0 2 0 0 ] [ α α − α α ] , α = 1 / 2 \begin{bmatrix}
\sqrt{2} & \sqrt{2}\\ -1 & 1\\ 0 & 0
\end{bmatrix} =
\mathbf{I}_3 \begin{bmatrix}
2 & 0 \\ 0 & \sqrt{2} \\ 0 & 0
\end{bmatrix} \begin{bmatrix}
\alpha & \alpha \\ -\alpha & \alpha
\end{bmatrix}, \quad \alpha=1/\sqrt{2} ⎣ ⎡ 2 − 1 0 2 1 0 ⎦ ⎤ = I 3 ⎣ ⎡ 2 0 0 0 2 0 ⎦ ⎤ [ α − α α α ] , α = 1/ 2
⌨ Let x be a vector of 1000 equally spaced points between 0 and 1. Suppose A n \mathbf{A}_n A n is the 1000 × n 1000\times n 1000 × n matrix whose ( i , j ) (i,j) ( i , j ) entry is x i j − 1 x_i^{j-1} x i j − 1 for j = 1 , … , n j=1,\ldots,n j = 1 , … , n .
(a) Print out the singular values of A 1 \mathbf{A}_1 A 1 , A 2 \mathbf{A}_2 A 2 , and A 3 \mathbf{A}_3 A 3 .
(b) Make a log-linear plot of the singular values of A 40 \mathbf{A}_{40} A 40 .
(c) Repeat part (b) after converting the elements of x to single precision.
(d) Having seen the plot for part (c), which singular values in part (b) do you suspect may be incorrect?
✍ In order to prove (7.3.15) of Theorem 7.3.3 , show that ∥ A + ∥ 2 = σ n − 1 \twonorm{\mathbf{A}^+} = \sigma_n^{-1} ∥ A + ∥ 2 = σ n − 1 . For simplicity, assume that m ≥ n m\ge n m ≥ n and that the rank of A \mathbf{A} A is n n n .
✍ Let A ∈ R m × n \mathbf{A}\in\real^{m\times n} A ∈ R m × n with m > n m>n m > n have the thin SVD A = U ^ S ^ V T \mathbf{A}=\hat{\mathbf{U}}\hat{\mathbf{S}}\mathbf{V}^T A = U ^ S ^ V T , and suppose all the singular values are nonzero. Show that the matrix A A + \mathbf{A}\mathbf{A}^{+} A A + is equal to U ^ U ^ T \hat{\mathbf{U}}\hat{\mathbf{U}}^T U ^ U ^ T , where A + \mathbf{A}^{+} A + is the pseudoinverse of A \mathbf{A} A , as given in (3.2.4) . (You must be careful with matrix sizes in this derivation.)
✍ In (3.2.6) we defined the 2-norm condition number of a rectangular matrix as κ ( A ) = ∥ A ∥ ⋅ ∥ A + ∥ \kappa(\mathbf{A})=\|\mathbf{A}\|\cdot \|\mathbf{A}^{+}\| κ ( A ) = ∥ A ∥ ⋅ ∥ A + ∥ , and then claimed (in the real case) that κ ( A ∗ A ) = κ ( A ) 2 \kappa(\mathbf{A}^*\mathbf{A})=\kappa(\mathbf{A})^2 κ ( A ∗ A ) = κ ( A ) 2 . Prove this assertion using the SVD .
✍ In this problem you will see how (7.3.14) is proved in the real case.
(a) Use the technique of Lagrange multipliers to show that among vectors that satisfy ∥ x ∥ 2 2 = 1 \|\mathbf{x}\|_2^2=1 ∥ x ∥ 2 2 = 1 , any vector that maximizes ∥ A x ∥ 2 2 \|\mathbf{A}\mathbf{x}\|_2^2 ∥ Ax ∥ 2 2 must be an eigenvector of A T A \mathbf{A}^T\mathbf{A} A T A . It will help to know that if B \mathbf{B} B is any symmetric matrix, the gradient of the scalar function x T B x \mathbf{x}^T\mathbf{B}\mathbf{x} x T Bx with respect to x \mathbf{x} x is 2 B x 2\mathbf{B}\mathbf{x} 2 Bx .
(b) Use the result of part (a) to prove (7.3.14) for real matrices.
✍ Suppose A ∈ R n × n \mathbf{A}\in\real^{n \times n} A ∈ R n × n , and define C \mathbf{C} C as in (7.3.7) .
(a) Suppose that v = [ x y ] \mathbf{v}=\begin{bmatrix} \mathbf{x} \\ \mathbf{y} \end{bmatrix} v = [ x y ] , and write the block equation C v = λ v \mathbf{C}\mathbf{v} = \lambda \mathbf{v} Cv = λ v as two individual equations involving both x \mathbf{x} x and y \mathbf{y} y .
(b) Suppose σ k \sigma_k σ k is a singular value of A \mathbf{A} A . By defining x \mathbf{x} x and y \mathbf{y} y in terms of the singular vectors u k \mathbf{u}_k u k and v k \mathbf{v}_k v k , show that σ k \sigma_k σ k and − σ k -\sigma_k − σ k are both eigenvalues of C \mathbf{C} C .