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 A

\[ A = \left(\begin{array}{cc} 1 & 2 & 3\\ -1 & 0 & 4 \end{array}\right) \] write code in R to compute \(X = AA^T\) and \(Y = A^TA\). Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commans 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.
A <- matrix(c(1,2,3,-1,0,4), nrow = 2, ncol = 3, byrow=TRUE)
X <- A%*%t(A)
Y <- t(A)%*%A
cat("A = ", A,
"\nX = AAT = ", X,
"\nY = ATA = ", Y,
"\n\nEigenvalue of X = ", eigen(X)$values,
"\nEigenvalue of Y = ", eigen(Y)$values,
"\n\nEigenvector of X = ", eigen(X)$vectors,
"\nEigenvector of Y = ", eigen(Y)$vectors,
"\n\nleft-singular of A = ", svd(A)$u,
"\nsingular of A = ", svd(A)$d,
"\nright-singular of A = ", svd(A)$v,
"\n\nIn addition,",
"\n(Non-zero singular of A)^2 = ", (svd(A)$d)^2,
"\nNon-zero Eigenvalue of X = ", eigen(X)$values,
"\nNon-zero Eigenvalue of Y = ", eigen(Y)$values[1:2],
"\n=> (Non-zero singular of A)^2 = Non-zero Eigenvalue of X = Non-zero Eigenvalue of Y")
## A =  1 -1 2 0 3 4 
## X = AAT =  14 11 11 17 
## Y = ATA =  2 2 -1 2 4 6 -1 6 25 
## 
## Eigenvalue of X =  26.6018 4.398198 
## Eigenvalue of Y =  26.6018 4.398198 1.058982e-16 
## 
## Eigenvector of X =  0.6576043 0.7533635 -0.7533635 0.6576043 
## Eigenvector of Y =  -0.01856629 0.2549994 0.966763 -0.6727903 -0.718451 0.1765824 0.7396003 -0.6471502 0.1849001 
## 
## left-singular of A =  -0.6576043 -0.7533635 -0.7533635 0.6576043 
## singular of A =  5.157693 2.097188 
## right-singular of A =  0.01856629 -0.2549994 -0.966763 -0.6727903 -0.718451 0.1765824 
## 
## In addition, 
## (Non-zero singular of A)^2 =  26.6018 4.398198 
## Non-zero Eigenvalue of X =  26.6018 4.398198 
## Non-zero Eigenvalue of Y =  26.6018 4.398198 
## => (Non-zero singular of A)^2 = Non-zero Eigenvalue of X = Non-zero Eigenvalue of Y


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.

Please submit PS1 and PS2 in an R-markdown document with your first initial and last name.
myinverse <- function(A) {
  if(nrow(A) == ncol(A) && det(A) != 0) {
    new_A <- matrix(NA, nrow = nrow(A), ncol = ncol(A), byrow=T)
    for(i in 1:nrow(A)){
      for(j in 1:ncol(A)){
        new_A[i,j] = (-1)^(i+j) * det(A[-i,-j]) 
      }
    }
    return((t(new_A)/det(A)))
  } else {
    return("Check if it's not square matrix nxn or if determinant is 0 or what went wrong!")
  }
}

A <- matrix(c(1, 2, 3, 1, 1, 1, 2, 0, 1),nrow=3,ncol=3,byrow=T)
B = myinverse(A)
cat("A = ", A,
"\n\nInverse of A from built-in library function = ", solve(A),
"\n\nB = Inverse of A from local function = ", B,
"\n\nInverse match check: ", abs(solve(A)-B)<=0.001,
"\n\nIdentity matrix = ", A%*%B,
"\n\nDiagonal elements check: ", (A%*%B-diag(3))<=0.009,"\n")
## A =  1 1 2 2 1 0 3 1 1 
## 
## Inverse of A from built-in library function =  -0.3333333 -0.3333333 0.6666667 0.6666667 1.666667 -1.333333 0.3333333 -0.6666667 0.3333333 
## 
## B = Inverse of A from local function =  -0.3333333 -0.3333333 0.6666667 0.6666667 1.666667 -1.333333 0.3333333 -0.6666667 0.3333333 
## 
## Inverse match check:  TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 
## 
## Identity matrix =  1 0 0 8.881784e-16 1 0 0 0 1 
## 
## Diagonal elements check:  TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
C = matrix(c(1,2,3,-1,0,4),nrow=2,ncol=3,byrow=T)
B = myinverse(C)
B
## [1] "Check if it's not square matrix nxn or if determinant is 0 or what went wrong!"