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 A =1 2 3 −1 0 4
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,2,3,-1,0,4), nrow = 2, byrow = TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
# compute X
X <- A%*%t(A)
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
# compute Y
Y <- t(A)%*%A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
# compute eigenvalues and eigenvectors of X
Ex <- eigen(X)
Ex
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
# compute eigenvalues and eigenvectors of Y
Ey <- eigen(Y)
Ey
## 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
Sa <- svd(A)
Sa
## $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
When we look at the eigen vector of X, we see that it is the same as left singular vector of A with the exception of -(1) multiplication of eigenvector of X. When we look at the eigen vector of Y, we see that is the same as right singular vector of A with the exception of -(1) multiplication of eigenvector of Y.
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 A×B = 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.
Matrix must be square matrix.
Matrix must be inversible (determinant != 0).
AXB=I
B=myinverse(A)
The matrix that we are trying to find the inverse of is <- A
Co factor of matrix (each element i and j) <- C
Inverse of matrix A <- B
We need to find the cofactors of each element in matrix A and transpose this matrix and then divide them by the determinant of matrix A in order to get the inverse of matrix A. So the formula is:
\[A^{-1}=C_{i,j}^T/det(A)\]
To calculate the cofactors of Matrix A, we use the below formula;
\[A_{i,j}=(-1)^{i+j}*M_{ij}\]
myinverse <- function(A) {
m <- dim(A)[1] # check to see if the matrix is square
if (m!=dim(A)[2]) {
return("Matrix must be square matrix")
}
else if (det(A)==0) { # check to see if the matrix is inversible.
return("Matrix must be inversible")
}
C <- matrix(0, nrow = m, ncol = m) # cofactors for mxm matrix starting with 0 values.
for(i in 1:m) { # loop through to place [i,j] values in mxm matrix to find the cofactors
for(j in 1:m) { # loop through the determinant of the minors (starting from M11 , and 1 row)
C[i,j] <- (-1)^(i+j)*det(matrix(A[-i,-j], nrow =m-1))
}
}
B <- t(C)/det(A) # using the formula to find the inverse of matrix A
return(B)
}
B <- myinverse(A)
B
## [1] "Matrix must be square matrix"