1. 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 = 1 2 3 -1 0 4 Write code in R to compute X = AAT and Y = ATA. 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.

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

#Print A

A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

Compute AAT and A=ATA

X <- A%*%t(A)
Y <- t(A)%*%A

# print X and Y
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

compute the eigenvalues and eigenvectors of X and Y using the built-in commans in R

eigen(X)$values
## [1] 26.601802  4.398198
eigen(X)$vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
eigen(Y)$values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
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

Eigen Values of X are equal to eigen values of Y. Eigen Value of X = [1] 26.601802 4.398198 Eigen Values of Y = 2.660180e+01 4.398198e+00

Compute the left-singular, singular values, and right-singular vectors of A using the svd command.

sqrt1 <- sqrt(eigen(X)$values[1])
sqrt2 <- sqrt(eigen(X)$values[2])

Diagonal 2x3 matrix is built using singular vector

S <- diag(c(sqrt1, sqrt2), nrow = 2, ncol = 3)
S
##          [,1]     [,2] [,3]
## [1,] 5.157693 0.000000    0
## [2,] 0.000000 2.097188    0

Decompose Matrix A using built-in svd function.

d <- svd(A)$d
d
## [1] 5.157693 2.097188

Left-Singular vectors

LV <- svd(A)$u
LV
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043

Right-Singular vector

RV <- svd(A)$v
RV
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

Left-singular Vector (LV) contains eigen vectors of X, so we will now compare the eigenvectors of X with the left-singular vectors.

x1 <- eigen(X)$vectors[,1]
x2 <- eigen(X)$vectors[,2]

LV1 <- LV[,1]
LV2 <- LV[,2]

round(x1 - (-1*LV1),12)
## [1] 0 0
round(x2 - LV2, 12)
## [1] 0 0

As shown above first column vector LV1 is -1 * first eigenvector of X. LV1 is an eigenvector of X. The 2nd column vector in LV2 is equal to the 2nd eigenvector of X.

Now comparing eigenvectors of Y with the right-singular vectors.

y1 <- eigen(Y)$vectors[,1]
y2 <- eigen(Y)$vectors[,2]

rv1 <- RV[,1]
rv2 <- RV[,2]

round(y1 - (-1*rv1), 12)
## [1] 0 0 0
round(y2 - rv2,12)
## [1] 0 0 0

hence above results concluded that the first column vector in rv1 is -1 * first eigenvector of Y and the 2nd column vector in rv2 is equal to the 2nd eigenvector of Y.

2. 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 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){
  
  ### Function to compute the inverse of well-conditioned full-rank square    matrix using co-factors.
  
  # temporary copy of input matrix 
  temp_A <- A
  
  # Find Dimension of A 
  a_row <- nrow(A)

  #  NA filled matrix with dimensions same as A 
  C <- matrix(data=NA,nrow=a_row,ncol=a_row)
  
  for (i in 1:a_row){
    # For loop to to determine corresponding submatrix M_ij, 
    # find determinant and store in C[i,j] 
    for (j in 1:a_row){
      M_ij <-temp_A[-i,-j]
      C[i,j] <- det(M_ij)
    }   
  }     
  
  # Determinant of A 
  det_A <- det(temp_A)
  
  # Inverse of A as 1/det(A)*t(C), in variable inv_A
  if (det_A != 0){
    inv_A <- t(C)/det_A
  }
  return(inv_A)
}

#Validate Function 
V <- matrix(c(1,2,5,2,-1,3,4,0,1), nrow=3, ncol=3, byrow = TRUE)

#print validation matrix

V
##      [,1] [,2] [,3]
## [1,]    1    2    5
## [2,]    2   -1    3
## [3,]    4    0    1
inv_V <- myinverse(V)

inv_V
##             [,1]        [,2]       [,3]
## [1,] -0.02564103  0.05128205  0.2820513
## [2,] -0.25641026 -0.48717949 -0.1794872
## [3,]  0.10256410 -0.20512821 -0.1282051
solve(V)
##             [,1]        [,2]       [,3]
## [1,] -0.02564103 -0.05128205  0.2820513
## [2,]  0.25641026 -0.48717949  0.1794872
## [3,]  0.10256410  0.20512821 -0.1282051