Search code examples
pythonnumpysvd

numpy SVD giving wrong values?


I am trying to get accustomed to doing singular value decomposition with numpy. I decided to do the SVD on a matrix from an example to understand how it works. I am following this pdf, where A = [[3, 2, 2], [2, 3, -2]]. When I run the svd, however, I get something different for the matrices U and V then what is provided in the pdf. It is the same matrix, except the signs have been flipped. Now, since the matrices are both linear operators and the signs have been flipped on both it is technically still correct, the flipping cancels out. But why is it this way?


Solution

  • Remember that U and V are eigenvectors. Scaling an eigenvector is still an eigenvector, but as long as you get some linear multiple of the solution that you get in the PDF, it is perfectly acceptable. You know the implementation is correct if the eigenvalues are the same. Judging from your post as you didn't comment on the eigenvalues, I'm assuming that they are correct. The eigenvalues need to be the same, but the eigenvectors can be different.

    In your case, the scaling is done by -1, which are still valid eigenvectors to the same eigenvalues. As to the reason why the eigenvectors are different in sign is most likely the way the SVD is calculated. Finding the actual left and right eigenvectors is computationally prohibitive, so some tips and tricks to arrive at the same solution are done, and that may mean that the eigenvectors are of a different scale than you expect.

    I'd finally like to point you to this Cross Validated post that talks about the different algorithms that compute the SVD. numpy.svd examines the properties of the input matrix and chooses the right algorithm that is suitable.

    https://stats.stackexchange.com/questions/66034/what-are-efficient-algorithms-to-compute-singular-value-decomposition-svd