Problem Set 1)
In this problem, we will verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a 3 x 3 matrix A \[ A=\begin{bmatrix} 1 & 2 & 3 \\ -1 & 0 & 4 \end{bmatrix} \] Write code in r to compute \[ X=AA^{T}\\ Y=A^{T}A \]
Then compute the eigenvalues and eigenvectors of X and Y using build in commands 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 defined as shown below:
library(matlib)
(A <- matrix(c(1,2,3,
-1,0,4), nrow=2, byrow = TRUE))
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
We can compute X per the problem requirements as shown below, and we will multiply a by it’s transpose
(X_Compute<-A%*%t(A))
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
We now proceed to compute Y using our previous function
(Y_Compute<-t(A)%*%A)
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
The eigen values for the resultant X and Y matrices with R’s built-in function as shown below:
eigen(X_Compute)
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
eigen(Y_Compute)
## 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
We notice that Y is close to a singular matrix based on eigen values and we can compute the singular vector decomposition as shown below Compute the SVD
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
We can store all of our values in the variables as shown in the r script below and use the all.equal command in R. if this returns true, we prove that these eigen values are indeed true.
singular<-svd(A)$d
eigen_X <- eigen(X_Compute)
eigenvalues_X<-eigen_X$values
eigen_Y <- eigen(Y_Compute)
eigenvalues_Y<-eigen_Y$values
all.equal(eigenvalues_X,(singular^2))
## [1] TRUE
Checking for Y
all.equal(eigenvalues_Y[1:2],(singular^2))
## [1] TRUE
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.
myinverse<-function(A)
{
#first we get how many rows of A exist
numrowA<-nrow(A)
A2<-A
# we initialize an empty matrix based on number of rows previously determined
emptyA<-matrix(NA, nrow=numrowA,ncol=numrowA)
# we then loop through columns and rows to obtain determinant to put into our empty matrix
for (i in 1:numrowA)
{
for (j in 1:numrowA)
{
sub_A<-A2[-i,-j]
emptyA[i,j]<-det(sub_A)
}
}
#compute the determinant of A
detA<-det(A2)
#compute the inverse of A
if (detA !=0)
{
invA<-t(emptyA)/detA
}
return(invA)
}
#Testing Function
#Our test matrix can be written as
(testMatrix <- matrix(c(5,1,6,8,11,-5,1,9,3), nrow=3, ncol=3))
## [,1] [,2] [,3]
## [1,] 5 8 1
## [2,] 1 11 9
## [3,] 6 -5 3
# our resultant from the function can be written as
(resultant_matrix <- myinverse(testMatrix))
## [,1] [,2] [,3]
## [1,] 0.10729023 0.03988996 0.08390646
## [2,] -0.07015131 0.01237964 0.06052270
## [3,] -0.09766162 -0.10041265 0.06464924
We can verify via a built in R function
solve(testMatrix)
## [,1] [,2] [,3]
## [1,] 0.10729023 -0.03988996 0.08390646
## [2,] 0.07015131 0.01237964 -0.06052270
## [3,] -0.09766162 0.10041265 0.06464924