Problem set 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 x 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^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.
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
compute, \[X = AA^T\]
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
compute, \[Y = A^TA\]
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
## [1] 26.601802 4.398198
## [1] 2.660180e+01 4.398198e+00 5.465713e-17
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
## [1] 5.157693 2.097188
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
The left-singular vectors (svd_a$u
) contains eigenvectors of X and right-singular vectors (svd_a$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.
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.
## [1] 26.601802 4.398198
From the aboove result we can see the two non-zero eigenvalues of both X and Y are the same and are squares of the non-zero singular values of A.
Problem set 2
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.
myinverse <- function(M) {
# Vaidate if matrix is square
if(nrow(M) != ncol(M)) { return('This function Needs a square matrix')}
cofac_M <- matrix(rep(0,length(M)),ncol=ncol(M))
for (i in 1:ncol(M)) {
for (j in 1:nrow(M)) {
cofac_M[i,j] <- det(M[-i,-j])*(-1)^(i+j)
}
}
return(t(cofac_M)/det(M))
}
Test
## [1] "This function Needs a square matrix"
## [,1] [,2] [,3]
## [1,] -0.5555556 -0.8888889 1.1111111
## [2,] 0.2222222 0.5555556 -0.4444444
## [3,] 0.3333333 -0.6666667 0.3333333
Verify
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [,1] [,2] [,3]
## [1,] 0.98 0.01 0.01
## [2,] -0.02 1.02 0.02
## [3,] -0.03 0.02 1.02