Data 605 Assignment Four
Problem Set One
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 =\begin{bmatrix}1 & 2 & 3 \\-1 & 0 & 4\end{bmatrix}\]
write code in R to compute \[X = AA^{T}\] and \[Y = A^{T}A\] 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.
Matrix A
(A <- matrix(c(1,-1,2,0,3,4),nrow=2))
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
\[X = AA^{T}\]
(X <- A %*% t(A))
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
\[Y = A^{T}A\]
(Y <- t(A) %*% A)
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
Eigenvalues
(lambda_x <- eigen(X)$values)
## [1] 26.601802 4.398198
(lambda_y <- eigen(Y)$values)
## [1] 2.660180e+01 4.398198e+00 -6.098625e-16
Eigenvectors
(v_x <- eigen(X)$vectors)
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
(v_y <- eigen(Y)$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
Left and Right Singular Values of A
(svd_a <- 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
Examine results of the singular vectorsa
By examining the results above, we can see left-singular vectors (u) contains eigenvectors of X and right-singular vectors (v) contains eigen vectors of Y, although the first two columns of eignvectors of X and Y has the opposite sign of u and v they are equivalent as they simply represent scalar multiplication.
Problem Set Two
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.
Create a Random Square Matrix
n <- round(runif(1,2,4))
(A <- matrix(sample.int(10, size = n^2, replace = TRUE), ncol = n))
## [,1] [,2] [,3]
## [1,] 8 7 7
## [2,] 8 1 2
## [3,] 9 4 6
Create Function to take Inverse of Matrix
myinverse <- function(x) {
# check if matrix is square
if(nrow(x) != ncol(x)) { return('takes only squre matrix as input')}
cofactor_Matrix <- matrix(rep(0,length(x)),ncol=ncol(x))
for (i in 1:ncol(x)) {
for (j in 1:nrow(x)) {
cofactor_Matrix[i,j] <- det(x[-i,-j])*(-1)^(i+j)
}
}
return(t(cofactor_Matrix)/det(x))
}
Create Inverse Using myinverse created function and also solve function
(B <- myinverse(A))
## [,1] [,2] [,3]
## [1,] 0.03076923 0.2153846 -0.1076923
## [2,] 0.46153846 0.2307692 -0.6153846
## [3,] -0.35384615 -0.4769231 0.7384615
(sol_B <- solve(A))
## [,1] [,2] [,3]
## [1,] 0.03076923 0.2153846 -0.1076923
## [2,] 0.46153846 0.2307692 -0.6153846
## [3,] -0.35384615 -0.4769231 0.7384615
Confirm that the two are equal
round(B, 3) == round(sol_B, 3)
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE