1. Problem Set 1

(1) write code in R to compute X = AAT and Y = ATA. Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commans in R.

Creating the matrix A

A<-matrix(c(1,2,3,-1,0,4),2,3,byrow=TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

Computing matix X

X<-A%*%t(A)  
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17

Computing matrix Y

Y<-t(A)%*%A
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

Computing eiegenvalues for X and Y

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

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

left-singular represented as u

singular values represented as d

right-singular represented as v

svdA<-svd(A)
svdA
## $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

Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y.

Comparing left-singular represented as u to eigenvectors X represented as egxvectors

svdA$u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
egx$vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043

Comparing right-singular represented as v to eigenvectors Y represented as egy$vectors

svdA$v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
egy$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

For both comparisons, the first column is not the same, one of them is negative while the other is positive. But for the second columns in both comparions they appear to have the same values.

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 on-zero singular values of A.

squaring the singular values represented as d and comparing to eigenvalues of X represented as egxvalues and eigenvalues of Y represented as egyvalues

svdA$d^2
## [1] 26.601802  4.398198
egx$values
## [1] 26.601802  4.398198
egy$values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16

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.

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.

create square matrix and myinverse function then run function

A<-matrix(c(1,0,3,1,3,1,1,2,1),nrow=3,byrow=TRUE)

myinverse<-function(x) {
  cofactorm <- matrix(0,nrow=nrow(x),ncol=ncol(x),byrow=TRUE)
  for (i in 1:ncol(x)) {
    for (j in 1:nrow(x)) {
      cofactorm[i,j] <- det(x[-i,-j])*(-1)^(i+j) 
    }
  }
  return(t(cofactorm)/det(x)) 
}

B<-myinverse(A)

B
##      [,1] [,2] [,3]
## [1,] -0.5   -3  4.5
## [2,]  0.0    1 -1.0
## [3,]  0.5    1 -1.5

identity matrix

I<-A%*%B
I
##               [,1]          [,2]         [,3]
## [1,]  1.000000e+00 -4.440892e-16 0.000000e+00
## [2,] -1.110223e-16  1.000000e+00 8.881784e-16
## [3,] -1.110223e-16 -2.220446e-16 1.000000e+00