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
A <- matrix(c(1,2,3,-1,0,4), byrow = T, ncol = 3)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
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 commands in R.
X <- A %*% t(A)
Y <- t(A) %*% A
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
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.
eigenX <- eigen(X)
The Eigenvalues of X are: \(\lambda_1 =\) 26.6018017 and \(\lambda_2 =\) 4.3981983
The vectors are given by:
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
For Y
eigenY <- eigen(Y)
The Eigenvalues of Y are: \(\lambda_1 =\) 26.6018017 and \(\lambda_2 =\) 4.3981983 \(\lambda_3 =\) 0
The vectors are given by:
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
The formula for SVD is:
\[ X = UDV\]
svdA <- svd(A, nu = dim(A)[1], nv = dim(A)[2])
svdA
## $d
## [1] 5.157693 2.097188
##
## $u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
##
## $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
all.equal(eigenX$vectors, svdA$u)
## [1] "Mean relative difference: 2"
Given that the differences in the matricies is caused by one vector being negative we can say that they are the same because a scalar multiplication will not change the value of the eigenvectors.
all.equal(sqrt(eigenX$values[1]), svdA$d[1])
## [1] TRUE
all.equal(sqrt(eigenX$values[2]), svdA$d[2])
## [1] TRUE
We see that the square root of eigenvalues of X are equal the singular values of A.
identical(eigenX$vectors, svdA$u)
## [1] FALSE
eigenX$vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
svdA$u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
While the two matrixies are not identical, the vector v1 is simply multiplied by -1. Since this does not change the igenvalues we see that this still holds true.
all.equal(abs(eigenY$vectors), abs(svdA$v))
## [1] TRUE
We also see that the absolute values of the eigenvectors of Y are equal to the absolute values of the right singular values of A.
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 AB = I. The o-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.
myinverse <- function(A) {
detA <- det(A)
C <- matrix(0L, nrow = nrow(A), ncol = ncol(A))
if (nrow(A) / ncol(A) != 1){
stop('The matrix is not square. Please provide a square matrix.')
}
if (det(A) == 0) {
stop('The Matrix is not invertable')
}
for (i in 1:nrow(A)){
for (j in 1:ncol(A)){
if (ncol(A) == 2){
C[i,j] = A[-i, -j] * (-1)^(i+j)
}
else {
C[i,j] = det(A[-i, -j]) * (-1)^(i+j)
}
}
}
invA = 1/det(A) * t(C)
return(invA)
}
M2 <- matrix(floor(runif(4, 1,11)) , 2)
M3 <- matrix(floor(runif(9, 1,11)) , 3)
M4 <- matrix(floor(runif(16, 1,11)) , 4)
M10 <- matrix(floor(runif(100, 1,11)) , 10)
Mlist <- list(M2, M3, M4, M10)
lapply(Mlist, function(x) myinverse(x))
## [[1]]
## [,1] [,2]
## [1,] 0.1590909 -0.02272727
## [2,] -0.1136364 0.15909091
##
## [[2]]
## [,1] [,2] [,3]
## [1,] 0 -0.02272727 0.2045455
## [2,] -1 0.75000000 0.2500000
## [3,] 1 -0.63636364 -0.2727273
##
## [[3]]
## [,1] [,2] [,3] [,4]
## [1,] 0.36831683 -0.2 -0.001980198 -0.00990099
## [2,] 0.37623762 0.0 0.148514851 -0.25742574
## [3,] -0.59603960 0.2 -0.324752475 0.37623762
## [4,] 0.00990099 0.0 0.188118812 -0.05940594
##
## [[4]]
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.36120870 -0.29294917 0.16003519 0.114637737 0.30245101
## [2,] -0.33564839 -0.12290593 0.10803991 0.018522786 -0.01659330
## [3,] 0.32998619 0.07335054 -0.11165221 -0.107380480 -0.08097626
## [4,] -0.13389462 -0.10420612 0.04516018 0.112436441 0.06826412
## [5,] -0.23798442 -0.14507879 0.08675933 0.022249988 0.21667511
## [6,] 0.21095079 0.19362564 -0.18983312 -0.006695529 0.01263903
## [7,] 0.04012616 0.05101071 0.01335644 -0.054868464 0.06376341
## [8,] 0.39187730 0.37600559 -0.11584966 -0.054673742 -0.15938029
## [9,] 0.09635850 -0.03593059 -0.09558173 -0.006886126 -0.11826749
## [10,] 0.04517247 0.02706364 0.04418304 -0.021962678 -0.20552864
## [,6] [,7] [,8] [,9] [,10]
## [1,] 0.05353238 0.33624010 -0.04604687 -0.1277427554 -0.13268427
## [2,] 0.11820131 0.31863887 -0.16761008 0.0392485315 -0.04492038
## [3,] -0.02287931 -0.20749228 0.20314852 -0.0402169968 0.04289263
## [4,] 0.02479968 0.16659763 -0.14734607 -0.0009737336 -0.03443962
## [5,] -0.02536858 0.28905161 0.09946700 -0.1089598904 -0.16492287
## [6,] -0.08052929 -0.21904228 0.09959627 -0.0930332036 0.15218192
## [7,] -0.01022818 -0.03445318 0.02864271 -0.0083809764 -0.02614916
## [8,] -0.12439136 -0.39094892 0.22348818 -0.0986889828 0.04981805
## [9,] 0.08746347 -0.12597178 -0.03549722 0.1324213581 0.09170778
## [10,] -0.01207120 -0.12198955 -0.16553932 0.2485244148 0.12369542
lapply(Mlist, function(x) round(myinverse(x) %*% x, 5))
## [[1]]
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
##
## [[2]]
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
##
## [[3]]
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 1 0
## [4,] 0 0 0 1
##
## [[4]]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 0 0 0 0 0 0 0 0 0
## [2,] 0 1 0 0 0 0 0 0 0 0
## [3,] 0 0 1 0 0 0 0 0 0 0
## [4,] 0 0 0 1 0 0 0 0 0 0
## [5,] 0 0 0 0 1 0 0 0 0 0
## [6,] 0 0 0 0 0 1 0 0 0 0
## [7,] 0 0 0 0 0 0 1 0 0 0
## [8,] 0 0 0 0 0 0 0 1 0 0
## [9,] 0 0 0 0 0 0 0 0 1 0
## [10,] 0 0 0 0 0 0 0 0 0 1