In this problem, we’ll verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a 3 x 2 matrix A.
write code in R to compute X = AAT and Y = ATA. 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
X <- A %*% t(A)
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
Y <- t(A) %*% A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
#Eigen Vectors and Values
eigX <- eigen(X)
eigX
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
eigY <- eigen(Y)
eigY
## 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
#SVD
svd_A <- svd(A)
#Left Singular Vector
svd_A$u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
eigX$vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
#Right Singular Vector
svd_A$v
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
eigY$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
Vectors produced using the svd command to eigen vectors X and Y the first column in u is -1 * the first column of the eigen vecor of X. The second column in u is equal to the second column of the eignvector of X. Same for v.
sqrt(eigen(X)$values)
## [1] 5.157693 2.097188
svd_A$d
## [1] 5.157693 2.097188
This shows that the two non zero eigenvalues of X and Y are squares of the two non zero singularvalues of A (d).
Using the procedure outlined in section 1 of the weekly handout, write a function to compute the inverse of a well-conditioned full-rank square matrix using co-factors. In order to compute the co-factors, you may use built-in commands to compute the determinant. Your function should have the following signature:
B = myinverse(A)
where A is a matrix and B is its inverse and AxB = I. The off-diagonal elements of I should be close to zero, if not zero. Likewise, the diagonal elements should be close to 1, if not 1. Small numerical precision errors are acceptable but the function myinverse should be correct and must use co-factors and determinant of A to compute the inverse. Please submit PS1 and PS2 in an R-markdown document with your first initial and last name.
A <- matrix(c(1,4,5,2,5,9,3,2,7),nrow=3)
myinverse <- function(A){
C <- diag(1,nrow(A),ncol(A))
for (i in 1:nrow(A)) {
for (j in 1:ncol(A)){
Pij <- A[-i,-j]
C[i,j] <- ((-1)^(i+j))*det(Pij)
}
}
return(t(C)/det(A))
}
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 2
## [3,] 5 9 7
myinverse(A)
## [,1] [,2] [,3]
## [1,] 1.2142857 0.92857143 -0.7857143
## [2,] -1.2857143 -0.57142857 0.7142857
## [3,] 0.7857143 0.07142857 -0.2142857
A%*%myinverse(A)
## [,1] [,2] [,3]
## [1,] 1.000000e+00 5.551115e-16 2.220446e-16
## [2,] 4.440892e-15 1.000000e+00 4.440892e-16
## [3,] 6.217249e-15 2.442491e-15 1.000000e+00