Skip to main content

Worksheet 4.7 Worksheet: Singular Value Decomposition

For this worksheet, the reader is directed to Section 4.6. Further details may be found in Section 8.6 of Linear Algebra with Applications, by Keith Nicholson. (See also notebook by Dr. Juan H Klopper 1 .)
In Section 4.6 we saw that the singular_value_decomposition algorithm in SymPy does things a little bit differently than in Section 4.6. If we start with a square matrix A, the results are the same, but if A is not square, the decomposition A=PΞ£AQT looks a little different. In particular, if A is mΓ—n, the matrix Ξ£A defined in Section 4.6 will also be mΓ—n, but it will contain some rows or columns of zeros that are added to get the desired size.
The matrix Q is an orthogonal nΓ—n matrix whose columns are an orthonormal basis of eigenvectors for ATA. The matrix P is an orthogonal mΓ—m matrix whose columns are an orthonormal basis of Rm. (The first r columns of P are given by Aqi, where qi is the eigenvector of ATA corresponding to the positive singular value Οƒi.)
The algorithm provided by SymPy replaces Ξ£A by the rΓ—r diagonal matrix of nonzero singular values. (This is common in most algorithms, since we don’t want to bother storing data we don’t need.) The matrix Q is replaced by the nΓ—r matrix whose columns are the first r eigenvectors of ATA, and the matrix P is replaced by the mΓ—r matrix whose columns are the orthonormal basis for the column space of A. (Note that the rank of ATA is equal to the rank of A, which is equal to the number r of nonzero eigenvectors of ATA (counted with multiplicity).)
The product PΞ£AQT will be the same in both cases, and the matrix P is the same as well.
This time, rather than using the SymPy algorithm, we will work through the process outlined in Section 4.6 step-by-step. Let’s revisit Example 4.6.4. Let A=[11110βˆ’1]. First, we get the singular values:
Next, we get the eigenvalues and eigenvectors of ATA:
Now we need to normalize the eigenvectors, in the correct order. Note that the eigenvectors were listed in increasing order of eigenvalue, so we need to reverse the order. Note that L1 is a list of lists. The eigenvector is the third entry (index 2) in the list (eigenvalue, multiplicity, eigenvector). We also need to turn list elements into matrices. So, for example the second eigenvector is Matrix(L1[1][2]).
Next, we can assemble these vectors into a matrix, and confirm that it’s orthogonal.
We’ve made the matrix Q! Next, we construct Ξ£A. This we will do by hand. (Can you think of a way to do it automatically?)
Alternatively, you could do SigA = diag(L0[0],L0[1]).row_join(Matrix([0,0])). Finally, we need to make the matrix P. First, we find the vectors Aq1,Aq2 and normalize. (Note that Aq3=0, so this vector is unneeded, as expected.)
Note that the matrix P is already the correct size, because rank(A)=2dim⁑(R2). In general, for an mΓ—n matrix A, if rank(A)=r<m, we would have to extend the set {p1,…,pr} to a basis for Rm. Finally, we check that our matrices work as advertised.
For convenience, here is all of the above code, with all print commands (except the last one) removed.
from sympy import Matrix,BlockMatrix,init_printing
init_printing()
A = Matrix([[1,1,1],[1,0,-1]])
B=(A.T)*A
L0=A.singular_values()
L1=B.eigenvects()
R1=Matrix(L1[2][2])
R2=Matrix(L1[1][2])
R3=Matrix(L1[0][2])
Q1 = (1/R1.norm())*R1
Q2 = (1/R2.norm())*R2
Q3 = (1/R3.norm())*R3
Q = Matrix(BlockMatrix([Q1,Q2,Q3]))
SigA = diag(L0[0],L0[1]).row_join(Matrix([0,0]))
S1 = A*Q1
S2 = A*Q2
P1 = (1/S1.norm())*S1
P2 = (1/S2.norm())*S2
P = Matrix(BlockMatrix([P1,P2]))
P,SigA,Q,P*SigA*Q.T

1.

Compute the SVD for the matrices
[2βˆ’1110βˆ’2][2βˆ’1βˆ’131βˆ’1][11001210βˆ’2].
Note that for these matrices, you may need to do some additional work to extend the pi vectors to an orthonormal basis. You can adapt the code above, but you will have to think about how to implement additional code to construct any extra vectors you need.

2.

By making some very minor changes in the matrices in Worksheet Exercise 4.7.1, convince yourself that (a) those matrices were chosen very carefully, and (b) there’s a reason why most people do SVD numerically.

3.

Recall from Worksheet 3.5 that for an inconsistent system Ax=b, we wish to find a vector y so that Ax=y is consistent, with y as close to b as possible.
In other words, we want to minimize β€–Axβˆ’bβ€–, or equivialently, β€–Axβˆ’bβ€–2.

(a)

Let A=PΞ£AQT be the singular value decomposition of A. Show that
β€–Axβˆ’bβ€–=β€–Ξ£Ayβˆ’zβ€–,
where y=QTx, and z=PTb.

(b)

Show that setting yi={zi/Οƒi, if Οƒiβ‰ 00, if Οƒi=0 minimizes the value of β€–Ξ£Ayβˆ’zβ€–.

(c)

Recall that we set Ξ£A=[DA000], where DA is the diagonal matrix of nonzero singular values. Let us define the pseudo-inverse of Ξ£A to be the matrix Ξ£A+=[DAβˆ’1000].
Show that the solution to the least squares problem is given by x=A+b, where A+=QΞ£A+PT.
You have attempted 1 of 1 activities on this page.