1 Problem Set

1.1

Let’s define matrix A

A <- matrix(c(1,-1,2,0,3,4), ncol = 3, nrow = 2)

A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

Let’s multiple A by trasnposed A

X<-A%*%t(A)

X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17

Let’s multiple transposed A by A

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

Let’s calculate eigenvalues and eigenvectors for X

evX <- eigen(X)

valuesX <- evX$values

valuesX
## [1] 26.601802  4.398198
vectorsX <- evX$vectors

vectorsX
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043

Let’s calculate eigenvalues and eigenvectors for Y

evY <- eigen(Y)

valuesY <- evY$values

valuesY
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
# As we can see eigenvalues of X and Y are the same

vectorsY <- evY$vectors

vectorsY
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001

Let’s calculate singular value decomposition of matrix A

svdA<-svd(A)

# Right singular matrix

svdAu<-svdA$u

svdAu
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043

As we can see, right singular vectors are the same as eigenvectors of X

Singular values

svdAd<-svdA$d

svdAd
## [1] 5.157693 2.097188

Left singular values

svdAv<-svdA$v

svdAv
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824

As we can see, right singular vectors are the same as 2 eigenvectors of Y

Let’s squire singular values

svdAdSq1<-svdA$d[1]^2

svdAdSq1
## [1] 26.6018
svdAdSq2<-svdA$d[2]^2

svdAdSq2
## [1] 4.398198

We can see that they are the same as eigenvalues of X and Y

2 Problem Set

Create a function to calculate inverse of a matrix

Solution:

myinverse<-function(A){
  C<-A
  for (i in (1:nrow(A))){
  for (j in (1:ncol(A))){
    ifelse(i==1,myrow<-c(2:nrow(A)),ifelse(i==nrow(A),myrow<-c(1:(nrow(A)-1)),myrow<-c(1:(i-1),(i+1):nrow(A))))
    ifelse(j==1,mycol<-c(2:ncol(A)),ifelse(j==ncol(A),mycol<-c(1:(ncol(A)-1)),mycol<-c(1:(j-1),(j+1):ncol(A))))
    
      temp<-as.matrix(A[myrow,mycol])
      
      
      C[i,j]<-(-1)^(i+j)*det(temp)/det(A)
      }}
  return(t(C))}

A<-matrix(c(1,3,2,4),nrow=2,ncol=2)

A
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
det(A)
## [1] -2
B<-myinverse(A)

B
##      [,1] [,2]
## [1,] -2.0  1.0
## [2,]  1.5 -0.5
A%*%B
##              [,1] [,2]
## [1,] 1.000000e+00    0
## [2,] 1.776357e-15    1