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\times2\) matrix

\[\mathbf{A} = \begin{bmatrix} 1 & 2 & 3 \\ -1 & 0 & 4 \end{bmatrix}\]

write code in R to compute \(\mathbf{X} = \mathbf{A}\mathbf{A}^T\) and \(\mathbf{Y} = \mathbf{A}^T\mathbf{A}\).

symmetric <- function(A) {
  r <- dim(A)[1]
  c <- dim(A)[2]
  # Create transpose of A
  At <- matrix(A, nrow=c, ncol=r, byrow = T)
  # Create X = A %*% t(A)
  X <- matrix(NA, nrow=r, ncol=r)
  for (i in 1:r) {
    for (j in 1:r) {
      X[i,j] <- sum(A[i,]*At[,j])
    }
  }
  # Create Y = t(A) %*% A
  Y <- matrix(NA, nrow=c, ncol=c)
  for (i in 1:c) {
    for (j in 1:c) {
      Y[i,j] <- sum(At[i,]*A[,j])
    }
  }
  # Convert to list for output
  XY <- list("X" = X, "Y" = Y)
return(XY)
}

A <- matrix(c(1,-1,2,0,3,4), nrow=2);
XY <- symmetric(A)

X <- XY[[1]]; X #or as.matrix(XY$X)
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
Y <- XY[[2]]; Y #or as.matrix(XY$Y)
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

Then, compute the eigenvalues and eigenvectors of \(\mathbf{X}\) and \(\mathbf{Y}\) using the built-in commands in R.

Xvalues <- eigen(X)$values
Xvectors <- eigen(X)$vectors
Yvalues <- eigen(Y)$values
Yvectors <- eigen(Y)$vectors

Then, compute the left-singular, singular values, and right-singular vectors of \(\mathbf{A}\) using the svd command.

# Singular vectors are only defined up to sign
LeftSingular <- svd(A)$u
LeftSingular[ ,1] <- -svd(A)$u[ ,1]
SingularValues <- svd(A)$d 
RightSingular <- -svd(A, nv=3)$v

Examine the two sets of singular vectors and show that they are indeed eigenvectors of \(\mathbf{X}\) and \(\mathbf{Y}\).

LeftSingular
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
Xvectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
all.equal(LeftSingular, Xvectors)
## [1] TRUE
RightSingular
##             [,1]       [,2]       [,3]
## [1,] -0.01856629  0.6727903  0.7396003
## [2,]  0.25499937  0.7184510 -0.6471502
## [3,]  0.96676296 -0.1765824  0.1849001
Yvectors
##             [,1]       [,2]       [,3]
## [1,] -0.01856629  0.6727903  0.7396003
## [2,]  0.25499937  0.7184510 -0.6471502
## [3,]  0.96676296 -0.1765824  0.1849001
all.equal(RightSingular, Yvectors)
## [1] TRUE

In addition, the two non-zero eigenvalues (the 3rd value will be very close to zero, if not zero) of both \(\mathbf{X}\) and \(\mathbf{Y}\) are the same and are squares of the non-zero singular values of \(\mathbf{A}\).

Xvalues
## [1] 26.601802  4.398198
Yvalues[1:2]
## [1] 26.601802  4.398198
SingularValues
## [1] 5.157693 2.097188
all.equal(Xvalues, Yvalues[1:2], SingularValues^2)
## [1] TRUE

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: \(\mathbf{B} =\) myinverse\(\left(\mathbf{A}\right)\) where \(\mathbf{A}\) is a matrix and \(\mathbf{B}\) is its inverse and \(\mathbf{A}\times\mathbf{B}=\mathbf{I}\). The off-diagonal elements of \(\mathbf{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 \(\mathbf{A}\) to compute the inverse.

myinverse <- function(A) {
  C <- matrix(NA, nrow=rank, ncol=rank)
  for (i in 1:rank) {
    for (j in 1:rank) {
      if (rank < 3) {
        C[i,j] <- (-1)^(i+j) * ifelse(rank == 1, 1, A[-i,-j])
      } else {
        C[i,j] <- (-1)^(i+j) * det(A[-i,-j])
      }
    }
  }
  B <- t(C)/det(A)
  return(B)
}

rank = 5
set.seed(10014)
x <- rnorm(rank^2)
A <- matrix(x, nrow=rank); A
##             [,1]       [,2]        [,3]       [,4]       [,5]
## [1,]  0.29510599 -0.7054909  0.47833482  0.4346340  1.8069750
## [2,] -1.76077985  1.1386157 -0.96048538  0.2011658 -0.5593332
## [3,]  2.15850481 -0.7107617 -1.09883393  1.2091517  1.3123407
## [4,]  1.35322735 -1.2040571 -0.07013568 -2.1669726  0.9981520
## [5,] -0.01176667  0.4352981  0.40104154  0.3656437  0.5578555
B <- myinverse(A); B
##             [,1]       [,2]        [,3]       [,4]       [,5]
## [1,] -0.50607863 -0.2813938  0.23887870  0.1074089  0.6029839
## [2,] -0.62879086  0.1940242  0.11465168  0.2203324  1.5673345
## [3,]  0.02460135 -0.4894107 -0.31040460 -0.1434307  0.4164601
## [4,]  0.18856111 -0.1367986  0.12244716 -0.4169531 -0.2899515
## [5,]  0.33869745  0.2841671  0.05846722  0.2067408  0.4729515
all.equal(solve(A), B)
## [1] TRUE
I <- round(A %*% B, 10); I
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0
## [5,]    0    0    0    0    1