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. 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.

1.1

Compute \(X = AA^T\) and \(Y = A^TA\).

# Define Matrix A
A <- matrix(c(1,2,3,-1,0,4), ncol = 3)
A
##      [,1] [,2] [,3]
## [1,]    1    3    0
## [2,]    2   -1    4
# Calculate A*A^T
x = A %*% t(A) 
x
##      [,1] [,2]
## [1,]   10   -1
## [2,]   -1   21
# Calculate A A^T*A
y = t(A) %*% A 
y
##      [,1] [,2] [,3]
## [1,]    5    1    8
## [2,]    1   10   -4
## [3,]    8   -4   16

1.2

Compute the eigenvalues and eigenvectors of \(X\) and \(Y\) using the built-in commands in R.

#Eigenvalues and eigenvectors of x 
eig.x = eigen(x) 
eig.x$vectors
##            [,1]       [,2]
## [1,] -0.0898056 -0.9959593
## [2,]  0.9959593 -0.0898056
eig.x$values
## [1] 21.09017  9.90983
#Eigenvalues and eigenvectors of y
eig.y = eigen(y) 
eig.y$vectors
##            [,1]       [,2]       [,3]
## [1,] -0.4141868 -0.3734355  0.8300574
## [2,]  0.2755368 -0.9206109 -0.2766858
## [3,] -0.8674842 -0.1141117 -0.4842001
eig.y$values
## [1] 21.09017  9.90983  0.00000

1.3

Compute left-singular, singular values, and right-singular vectors of \(A\) using the svd command.

# Singular Value Decomposition (Left Vector)
svd(A)$u 
##            [,1]      [,2]
## [1,] -0.0898056 0.9959593
## [2,]  0.9959593 0.0898056
# Singular Value Decomposition (Singular Values)
svd(A)$d
## [1] 4.592404 3.147988
# Singular Value Decomposition (Right Vector)
svd(A)$v 
##            [,1]      [,2]
## [1,]  0.4141868 0.3734355
## [2,] -0.2755368 0.9206109
## [3,]  0.8674842 0.1141117

1.4

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.

# Eigenvalue of x
svd(x)$d 
## [1] 21.09017  9.90983
# Eigenvalue of y
svd(y)$d 
## [1] 2.109017e+01 9.909830e+00 1.443890e-16
# Square of single value of A square
squares <- c(svd(A)$d)
squares <- (squares^2) 
squares
## [1] 21.09017  9.90983

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 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.

2.1

Create myinverse function.

# Construct diagnol matrix 
A <- matrix(c(1, 7, -3, 1, 5, 0, 2, 0, 7), ncol = 3)
A
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    7    5    0
## [3,]   -3    0    7
# Create function to solve cofactor of det(A)*inv(A)

myinverse <- function(A){
  cofactor_Matrix <- matrix(rep(0,length(A)),ncol=ncol(A))
  for (i in 1:ncol(A)) {
    for (j in 1:nrow(A)) {
      cofactor_Matrix[i,j] <- det(x[-i,-j])*(-1)^(i+j) 
    }
  }
  return(t(cofactor_Matrix)/det(A)) 
}

2.2

Test myinverse function.

# Use built in Solve function to calculate inverse of A: 
B <- solve(A)
B

# Calculate B with function: 
B <- myinverse(A)
B

# Unable to compare results due to difficulties troubleshooting the following error within function: 

##Error in UseMethod("determinant") : 
##no applicable method for 'determinant' applied to an object of class "c('double', 'numeric')"