DATA605 Assignment 2

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 by 2 matrix A A = 1 2 3 ô€€€1 0 4

  1. write code in R to compute X = AAT and Y = ATA. Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commans 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.

The Matrix

A<-matrix(c(1,2,3,1,0,4),2,3,byrow=TRUE)

A transpose A and traspose A A

X <- A%*%t(A)
Y <- t(A)%*%A
X
##      [,1] [,2]
## [1,]   14   13
## [2,]   13   17
Y
##      [,1] [,2] [,3]
## [1,]    2    2    7
## [2,]    2    4    6
## [3,]    7    6   25

Get eigen values

e.Y<-eigen(Y)
e.X<-eigen(X)
e.Y$values
## [1]  2.858625e+01  2.413748e+00 -9.583074e-16
e.X$values
## [1] 28.586252  2.413748
e.X
## eigen() decomposition
## $values
## [1] 28.586252  2.413748
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6653480 -0.7465334
## [2,] 0.7465334  0.6653480
e.Y
## eigen() decomposition
## $values
## [1]  2.858625e+01  2.413748e+00 -9.583074e-16
## 
## $vectors
##            [,1]        [,2]       [,3]
## [1,] -0.2640703 -0.05225548  0.9630868
## [2,] -0.2488859 -0.96102189 -0.1203859
## [3,] -0.9318383  0.27148903 -0.2407717

Get SVD

s.A<-svd(A)
#Siginular values of A
s.A$d
## [1] 5.346611 1.553624
#left singular vectors of A
s.A$u 
##            [,1]       [,2]
## [1,] -0.6653480 -0.7465334
## [2,] -0.7465334  0.6653480
e.X$vectors
##           [,1]       [,2]
## [1,] 0.6653480 -0.7465334
## [2,] 0.7465334  0.6653480
#right singular vectors of A
s.A$v
##            [,1]        [,2]
## [1,] -0.2640703 -0.05225548
## [2,] -0.2488859 -0.96102189
## [3,] -0.9318383  0.27148903
e.Y$vectors
##            [,1]        [,2]       [,3]
## [1,] -0.2640703 -0.05225548  0.9630868
## [2,] -0.2488859 -0.96102189 -0.1203859
## [3,] -0.9318383  0.27148903 -0.2407717

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 AB = 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. Please submit PS1 and PS2 in an R-markdown document with your first initial and last name.

Build the funtion

  • In this funciton we are taking input list then:
    1. Get Determinant of A
    2. Get cofactors by mulitplying the inverse of A by determinant of A
    3. Get the Diagonals of the cofactors
    4. Confirm Cofactors muiltplied by 1/det is equal to inverse of A by subtracting both matrixes and getting 0
ff<- function(A=ls())
{
    print("Matrix A is:")
    print (A)
    
    #Determinant of A
    print("Determanint of A is:")
    print(det(A))
    
    #Cofactor of A
    print("The Cofactors are:")
    c<-solve(A)*det(A)
    print(c)
    
    #Diag of det matrix
    print("The diagonals of the cofactor matrix are:")
    diag<-diag(solve(A)*det(A))
    print(diag)
    
    #Inverse cofactor checks
    print("Subtracting inverses gives a 0 matrix so they are equal")
    print(1/det(A)*solve(A)*det(A) - solve(A))
}

Test the function

ff(matrix(c(0,0,1,2,-1,3,1,1,4),3,3))
## [1] "Matrix A is:"
##      [,1] [,2] [,3]
## [1,]    0    2    1
## [2,]    0   -1    1
## [3,]    1    3    4
## [1] "Determanint of A is:"
## [1] 3
## [1] "The Cofactors are:"
##      [,1] [,2] [,3]
## [1,]   -7   -5    3
## [2,]    1   -1    0
## [3,]    1    2    0
## [1] "The diagonals of the cofactor matrix are:"
## [1] -7 -1  0
## [1] "Subtracting inverses gives a 0 matrix so they are equal"
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
## [3,]    0    0    0