(1) In this problem, we’ll verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a 3 × 2 matrix A write code in R to compute X = AA^T and Y = A^TA. Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commans in R. Then, compute the left-singular, singular values, and right-singular vectors of A using the svd command. Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y. In addition, the two non-zero eigenvalues (the 3rd value will be very close to zero, if not zero) of both X and Y are the same and are squares of the non-zero singular values of A.Your code should compute all these vectors and scalars and store them in variables.Please add enough comments in your code to show me how to interpret your steps.
a <- matrix(c(1,-1,2,0,3,4),nrow=2,ncol=3)
a
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
Using built in R function svd, which takes an arbitrary matrix argument and calculates the singular value decomposition. d= eigenvalues u= left-singular eigenvectors v= right-singular eigenvectors
svd(a)
## $d
## [1] 5.157693 2.097188
##
## $u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
##
## $v
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
Computing for x = aa^t
#x = aa^t
x = a %*% t(a)
x
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
svd(x)
## $d
## [1] 26.601802 4.398198
##
## $u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
##
## $v
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
eigen(x)
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
computing for y = a^ta
#y = a^ta
y = t(a) %*% a
y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
svd(y)
## $d
## [1] 2.660180e+01 4.398198e+00 2.836098e-15
##
## $u
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
##
## $v
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 -0.7396003
## [2,] 0.25499937 -0.7184510 0.6471502
## [3,] 0.96676296 0.1765824 -0.1849001
eigen(y)
## eigen() decomposition
## $values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
As, you can see the the left-singular eigenvectors of a equal the eigenvectors of x and right-singular eigenvectors of a are very close to eigenvectors of y.
svd(a)$u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
svd(x)$u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
svd(x)$v
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
svd(a)$v
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
svd(y)$u
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
svd(y)$v
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 -0.7396003
## [2,] 0.25499937 -0.7184510 0.6471502
## [3,] 0.96676296 0.1765824 -0.1849001