DATA 605: Week 4 Homework Assignment
Problem Set 1)
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.
# Define 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
# Let's compute X we need to multiply A by it's transpose
X <- A %*% t(A)
X [,1] [,2]
[1,] 14 11
[2,] 11 17
# Let's compute Y We now proceed to compute Y multiply A by it's transpose
Y <- t(A) %*% A
Y [,1] [,2] [,3]
[1,] 2 2 -1
[2,] 2 4 6
[3,] -1 6 25
We use the built in commands in R to find the eigen values and the eigen vectors
For X
eigen_X <- eigen(X)For Y
eigen_Y <- eigen(Y)By looking at the eigen values associated with Y, one can argue that Y is almost a singular matrix. We move on to the next part of the question.
Compute the SVD
S <- svd(A)
S$d #the vector that contains singular values of X.[1] 5.157693 2.097188
S$u #a matrix where columns contain left singular vectors [,1] [,2]
[1,] -0.6576043 -0.7533635
[2,] -0.7533635 0.6576043
S$v #a matrix that contain right singular vectors [,1] [,2]
[1,] 0.01856629 -0.6727903
[2,] -0.25499937 -0.7184510
[3,] -0.96676296 0.1765824
As you can see, the S$u is close to the eigen vectors of X while S$v is close to the eigen vectors of Y.
We should now show that the eigen values of X and Y are indeed the same and are squares of SVD. We can do this using the all.equal command. all.equal will simply return a boolean which is enough for our purpose.
Lets store the singular values using SVD, eigen values of X and eigen values of Y so we can call on them
singular <- S$d
eigenvalues_X <- eigen_X$values
eigenvalues_Y <- eigen_Y$valuesReturn True if indeed the eigenvalue matches the square of SVD For X
all.equal(eigenvalues_X, (singular^2))[1] TRUE
For Y
all.equal(eigenvalues_Y[1:2], (singular^2))[1] TRUE
Thus, We can conclude that computed non-zero eigenvalues of X and Y are the same as the non-zero SVD values of matrix A.
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) {
# check if matrix is square
if (nrow(A) != ncol(A)) {
return("takes only squre matrix as input")
}
r <- nrow(A)
c <- ncol(A)
cofactor <- diag(r)
for (i in 1:r) {
for (j in 1:c) {
cofactor[i, j] <- (-1)^(i + j) * det(A[-i, -j])
}
}
return(t(cofactor)/det(A))
}Lets test with \[ \begin{bmatrix} 1 & 2 & 3 \\ 0 & -1 & 3 \\ 1 & -2 & 3 \end{bmatrix} \]
# Testing Function
A <- matrix(c(1, 2, 3, 0, -1, 3, 1, -2, 3), nrow = 3, byrow = TRUE)
A [,1] [,2] [,3]
[1,] 1 2 3
[2,] 0 -1 3
[3,] 1 -2 3
isTRUE(all.equal(myinverse(A), solve(A)))[1] TRUE
We can use R to verify
B <- myinverse(A)
B [,1] [,2] [,3]
[1,] 0.25000000 -1.0000000 0.75000000
[2,] 0.25000000 0.0000000 -0.25000000
[3,] 0.08333333 0.3333333 -0.08333333
It seems like our function is doing what it is supposed to .