1. Show that ATA???AAT in general. (Proof and demonstration.)
matrix A
5 7
6 8
A <- matrix(c(5, 6, 7, 8), ncol = 2)
t(A)
##      [,1] [,2]
## [1,]    5    6
## [2,]    7    8
t <- 2

ATA <- A^t %*% A
AAT <- A %*% A^t

identical(ATA, AAT)
## [1] FALSE
  1. 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). Please typeset your response using LaTeX mode in RStudio. If you do it in paper, please either scan or take a picture of the work and submit it. Please ensure that your image is legible and that your submissions are named using your First initial, last name, assignment and problem set within the assignment. E.g. LFulton_Assignment2_PS1.png

Answer: Here, We have a matrix A

A <- matrix(c(1, 1, 1, 1, 2, 1, 1, 1, 1), nrow = 3, byrow = T)
A
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    2    1
## [3,]    1    1    1
# let's transpose the value of A
t(A)
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    2    1
## [3,]    1    1    1
#Write equations for $A^T A = AA^T$
ATA <- t(A) %*% A
AAT <- A %*% t(A)

AAT == ATA
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

Hence, we can state that it is true when the matrix is symmetrical.

  1. 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 beingfactorized. Radars that track ights 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 fight using radars. Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer. Please submit your response in an R Markdown document using our class naming convention, E.g. LFulton_Assignment2_PS2.png You donโ€™t have to worry about permuting rows of A and you can assume that A is less than 5x5, if you need to hard-code any variables in your code. If you doing the entire assignment in R, then please submit only one markdown document for both the problems.

Answer:

# to freeze the randomnly selected numbers we need to freeze it and set.seed helps us to do that.
set.seed(5)
n <- 5
X <- matrix(sample.int(5, size = n^2, replace = TRUE), ncol = n)
X
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    4    2    2    5
## [2,]    4    3    3    2    4
## [3,]    5    5    2    5    2
## [4,]    2    5    3    3    2
## [5,]    1    1    2    5    1
# let's take the values for the matrix.

matrixLDU <- function(X){
  U = X 
  
  # to populate cells we need a diagonal
  L = diag(x = 1, ncol = ncol(X), nrow = nrow(X)) 
  
  # here we are using loop to populate elements for L & U
    for (i in 1:(nrow(U) - 1)){
      for (j in (i + 1):nrow(U)){
        if (U[i, i] != 0){
          multiplier = (U[j, i] / U[i, i])
          L[j, i] = multiplier
          U[j,] = (U[j,] - multiplier*U[i,])
      }
    }
  }
  return(list('L'= L, 'U'= U))
}

Y <- matrixLDU(X)
Y$L
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1.0  0.0  0.0    0    0
## [2,]  2.0  1.0  0.0    0    0
## [3,]  2.5  1.0  1.0    0    0
## [4,]  1.0 -0.2 -0.4    1    0
## [5,]  0.5  0.2 -0.6    4    1
Y$U
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    4    2  2.0  5.0
## [2,]    0   -5   -1 -2.0 -6.0
## [3,]    0    0   -2  2.0 -4.5
## [4,]    0    0    0  1.4 -6.0
## [5,]    0    0    0  0.0 21.0
# to calculate the outer product for dimensions
Y$L%*%Y$U
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    4    2    2    5
## [2,]    4    3    3    2    4
## [3,]    5    5    2    5    2
## [4,]    2    5    3    3    2
## [5,]    1    1    2    5    1
# to create another matrix and check 
Z <-  matrix(c(1:16), ncol = 4)
Y1 <- matrixLDU(Z)
Y1$L
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    2    1    0    0
## [3,]    3    2    1    0
## [4,]    4    3    0    1
Y1$U
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    0   -4   -8  -12
## [3,]    0    0    0    0
## [4,]    0    0    0    0
# to check the values of the outer product and dimensions
all.equal(Y1$L %*% Y1$U, Z)
## [1] TRUE