0
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.

123123
  • 63
  • 6
  • Where do you get the `pseudoinverse()` function from? There are many different meanings of that term and they don't necessarily agree for actually singular matrices like `B`. – pseudospin Aug 23 '20 at 16:58
  • got it from here https://www.rdocumentation.org/packages/corpcor/versions/1.6.9/topics/pseudoinverse – 123123 Aug 23 '20 at 17:01

1 Answers1

1

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.

pseudospin
  • 2,737
  • 1
  • 4
  • 19
  • For some reason I am getting differing values for different matrices. Sometimes the inverses are the same sometimes they are not. would you know why? is it because the matrices are singular? – 123123 Aug 23 '20 at 18:45
  • can give you another example if you want – 123123 Aug 23 '20 at 18:45
  • It'll be which eigenvalues/vectors you are excluding. It won't always be the third one. That other function will probably use some tolerance and if the eignenvalue of the original matrix is below some number then that eignevalue in `1/D` will be set to zero. – pseudospin Aug 23 '20 at 18:50