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

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

write code in R to compute \(\mathbf{X}\) = \(\mathbf{A}\mathbf{A}^\mathbf{T}\) and \(\mathbf{Y}\) = \(\mathbf{A}^\mathbf{T}\mathbf{A}\) Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commands in R.

A = matrix(c(1,2,3,-1,0,4),nrow=2,byrow=TRUE)
X = t(A)%*%A
X
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25
Y = A%*%t(A)
Y
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
EAX = eigen(X)
EAY = eigen(Y)
EAX
## eigen() decomposition
## $values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
## 
## $vectors
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001
EAY
## eigen() decomposition
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043

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.

svd(A)
## $d
## [1] 5.157693 2.097188
## 
## $u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
## 
## $v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
#Left-singular values vector U = vector Y?
svd(A)$u == EAY$vectors
##       [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE TRUE
UL = svd(A)$u
UL[,1] = -UL[,1]
round(UL,digits=5) == round(EAY$vectors,digits=5)
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
#Answer - Yes


#Right-singular values vector V = vector X?
svd(A)$v == EAX$vectors[3:2]
##       [,1]  [,2]
## [1,] FALSE FALSE
## [2,] FALSE FALSE
## [3,] FALSE FALSE
VL = svd(A)$v
VL[,1] = -VL[,1]
round(VL,digits=3) == round(EAX$vectors[1:3,1:2],digits=3)
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [3,] TRUE TRUE
#Answer - Yes



#Non-zeor eigenvalues of X and Y = square of non-ezor singular values of A ($d)?
DEV=svd(A)$d^2
DEV 
## [1] 26.601802  4.398198
round(DEV,digits=5) == round(EAX$values[1:2],digit=5)
## [1] TRUE TRUE
#Answer - Yes

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.

qr(A)$rank
## [1] 2
#function to find the inverse od any square matrix A with rows, cols = n
myinverse = function(A)
{
# get matrix dimensions 
  r = nrow(A)
  c = ncol(A)
# since input must be a square matrix, return an error if r not equal to c
  if (r != c)
  {
     return (-1)
  }
#initialize vector that will contain the elements of the cofactors (Cf) of matrix A
  v = vector()
#initialize Cofactor matrix Cf
  Cf=matrix(data=NA,nrow=r,ncol=c)
  for (i in 1:r)
  {
    for (j in 1:c)
    {
      for(p in 1:r)
      {
        for (q in 1:c)
    #determine the co-factor matrix (r-1,s-1)
          if (p != i & q!= j)
          {
             v =  append(v,A[p,q]) 
          }
      
      }
      print(v) 
    #convert the vector list to the co-factor matrix
      M = matrix(v,ncol=(c-1))
      print(M)
    #compute the cofactor for row i, column j of matrix A    
      Cf[i,j]=(-1)^(i+j)*det(M)
      print(Cf[i,j])
    #re-initialize matrix for the next element of A
      v = vector()
    }
  }
  #show the cofactor matrix of A
  print(Cf)
  #show the product of matrix for A X transpose of the Cofactor matrix
  print (A%*%t(Cf))
  #compute fo the determinant of A
  det_A = (A[1,]%*%Cf[1,])
  print(det_A)
  #compute for the inverse of matrix A
  print(t(Cf))
  print(class(det_A))
  print(class(t(Cf)))
  A_inv = (1/as.vector(det_A))*t(Cf)
  #return the inverse of matrix A
  return(A_inv)
}

A = matrix(c(0,1,5,3,-6,9,2,6,1),nrow=3,byrow=TRUE)
A_i = myinverse(A) 
## [1] -6  9  6  1
##      [,1] [,2]
## [1,]   -6    6
## [2,]    9    1
## [1] -60
## [1] 3 9 2 1
##      [,1] [,2]
## [1,]    3    2
## [2,]    9    1
## [1] 15
## [1]  3 -6  2  6
##      [,1] [,2]
## [1,]    3    2
## [2,]   -6    6
## [1] 30
## [1] 1 5 6 1
##      [,1] [,2]
## [1,]    1    6
## [2,]    5    1
## [1] 29
## [1] 0 5 2 1
##      [,1] [,2]
## [1,]    0    2
## [2,]    5    1
## [1] -10
## [1] 0 1 2 6
##      [,1] [,2]
## [1,]    0    2
## [2,]    1    6
## [1] 2
## [1]  1  5 -6  9
##      [,1] [,2]
## [1,]    1   -6
## [2,]    5    9
## [1] 39
## [1] 0 5 3 9
##      [,1] [,2]
## [1,]    0    3
## [2,]    5    9
## [1] 15
## [1]  0  1  3 -6
##      [,1] [,2]
## [1,]    0    3
## [2,]    1   -6
## [1] -3
##      [,1] [,2] [,3]
## [1,]  -60   15   30
## [2,]   29  -10    2
## [3,]   39   15   -3
##               [,1]          [,2]          [,3]
## [1,]  1.650000e+02  1.776357e-15 -1.776357e-15
## [2,] -2.842171e-13  1.650000e+02 -3.197442e-14
## [3,] -3.907985e-14 -7.105427e-15  1.650000e+02
##      [,1]
## [1,]  165
##      [,1] [,2] [,3]
## [1,]  -60   29   39
## [2,]   15  -10   15
## [3,]   30    2   -3
## [1] "matrix"
## [1] "matrix"
#print the inverse of matrix A
print(A_i)
##             [,1]        [,2]        [,3]
## [1,] -0.36363636  0.17575758  0.23636364
## [2,]  0.09090909 -0.06060606  0.09090909
## [3,]  0.18181818  0.01212121 -0.01818182
#print the identity matrix (product of A and its inverse)
print(A%*%A_i)
##               [,1]          [,2]          [,3]
## [1,]  1.000000e+00  6.938894e-18 -1.387779e-17
## [2,] -1.554312e-15  1.000000e+00 -1.387779e-16
## [3,] -3.053113e-16 -7.459311e-17  1.000000e+00

A = ??? ??? 0 1 5 3 ???6 9 2 6 1 ??? ??? .