library(Matrix)
library(matrixcalc)
In this problem, we’ll verify using R that SVD and Eigenvalues are related as workedout in the weekly module. Given a 3×2 matrix A
\[ A = \begin{bmatrix}1&2&3\\-1&0&4\end{bmatrix} \\ \]
# Setup the 3x2 matrix A
A <- matrix(c(1,2,3,-1,0,4), nrow=2, byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
# Compute X
X <- t(A)%*%A
X
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
# Compute Y
Y <- A%*%t(A)
Y
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
# eigen vectors of X
eX <- eigen(X)
eX
## 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
#eigen vectors of Y
eY <- eigen(Y)
eY
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
Singular Value Decomposition (SVD) is a factorization of a real or complex matrix that generalizes the eigen decomposition of a square normal matrix to any \(m\times n\) matrix.
SVD
# A is the matrix where single value decomposition is to be computed
svdA <- svd(A)
# d is the vector containing the singular values of A
svdA$d
## [1] 5.157693 2.097188
# u is a matrix whose columns contain the left singular vectors of A
svdA$u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
# v is a matrix whose columns contain the right singular vectors of A
svdA$v
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
# Compare u and eY
svdA$u == eY$vectors
## [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE TRUE
UL <- svdA$u
UL[,1] = -UL[,1]
round(UL,digits=5) == round(eY$vectors,digits=5)
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
# compare v with the eigen value of X (2 bottom columns)
svdA$v == eX$vectors[3:2]
## [,1] [,2]
## [1,] FALSE FALSE
## [2,] FALSE FALSE
## [3,] FALSE FALSE
# Compare v
VL <- svdA$v
VL[,1] = -VL[,1]
round(VL,digits=3) == round(eX$vectors[1:3,1:2],digits=3)
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [3,] TRUE TRUE
# Compare the d values
v1=svdA$d^2
v1
## [1] 26.601802 4.398198
round(v1,digits=5) == round(eX$values[1:2],digit=5)
## [1] TRUE TRUE
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.
myinverse = function(A){
if(dim(A)[1] != dim(A)[2]) {
# Check if square
return('ERROR : Matrix is not square')
}
else if(det(A) == 0) {
# Check if determinant is 0
return('ERROR : Matrix is singular')
}
# Create co-factor matrix
coMatrix = A * 0
# Cofactoring
for (i in 1:ncol(A)) {
for (j in 1:nrow(A)) {
coMatrix[i,j] = det(A[-i,-j]) * (-1)^(i+j)
}}
inversed = t((coMatrix)/det(A))
return(inversed)
}
# Non square matrix should give error
nonsquaretest = matrix(c(1,5,4,3,7,3,2,1,4,2,1,4),nrow=3)
nonsquaretest
## [,1] [,2] [,3] [,4]
## [1,] 1 3 2 2
## [2,] 5 7 1 1
## [3,] 4 3 4 4
myinverse(nonsquaretest)
## [1] "ERROR : Matrix is not square"
# Singular matrix should give error
singulartest = matrix(c(0,0,0,0),nrow=2)
singulartest
## [,1] [,2]
## [1,] 0 0
## [2,] 0 0
myinverse(singulartest)
## [1] "ERROR : Matrix is singular"
# Create a 4x4 matrix
A = matrix(c(1,6,7,3,9,3,1,5,1,3,1,9,2,3,1,2),nrow=4)
A
## [,1] [,2] [,3] [,4]
## [1,] 1 9 1 2
## [2,] 6 3 3 3
## [3,] 7 1 1 1
## [4,] 3 5 9 2
# Inverse the matrix
B = myinverse(A)
B
## [,1] [,2] [,3] [,4]
## [1,] 1.708035e-18 -0.06666667 0.200000000 0.00000000
## [2,] 1.346154e-01 -0.13076923 0.084615385 0.01923077
## [3,] -5.769231e-02 -0.04871795 -0.007692308 0.13461538
## [4,] -7.692308e-02 0.64615385 -0.476923077 -0.15384615
I = round(A %*% B)
I
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 1 0
## [4,] 0 0 0 1
# Check values
C = solve(A)
round(B,4)==round(C,4)
## [,1] [,2] [,3] [,4]
## [1,] TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE