rsvdmatrix-inversematrix-decomposition

Manually calculating Pseudo inverse from SVD in R looks wrong?


A<-matrix(c(4,5,5,6,4,3,6,11,31),nrow=3,ncol=3)

 B
B<-cov(A)
           [,1]        [,2]  [,3]
[1,]  0.3333333  -0.8333333   5.0
[2,] -0.8333333   2.3333333 -17.5
[3,]  5.0000000 -17.5000000 175.0

bsvd<-svd(B)

D<-diag(1/bsvd$d)
D
V<-bsvd$v
U<-bsvd$u
a<-V%*%D
a%*%t(U)
 a%*%t(U)
             [,1]         [,2]         [,3]
[1,] 3.420461e+15 1.954549e+15 9.772746e+13
[2,] 1.954549e+15 1.116885e+15 5.584426e+13
[3,] 9.772746e+13 5.584426e+13 2.792213e+12
pseudoinverse(B)
            [,1]       [,2]        [,3]
[1,]  0.32090310 -0.5583242 -0.06512408
[2,] -0.55832422  0.9714162  0.11302345
[3,] -0.06512408  0.1130235  0.01887380



I am unsure why this is giving me different answers? The formula for the pseudo inverse is VDU^T from what I have seen online.


Solution

  • Set D[3,3] <- 0, i.e. exclude the zero eigenvalue of B from the inverse, and then recalculate VDU^T and you'll get the same answer as the pseudoinverse function you are using.