Problem Set 1.

1. Show that AT A not = AAT in general. (Proof and demonstration.)

a. Create matrix A

A <- matrix(c(1,2,3,4,-1,-2,-3,-4,5,6,7,8,-5,-6,-7,-8), nrow=4, byrow=TRUE)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]   -1   -2   -3   -4
## [3,]    5    6    7    8
## [4,]   -5   -6   -7   -8

b. Transpose Matrix A

A_T = t(A)
A_T
##      [,1] [,2] [,3] [,4]
## [1,]    1   -1    5   -5
## [2,]    2   -2    6   -6
## [3,]    3   -3    7   -7
## [4,]    4   -4    8   -8

c. Check if ATA == AAT

A%*%A_T == A_T%*%A
##       [,1]  [,2]  [,3]  [,4]
## [1,] FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE

2. For a special type of square matrix A, we get AT A = AAT . Under what conditions could this be true?(Hint:The Identity matrix I is an example of such a matrix).

You get ATA=AAT for symmetric square matrices, only. This occurs when the transpose of the matrix AT is equal to the matrix A itself. The order of the elements within the matrices must be equal, which can only occur in a square matrix where the number of rows and number of columns are equal, so when transposed, the shape remains the same. Example:

a. A 2x2 matrix B:

B <- matrix(c(1,2,2,3), nrow=2, byrow=TRUE)
B
##      [,1] [,2]
## [1,]    1    2
## [2,]    2    3

b. Transpose B:

B_T <- t(B)
B_T
##      [,1] [,2]
## [1,]    1    2
## [2,]    2    3

c. Check if BTB == BBT

B%*%B_T == B_T%*%B
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE

Problem Set 2.

Matrix factorization is a very important problem.There are supercomputers built just to do matrix factorizations.Every second you are on an airplane, matrices are being factorized.Radars that track flights use a technique called Kalman filtering.At the heart of Kalman Filtering is a Matrix Factorization operation. Kalman Filters are solving linear systems of equations when they track your flight using radars. Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer.

lu_decomp <- function(m){
  
  # Validate the matrix is square
  
  if(dim(m)[1]!=dim(m)[2]){
  return('Error: The matrix is not square, cannot factor')
  }
  
  n <- nrow(m)   #number of rows in m
  U <- m
  L <- diag(n)   #assign diagonal to L

  for (j in c(1:n)){
    for(i in c(2:n)){
      if(i > j){
        row <- U[j,]
        mult <- U[i, j] / row[j]
        U[i,] <- U[i,] - (mult * row)
        L[i,j] <- mult
      }
    }
  }
  
  return(list(L=L, U=U))
}

Test function

lu_decomp(A) #A matrix from above. Doesn't seem to work exactly
## $L
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]   -1    1    0    0
## [3,]    5 -Inf    1    0
## [4,]   -5  Inf  NaN    1
## 
## $U
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    0    0    0    0
## [3,]  NaN  NaN  NaN  NaN
## [4,]  NaN  NaN  NaN  NaN