Assignment 4

Problem Set 1

Problem 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

\[\begin{equation*} A = \mathbf{}\left[\begin{matrix} 1 & 2 & 3\\ -1 & 0 & 4\\ \end{matrix}\right] \end{equation*}\]

Write code in R to compute X = AAT and Y = ATA:

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

X = A%*%A_T
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
Y = A_T%*%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 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. Your code should compute all these vectors and scalars and store them in variables. Please add enough comments in your code to show me how to interpret your steps.

The eigen values & vectors of X are:

eigen_X <- eigen(X)

eigen_value_X <- eigen_X$values
eigen_value_X
## [1] 26.601802  4.398198
eigen_vector_X <- eigen_X$vectors
eigen_vector_X
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043

The eigen values and vectors of Y are:

eigen_Y <- eigen(Y)

eigen_value_Y <- eigen_Y$values
eigen_value_Y
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
eigen_vector_Y <- eigen_Y$vectors
eigen_vector_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

AV = USigma
t(V)AV = U * Sigma * t(V)
A = U * Sigma * t(V)
t(A) * A = V * t(Sigma) * t(U) * U * Sigma * t(V)
t(A) * A = V * t(Sigma) * Sigma * t(V)
t(A) * A = V * Sigma ^2 * t(V)

Here U & V are orthogonal matrices and Sigma is a diagonal matrix.
U is the eigenvectors of A * t(A)
V is the eigenvectors of t(A) * A
& Sigma ^2 are the square root of the eigen values of At(A).

Here we see calcuating the eigen vectors of X = A * t(A) are equal to the U value from svd().
We also see that the eigen vectors of Y = t(A) * A are equal to the V value from svd().
d represents the square root of the eigen values squared from X or A * t(A). The eigen values from Y or t(A) * A are 0, so we ignore those.

svd = svd(A)
svd
## $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
(svd$d)^2
## [1] 26.601802  4.398198

Problem Set 2

Problem 1

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.

#assume a 3x3 matrice to start
myinverse <- function(matrix){
  sum = 0
  
  num_rows = dim(matrix)[1]
  num_columns = dim(matrix)[2]
  
  B <- matrix(,nrow=num_rows,ncol=num_columns)
  
  for (i in 1:num_rows){
    for (j in 1:num_columns){
      M = matrix
      #delete the row
      M = M[-i,]
      #delete the column
      M = M[,-j]
    
    B[i,j] = (-1)^(i+j)*(matrix[i,j]*det(M))
  
    }
  }
  
  return(t(B)/det(A))
}

A = matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,byrow=TRUE)
myinverse(A)
##              [,1]          [,2]          [,3]
## [1,] -4.50360e+15  3.602880e+16 -3.152520e+16
## [2,]  1.80144e+16 -9.007199e+16  7.205759e+16
## [3,] -1.35108e+16  5.404320e+16 -4.053240e+16
C = matrix(c(2,2,6,4,8,6,8,1,9),nrow=3,byrow=TRUE)
myinverse(C)
##               [,1]          [,2]          [,3]
## [1,]  1.981584e+17 -7.205759e+16 -4.323456e+17
## [2,]  3.602880e+16 -3.602880e+17  1.801440e+16
## [3,] -5.404320e+17  1.261008e+17  1.080864e+17