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 = \left(\begin{array}{cc} 1 & 0\\ -1 & 3 \\ 2 & 4 \end{array}\right) \]

write code in R to compute X = AA^T and Y = A^TA.

A = matrix(c(1, -1, 2, 0, 3, 4), ncol=3, nrow=2)
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

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

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

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

SVD <- svd(A)
#singular values 
singular_values <- SVD$d
singular_values
## [1] 5.157693 2.097188
# left singular values 
left_singular_values <- SVD$u
left_singular_values
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
SVD <- svd(A)
# right singular values 
right_singular_values <- SVD$v
right_singular_values
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y.

abs(round(right_singular_values[,1], 5)) == abs(round(vectors_X[,1], 5))
## Warning in abs(round(right_singular_values[, 1], 5)) ==
## abs(round(vectors_X[, : longer object length is not a multiple of shorter
## object length
## [1] FALSE FALSE FALSE
abs(round(right_singular_values[,2], 5)) == abs(round(vectors_X[,2], 5))
## Warning in abs(round(right_singular_values[, 2], 5)) ==
## abs(round(vectors_X[, : longer object length is not a multiple of shorter
## object length
## [1] FALSE FALSE FALSE
abs(round(right_singular_values[,1], 5)) == abs(round(vectors_Y[,1], 5))
## [1] TRUE TRUE TRUE
abs(round(right_singular_values[,2], 5)) == abs(round(vectors_Y[,2], 5))
## [1] TRUE TRUE TRUE

From above we can see that the right singular vector contains 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.

d_sq <- SVD$d^2
d_sq
## [1] 26.601802  4.398198

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 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.

myinverse <- function(A) {
  ncol = length(A[1,])
  nrow = length(A[,1])
  loop = seq(1, ncol, by=1)
  cofactor <- matrix(0:0, ncol, nrow)
  if (ncol == nrow){
    for (i in loop) {
      for (j in loop) {
        cofactor[i,j] <- det(A[-i,-j])*(-1)^(i+j)
    }
    }
  }
  else {
    print ("Matrix is not a square")
  }
  Ainverse <- t(cofactor)/det(A)
  return(Ainverse)
}
A <- matrix(c(7, 1, 3, 2, 4, 6, 1, 0, 1), ncol = 3, nrow=3)
B = myinverse(A)
B
##       [,1] [,2]  [,3]
## [1,]  0.20  0.2 -0.20
## [2,] -0.05  0.2  0.05
## [3,] -0.30 -1.8  1.30
solve(A)
##       [,1] [,2]  [,3]
## [1,]  0.20  0.2 -0.20
## [2,] -0.05  0.2  0.05
## [3,] -0.30 -1.8  1.30
round(A%*%B, 5)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1