In this problem, we’ll verify using R that SVD and Eigenvalues are elated as worked out in the weekly module. Given a 3 x 2 matrix A.
\[\mathbf{A} = \left[\begin{array} {rrr} 1 & 2 & 3\\ -1 & 0 & 4 \end{array}\right]\]
Write code in R to compute \(X=AA^T \space and \space Y=A^TA\) Then, Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commands in R.
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
#Transpose matrix A
AT <- t(A)
AT
## [,1] [,2]
## [1,] 1 -1
## [2,] 2 0
## [3,] 3 4
X <- A %*% AT
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
Y <- AT %*% A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
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.
SVD_A <- svd(A)
SVD_A
## $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
Find the eigen values of \(X = AA^T\)
eigenX <- eigen(X)
eigenX$values
## [1] 26.601802 4.398198
Now, findin the eigen vectors
eigenX$vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
Computing singular values
singular_X <- sqrt(eigenX$values)
singular_X
## [1] 5.157693 2.097188
Now, examining if both X and Y are the same and are squares of the non-zero singular values of A.
SVD_A$d
## [1] 5.157693 2.097188
Find the eigen values of \(Y = A^TA\)
eigenY <- eigen(Y)
eigenX$values
## [1] 26.601802 4.398198
Finding eigen vectors
eigenY$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
Finally calculating singular values
singular_Y <- sqrt(eigenY$values)
singular_Y
## [1] 5.157693e+00 2.097188e+00 1.029068e-08
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 o-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.
library(generalCorr)
## Warning: package 'generalCorr' was built under R version 3.4.4
## Loading required package: np
## Warning: package 'np' was built under R version 3.4.4
## Nonparametric Kernel Methods for Mixed Datatypes (version 0.60-8)
## [vignette("np_faq",package="np") provides answers to frequently asked questions]
## [vignette("np",package="np") an overview]
## [vignette("entropy_np",package="np") an overview of entropy-based methods]
## Loading required package: xtable
## Loading required package: meboot
## Warning: package 'meboot' was built under R version 3.4.4
## Loading required package: dynlm
## Warning: package 'dynlm' was built under R version 3.4.4
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.4.4
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: nlme
## Loading required package: psych
## Warning: package 'psych' was built under R version 3.4.4
myinverse <- function(A) {
co.factor <- matrix(c(0:0), nrow=nrow(A), ncol=ncol(A), byrow = T)
for (i in 1:nrow(A)) {
for (j in 1:ncol(A)){
co.factor[i,j] <- (-1)^(i+j)*det(A[-i,-j])
}
}
#co factors
print("co factors")
print(co.factor)
A.inv <- t(co.factor)/det(A)
#inverse
print("Inverse")
print(A.inv)
#identity
print("Identity")
print(round(A.inv %*% A, 0))
}
Z <- matrix(c(1,0,1
,2,4,0
,3,5,6),ncol=3,nrow = 3)
myinverse(Z)
## [1] "co factors"
## [,1] [,2] [,3]
## [1,] 24 5 -4
## [2,] -12 3 2
## [3,] -2 -5 4
## [1] "Inverse"
## [,1] [,2] [,3]
## [1,] 1.0909091 -0.54545455 -0.09090909
## [2,] 0.2272727 0.13636364 -0.22727273
## [3,] -0.1818182 0.09090909 0.18181818
## [1] "Identity"
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1