Search code examples
rsastranslatesvdsas-iml

Different result from SAS/IML and R, in SVD decomposition


shortly, I'm translating an R package into IML languageand I'm totally struggling myself with the SVD decomposition result between R and IML.

R code:

s <- svd(MAT) 
s$v

SAS/IML code:

call svd (U, D, V, MAT);
print V;

V is in both sides the matrix containing right singular vectors from the SVD decomposition, but here, please take a look at these results from R:

             [,1]         [,2]         [,3]        [,4]        [,5]         [,6]         [,7]
[1,] -2.625059e-02  0.029572211 -0.006491235 0.015622547  0.01553215 -0.003882378  0.007250290
[2,] -4.762146e-06 -0.030403155 -0.016635218 0.024949110 -0.01238686  0.001334805  0.041902431
[3,] -8.460010e-02  0.025365547  0.006657322 0.020129575 -0.02312842  0.038366880  0.054249177
[4,] -1.368302e-02  0.029621706  0.005462163 0.017887163  0.02605000 -0.002546119 -0.001913554
[5,] -3.326751e-02  0.003552646  0.003634580 0.065277891 -0.01218518 -0.026305833  0.029209961
[6,] -1.451836e-02  0.089992653 -0.012355758 0.009777273 -0.07790069 -0.044679172 -0.028174261

and these result from SAS/IML:

            COL1      COL2      COL3      COL4      COL5      COL6      COL7      COL8      COL9

 ROW1   0.0262506 -0.029572 -0.006491 0.0156225 0.0155322 -0.003882  -0.00725  0.040721 -0.000566
 ROW2   4.7621E-6 0.0304032 -0.016635 0.0249491 -0.012387 0.0013348 -0.041902 0.0225321 0.0070566
 ROW3   0.0846001 -0.025366 0.0066573 0.0201296 -0.023128 0.0383669 -0.054249 0.0305745 -0.041534
 ROW4    0.013683 -0.029622 0.0054622 0.0178872   0.02605 -0.002546 0.0019136 0.0168932 0.0229999
 ROW5   0.0332675 -0.003553 0.0036346 0.0652779 -0.012185 -0.026306  -0.02921 -0.029533 0.0145009
 ROW6   0.0145184 -0.089993 -0.012356 0.0097773 -0.077901 -0.044679 0.0281743 -0.025475 -0.036881
 ROW7   -0.012385 0.0295035 0.0051056   -0.0007 0.0025335 -0.009391 -0.045927 -0.054661 -0.029963

The values are the same as you can see, but their signs are sometimes the same and sometimes different. I can't find any reason why this is happening and this is totally driving me crazy. Could someone give me a solution, or at least an hint or anything. Any help will be appreciated, also if it won't solve this problem.

Many thanks.

EDIT: obviosly, results posted are only a portion of the total matrix.


Solution

  • In short, the SVD decomposition is not unique.

    The singular vectors of M are the eigenvectors of M`M. Eigenvectors are not unique. Even when the matrix is full rank, the eigenvectors are only defined up to a sign: If v is an eigenvector of the matrix A for eigenvalue lambda, then so is -v because A*(-v) = -(Av) = -(lambda v) = lambda (-v).

    Different SVD (and eigenvalue) algorithms can lead to different decompositions. As long as M = UDV`, the decomposition is valid. The D matrix of singular values will be essentially the same across software packages, but the U and V matrices can be different.

    Incidentally, if your matrix is not full rank, then even "uniqueness up to sign" breaks down. The Wikipedia article on the SVD has an example of two very different SVDs for the same rank-deficient matrix.