Week 4 Data 605 Assignment

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

$\mathbf{A} = \left[\begin{array} {rrr} 1 & 2 & 3 \\ -1 & 0 & 4 \end{array}\right]$

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.

A <- matrix(c(1, 2, 3, -1, 0, 4), nrow = 2, byrow = TRUE)

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

Now, finding the eigen values and eigen vectors of X and Y.

lambdaX <- eigen(X)$values lambdaX ## [1] 26.601802 4.398198 lambdaY <- eigen(Y)$values
lambdaY
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
eigenV_X <- eigen(X)$vectors eigenV_X ## [,1] [,2] ## [1,] 0.6576043 -0.7533635 ## [2,] 0.7533635 0.6576043 $\mathbf{eigenV(X)} = \left[\begin{array} {rrr} 0.6576043 & -0.7533635 \\ 0.7533635 & 0.6576043 \end{array}\right]$ eigenV_Y <- eigen(Y)$vectors
eigenV_Y
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001

$\mathbf{eigenV(Y)} = \left[\begin{array} {rrr} -0.01856629 & -0.6727903 & 0.7396003 \\ 0.25499937 & -0.7184510 & -0.6471502 \\ 0.96676296 & 0.1765824 & 0.1849001 \end{array}\right]$

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.

svd <- svd(A)
d <- svd$d u <- svd$u
v <- svd\$v
print(d)
## [1] 5.157693 2.097188
print(u)
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
print(v)
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

$\mathbf{U} = \left[\begin{array} {rrr} -0.6576043 & -0.7533635 \\ -0.7533635 & 0.6576043 \end{array}\right]$

$\mathbf{V} = \left[\begin{array} {rrr} 0.01856629 & -0.6727903 \\ -0.25499937 & -0.7184510 \\ -0.96676296 & 0.1765824 \end{array}\right]$

Note that the U and V are equavalent to the eigen vectors of X and Y simultaneously.

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.

note that the square of the non-zero singular values of A(D) is same as eigen value of X.

$EigenVal(X) = (26.601802 , 4.398198 )$

$\mathbf{D} = \left[\begin{array} {rrr} 5.157693 & 2.097188 \end{array}\right]$

Problem 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(A) {
# Check whether the matrix is a square matrics
rows = nrow(A)
cols = ncol(A)
if(!(rows == cols & Matrix::rankMatrix(A)[1] == nrow(A))){
return("Not a square matric")
}
else
{
# Create an empty matrix with 0
newMatrics = matrix(rep(0, length(A)), ncol=cols, nrow = rows)
# fill in the newMatrix
# go first by row
for (i in 1:cols) {
# then by column
for (j in 1:rows) {
# exclude the entry to calculate determinate
determinant = det(A[-i,-j])
#  change the signs based on row and column
newMatrics[i,j] = determinant*((-1)^(i+j))
}
}
# Transpose matrix and divide by determinant
inverse = t(newMatrics)/det(A)
return(inverse)
}
}
A = matrix(c(1,2,3,-1,0,4,5,7,9), nrow = 3, byrow = T)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
## [3,]    5    7    9
B <- myinverse(A)
B
##            [,1]       [,2]       [,3]
## [1,] -3.1111111  0.3333333  0.8888889
## [2,]  3.2222222 -0.6666667 -0.7777778
## [3,] -0.7777778  0.3333333  0.2222222