1. Problem Set 1
In this problem, we’ll verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a 3 × 2 matrix \(A\)
\[A =
\begin{bmatrix}
1 & 2 & 3\\
-1 & 0 & 4\\
\end{bmatrix}
\]
write code in R to compute \(X = AA^T\) and \(Y = A^TA\). Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commands in R.
#create matrix
A = matrix(c(1,2,3,-1,0,4), nrow = 2, byrow = T)
print("Our original matrix:")
## [1] "Our original matrix:"
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
#transpose of matrix
At = t(A)
print("Transpose")
## [1] "Transpose"
At
## [,1] [,2]
## [1,] 1 -1
## [2,] 2 0
## [3,] 3 4
X = A %*% At
print("X:")
## [1] "X:"
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
print("Y:")
## [1] "Y:"
Y = At %*% A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
Eigenvalues and vectors for X:
X_eigen <- eigen(X)
print("Eigenvalues for X:")
## [1] "Eigenvalues for X:"
X_eigen_value <- X_eigen$values
X_eigen_value
## [1] 26.601802 4.398198
print("Eigenvectors for x:")
## [1] "Eigenvectors for x:"
X_eigen_vector <- X_eigen$vectors
X_eigen_vector
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
Eigenvalues and vectors for Y:
Y_eigen <- eigen(Y)
print("Eigenvalues for Y:")
## [1] "Eigenvalues for Y:"
Y_eigen_value <- Y_eigen$values
Y_eigen_value
## [1] 2.660180e+01 4.398198e+00 -1.233248e-16
print("Eigenvectors for Y:")
## [1] "Eigenvectors for Y:"
Y_eigen_vector <- Y_eigen$vectors
Y_eigen_vector
## [,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 see that the first eigenvalues of Y match the first two eigenvalues of X - indeed, the third value is very close to zero.
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\).
Let’s look at the left-singular vector A and see if it equals the eigenvector of \(X\).
svd_A <- svd(A)
#left singluar value of A
left_singular_A <- svd_A$u
print("Left-singular vector of A: ")
## [1] "Left-singular vector of A: "
left_singular_A
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
print("Compared to eigenvector of X: ")
## [1] "Compared to eigenvector of X: "
X_eigen_vector
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
#Check if absolute values are equal:
print("Is the left-singular vector of A equal to to the eigenvector of X: ")
## [1] "Is the left-singular vector of A equal to to the eigenvector of X: "
abs(round(left_singular_A,3)) == abs(round(X_eigen_vector,3))
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
Let’s look at the right-singular vector A and see if it equals the eigenvector of \(Y\), ignoring the third column.
#right singluar value of A
right_singular_A <- svd_A$v
print("Right-singular vector of A: ")
## [1] "Right-singular vector of A: "
right_singular_A
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
print("Compared to eigenvector of Y : ")
## [1] "Compared to eigenvector of Y : "
Y_eigen_vector
## [,1] [,2] [,3]
## [1,] -0.01856629 0.6727903 0.7396003
## [2,] 0.25499937 0.7184510 -0.6471502
## [3,] 0.96676296 -0.1765824 0.1849001
#Check if absolute values are equal:
print("Is the right-singular vector of A equal to to the eigenvector of Y: ")
## [1] "Is the right-singular vector of A equal to to the eigenvector of Y: "
abs(round(right_singular_A,3)) == abs(round(Y_eigen_vector[,1:2],3))
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [3,] TRUE TRUE
Now let’s examine the singular values.
singular_v_A <- svd_A$d
#singular values of A
singular_v_A
## [1] 5.157693 2.097188
#Do the singular values of A squared equal the eigenvalue of X?
print("Do the singular values of A squared equal the eigenvalues of X?")
## [1] "Do the singular values of A squared equal the eigenvalues of X?"
round(singular_v_A^2,3) == round(X_eigen_value,3)
## [1] TRUE TRUE
#Do the singular values of A squared equal the eigenvalue of Y?
print("Do the singular values of A squared equal the eigenvalues of Y?")
## [1] "Do the singular values of A squared equal the eigenvalues of Y?"
round(singular_v_A^2,3) == round(Y_eigen_value[1:2],3)
## [1] TRUE TRUE
Yep.
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.
We’re going to find the cofactor of A, then use it to get the adjugate. Finally, the inverse is 1/determinant multiplied by the adjugate matrix.
myinverse <- function(A){
#verify square matrix
if (nrow(A) != ncol(A)){
print("Come back when you have a square matrix.")
break
}
#create cofactor matrix, starting with copy of A so same dim
C <- A
for (i in 1:nrow(A)) {
for (j in 1:ncol(A)) {
C[i,j] <- (det(A[-i,-j])*(-1)^(i + j))
}
}
#Adjugate matrix is transpose of cofactor
AA <- t(C)
#Inverse is 1/deta(A) * adjugate
invA <- (1/det(A)) * AA
print(invA)
}
Now let’s test the function.
A <- matrix(c(1,2,3,-4,5,6,7,-8,9), nrow = 3)
myinverse(A)
## [,1] [,2] [,3]
## [1,] 0.3875 0.325 -0.01250000
## [2,] -0.1750 -0.050 0.09166667
## [3,] -0.0125 -0.075 0.05416667
Verify with R’s solve function.
solve(A)
## [,1] [,2] [,3]
## [1,] 0.3875 0.325 -0.01250000
## [2,] -0.1750 -0.050 0.09166667
## [3,] -0.0125 -0.075 0.05416667
print("Does our function work?")
## [1] "Does our function work?"
round(myinverse(A),3) == round(solve(A),3)
## [,1] [,2] [,3]
## [1,] 0.3875 0.325 -0.01250000
## [2,] -0.1750 -0.050 0.09166667
## [3,] -0.0125 -0.075 0.05416667
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
It does.