Problem Set 1

Given a 3×2 matrix A \[\begin{bmatrix}1&2&3\\-1&0&4\end{bmatrix}\]

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

A <- matrix(c(1, 2, 3, -1, 0, 4), 2, 3, byrow = T)

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

Compute the eigenvalues and eigenvectors of X and Y using the built-in commands in R.

eigen_X <-eigen(X)
eigen_X
## eigen() decomposition
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
eigen_Y <- eigen(Y)
eigen_Y
## 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

Compute for A using the SVD command:



Left-singular Vectors

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

Singular Values

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

Right-singular Vectors

V <- svd(A)$v
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\)

N.B We see that the output for all vectors are the same but with some sign changes. The eigenvectors are scaled by −1.

left <- list(x = eigen_X$vectors, u=U)
left
## $x
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
## 
## $u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
right <- list(y = eigen_Y$vectors[,1:2], vt=V)
right
## $y
##             [,1]       [,2]
## [1,] -0.01856629 -0.6727903
## [2,]  0.25499937 -0.7184510
## [3,]  0.96676296  0.1765824
## 
## $vt
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

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

eigen_X$values[1:2]
## [1] 26.601802  4.398198
round(eigen_Y$values[1:2],6)
## [1] 26.601802  4.398198
list(eigenx_values = eigen_X$values, eigeny_values = eigen_Y$values[1:2], svd_sing_values = sigma^2)
## $eigenx_values
## [1] 26.601802  4.398198
## 
## $eigeny_values
## [1] 26.601802  4.398198
## 
## $svd_sing_values
## [1] 26.601802  4.398198

Problem Set 2

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

my_inverse <- function(my_matrix){
  if (nrow(my_matrix)!= ncol(my_matrix)){
    print(" Must be a square matrix!")
  }else  if(det(my_matrix) == 0){
    print("This is a singular matrix!")
  }else{  #create an empty matrix to hold cofactors later
    cat("\n\nThe determinant is ", det(my_matrix), "\n\n")
    
    #Calculate the cofactors
    cf <- matrix(nrow = nrow(my_matrix), ncol = ncol(my_matrix))
  
    for(i in 1:nrow(my_matrix)){
      for(j in 1:ncol(my_matrix)){
        cf[i, j] <- det(my_matrix[-i,-j])*(-1)^(i+j) #fill in cofactor matrix
      }
    }
  }
  
#Adjugate -- Transpose all elements in the cofactor matrix
adjugate <- t(cf)

#Multiply by 1/Determinant
the_inverse <- (1 / det(my_matrix)) * adjugate
return(the_inverse)
}



Let’s test out the function.

A <- matrix(c(2, 0, 3, -2, 3, -4, -3, 1, -4), 3, 3, byrow = T)
A
##      [,1] [,2] [,3]
## [1,]    2    0    3
## [2,]   -2    3   -4
## [3,]   -3    1   -4
B = my_inverse(A)
## 
## 
## The determinant is  5
B
##      [,1] [,2] [,3]
## [1,] -1.6  0.6 -1.8
## [2,]  0.8  0.2  0.4
## [3,]  1.4 -0.4  1.2




Show that \(A \times B = I\)

round(A %*% B)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1



Another example using 4 by 4 matrix

A1 <- matrix(c(0, 7, 1, -5, 3, 2, -1, 1, 1, 0, 0, 2, 2, -4, -2, 0), 4, 4, byrow = T)
A1
##      [,1] [,2] [,3] [,4]
## [1,]    0    7    1   -5
## [2,]    3    2   -1    1
## [3,]    1    0    0    2
## [4,]    2   -4   -2    0
B1 = my_inverse(A1)
## 
## 
## The determinant is  26
B1
##            [,1]       [,2]       [,3]       [,4]
## [1,]  0.6153846 -0.7692308  1.9230769  0.6923077
## [2,] -0.2307692  0.5384615 -0.8461538 -0.3846154
## [3,]  1.0769231 -1.8461538  3.6153846  0.9615385
## [4,] -0.3076923  0.3846154 -0.4615385 -0.3461538
round(A1 %*% B1)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1

Helpful Links: