Data 605 Assignment Two
Data 605 Assignment Two
Problem Set One
Show that \[ A^TA \neq AA^T\]
\[ A = \begin{bmatrix} a & b \\ c & d \\ \end{bmatrix} \\ A^T = \begin{bmatrix} a & c \\ b & d \\ \end{bmatrix}\]
When the respective matrix multiplication is perfomed
\[ AA^T = \begin{bmatrix} a^2+b^2 & ac + bd \\ ac +bd & c^2 + d^2 \\ \end{bmatrix} \\ A^TA = \begin{bmatrix} a^2+c^2 & ab + cd \\ ab - bd & b^2 + d^2 \\ \end{bmatrix} \]
So we can see that they are not the same
We can also prove the same thing using R functions with a 3X3 matrix
Create a Matrix
A <- matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3)
A
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
Transpose the Matrix
AT <- t(A)
AT
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
Multiply Transposed Matrix by the Original
ATA <- AT %*% A
ATA
## [,1] [,2] [,3]
## [1,] 14 32 50
## [2,] 32 77 122
## [3,] 50 122 194
Multiply Original Matrix by the Transposed Matrix
AAT <- A %*% AT
AAT
## [,1] [,2] [,3]
## [1,] 66 78 90
## [2,] 78 93 108
## [3,] 90 108 126
Compare the two Matrices
ATA != AAT
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
Problem Set One Question Two
For a special type of Matrix A we find the following to be true:
\[A^TA = AA^T \]
Using Identity Matrix of 3x3
I <- diag(3)
I
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
# Transpose the id matrix
IT <- t(I)
# try both
I1 <- IT %*% I
I2 <- I %*% IT
# check using logic
I1 != I2
## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
In order for \[A^TA = AA^T\] to be true we need to have an identity matrix A
Problem Set Two Matrix Factorization
Create a random Matrix
n <- round(runif(1,2,4))
A <- matrix(sample.int(10, size = n^2, replace = TRUE), ncol = n)
A
## [,1] [,2] [,3] [,4]
## [1,] 8 7 4 10
## [2,] 6 10 2 6
## [3,] 5 7 3 5
## [4,] 7 2 9 5
matrixLU <- function(A){
U = A # to keep consistant formatting.
L = diag(x = 1, ncol = ncol(A), nrow = nrow(A)) # Square diagonal to populate cells.
# For loops to loop over elements in L and 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))
}
X <- matrixLU(A)
X$L # return the lower triangular matrix
## [,1] [,2] [,3] [,4]
## [1,] 1.000 0.0000000 0.0 0
## [2,] 0.750 1.0000000 0.0 0
## [3,] 0.625 0.5526316 1.0 0
## [4,] 0.875 -0.8684211 4.4 1
X$U # return the upper triangular matrix
## [,1] [,2] [,3] [,4]
## [1,] 8 7.000000e+00 4.000000 10.0000000
## [2,] 0 4.750000e+00 -1.000000 -1.5000000
## [3,] 0 -4.440892e-16 1.052632 -0.4210526
## [4,] 0 1.953993e-15 0.000000 -3.2000000
See if the upper and lower triangular matrices multiplied together equal the original matrix
(X$L %*% X$U)==A
## [,1] [,2] [,3] [,4]
## [1,] TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE
## [4,] TRUE FALSE TRUE FALSE