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 × 2 matrix \(A\)
\[A=\begin{bmatrix}1&2&3\\-1&0&4\end{bmatrix} \] write a 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 commands 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.

#Construct and display matrix A
A <- matrix(c(1,2,3,-1,0,4), 2, 3, byrow = TRUE) 
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
#Compute X = AA^T
X <- A%*%t(A)
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
#Computer Y = A^TA
Y <- t(A)%*%A
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25
#Eigenvectors of X and Y
eigen(X)$vector
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
eigen(Y)$vector
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001
#Eigenvalues of X and Y
eigen(X)$value
## [1] 26.601802  4.398198
eigen(Y)$value
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
#Left-singular vectors of A
svd(A)$u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
#Singular values of A
svd(A)$'d'
## [1] 5.157693 2.097188
#Right-singular vectors of A
svd(A)$v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
#Check if U and V are eigenvectors of X and Y, respectively
abs(round(eigen(X)$vector, 5)) == abs(round(svd(A)$u, 5))
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
abs(round(eigen(Y)$vector[,1:2], 5)) == abs(round(svd(A)$v, 5))
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [3,] TRUE TRUE
#Check if two non-zero eigenvalues of X and Y are the same and are squares of the non-zero singular values of A
round((svd(A)$'d')^2, 5) == round(eigen(X)$value, 5) 
## [1] TRUE TRUE
round((svd(A)$'d')^2, 5) == round(eigen(Y)$value[1:2], 5) 
## [1] TRUE TRUE

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 \(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 function to compute the inverse of a well-conditioned full-rank square matrix using co-factors
myinverse <- function(x){
  if(nrow(x) != ncol(x)){
    return('Not a Square Matrix!')
  }
  if(det(x) == 0){
    return('Determinant should be non-zero!')
  }
  cofactor_matrix <- matrix(rep(0, length(x)), ncol = ncol(x))
  for(i in 1:ncol(x)){
    for(j in 1:ncol(x)){
      cofactor_matrix[i, j] <- det(x[-i, -j]) * (-1)^(i+j)
    }
  }
  return(t(cofactor_matrix) / det(x))
}

#Construct a matrix 
A <- matrix(c(8,7,7,8,1,2,9,4,6), 3, 3, byrow = TRUE)
A
##      [,1] [,2] [,3]
## [1,]    8    7    7
## [2,]    8    1    2
## [3,]    9    4    6
#Find the inverse of the matrix using the myinverse function
B <- myinverse(A)
B
##             [,1]       [,2]       [,3]
## [1,]  0.03076923  0.2153846 -0.1076923
## [2,]  0.46153846  0.2307692 -0.6153846
## [3,] -0.35384615 -0.4769231  0.7384615
#Find the identity matrix of matrix A and the inverse of the matrix A
round(A %*% B)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1