Problem Set 1

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

\[A = \left[\begin{array} {rr} 1 & 2 & 3\\ -1 & 0 & 4 \end{array}\right]\]

A <- matrix(c(1, -1, 2, 0, 3, 4), ncol = 3, byrow = F)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
#Find X
X <- A %*% t(A)
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
#Find Y
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 commans in R

#eigenvalue of X
evalX <- eigen(X)$values
evalX
## [1] 26.601802  4.398198
#eigenvector of X
evectX <- eigen(X)$vectors
evectX
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
#eigenvalue of Y
evalY <- eigen(Y)$values
evalY
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
#eigenvector of Y
evectY <- eigen(Y)$vectors
evectY
##             [,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 the left-singular, singular values, and right-singular vectors of A using the svd command (Got some assistance on this one with this article: http://www.endmemo.com/program/R/svd.php)

singularvalue <- svd(A)$d
singularvalue
## [1] 5.157693 2.097188
leftsingularvector <- svd(A)$u
leftsingularvector
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
rightsingularvector <- svd(A)$v
rightsingularvector
##             [,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.

print(leftsingularvector) 
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
print(evectX)
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043

The negative values of U represent the scaler.

print(rightsingularvector)
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
print(evectY[,1:2])
##             [,1]       [,2]
## [1,] -0.01856629 -0.6727903
## [2,]  0.25499937 -0.7184510
## [3,]  0.96676296  0.1765824

The negative value represents the scaler.

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.

print(evalX) 
## [1] 26.601802  4.398198
print(singularvalue^2)
## [1] 26.601802  4.398198
print(evalY[1:2])
## [1] 26.601802  4.398198
print(singularvalue^2)
## [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)\)

myinverse <- function (A){
  a <- is.numeric(a)
  a <- matrix(nrow(A) == ncol(A))
  if(det(A)!=0){
    cofactor<- matrix(0:0,nrow = nrow(A),ncol = ncol(A))
      for(i in 1:a){
        for(j in 1:a){
          detA<-det(A[-i,-j])
          cofactors[i,j]<- detA*(-1^(i+j))
        }}}
return(t(cofactor)/det(A))
}

Now I’ll try running the program

C <- matrix(c(1,2,3,4), ncol = 2)
C
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

I kept running into errors in my code and I’m not sure how to fix them.