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 x 2 matrix A

\[ A = \begin{bmatrix} 1 & 2 & 3 \\ -1 & 0 & 4 \end{bmatrix} \]

(A <- matrix(c(1,-1,2,0,3,4),nrow=2))
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
# Display Matrix A
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

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

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.

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)$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

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

svd_a <- svd(A)
#left-singular vectors
svd_a$u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
#singular values
svd_a$d
## [1] 5.157693 2.097188
#right-singular vectors
svd_a$v
##             [,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 .

Examining the results above, left-singular vectors (u) contains eigenvectors of X and right-singular vectors (v) contains eigen vectors of Y, although the first two columns of eignvectors of X and Y has the opposite sign of u and v they are equivalent as they simply represent scalar multiplication.

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 .

sq <- svd_a$d^2
sq
## [1] 26.601802  4.398198

The above statement is confirmed based on the results above.

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 x 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(x) {
  # check if matrix is square
  if(nrow(x) != ncol(x)) { return('Input only squre matrix')}
  # replace all values with zeros
  Matr <- matrix(rep(0,length(x)),ncol=ncol(x))
  #Find determinant and inverse
  for (i in 1:ncol(x)) {
    for (j in 1:nrow(x)) {
      Matr[i,j] <- det(x[-i,-j])*(-1)^(i+j) 
    }
  }
  return(t(Matr)/det(x)) 
}

A <- matrix(c(1,2,4,2,-1,3,4,0,1),nrow=3)
#Inverse using 'myinverse' function
(B <- myinverse(A))
##             [,1]       [,2]       [,3]
## [1,] -0.02857143  0.2857143  0.1142857
## [2,] -0.05714286 -0.4285714  0.2285714
## [3,]  0.28571429  0.1428571 -0.1428571
solve(A)
##             [,1]       [,2]       [,3]
## [1,] -0.02857143  0.2857143  0.1142857
## [2,] -0.05714286 -0.4285714  0.2285714
## [3,]  0.28571429  0.1428571 -0.1428571