Question

Not sure whether this is a bug in the package or due to some other cause, but here we go.

I'm using the following package to find the largest eigenvalue and its corresponding eigenvector over a symmetric matrix of similarity scores (10x10 in size):

scipy.sparse.linalg.eigen.arpack.eigsh

, like so:

scipy.sparse.linalg.eigen.arpack.eigsh(mymatrix, 1, which='LM')

Now the question I have is that when I run it several times (using same matrix, settings etc.), sometimes the values in the eigenvectors are positive, and sometimes negative (see Run 3).

Does anyone know why this might be, or is it a bug? There doesn't seem to be a pattern to it, but it only happens when I run the code without closing Python after each iteration (i.e hitting F5 after each run).

### Run 1: ###
[[-0.31056873]
[-0.31913092]
[-0.3149287 ]
[-0.32262921]
[-0.32190688]
[-0.31292658]
[-0.32424732]
[-0.31885208]
[-0.31808024]
[-0.298174  ]]

### Run 2: ###
[[-0.31056873]
[-0.31913092]
[-0.3149287 ]
[-0.32262921]
[-0.32190688]
[-0.31292658]
[-0.32424732]
[-0.31885208]
[-0.31808024]
[-0.298174  ]]

### Run 3:###
[[ 0.31056873]
[ 0.31913092]
[ 0.3149287 ]
[ 0.32262921]
[ 0.32190688]
[ 0.31292658]
[ 0.32424732]
[ 0.31885208]
[ 0.31808024]
[ 0.298174  ]]

### Run 4: ###
[[-0.31056873]
[-0.31913092]
[-0.3149287 ]
[-0.32262921]
[-0.32190688]
[-0.31292658]
[-0.32424732]
[-0.31885208]
[-0.31808024]
[-0.298174  ]]

It's not a major problem, but I don't like the uncertainty in my code ;-)

Many thanks in advance,

Martyn

Was it helpful?

Solution

This is actually a math question.

But the reason is that there's an arbitrary phase when you calculate eigenvectors. You're solving Ax = bx for x. The equation is invariant under multiplication by a (possibly ocmplex) phase.

As to why it happens in a (seemingly) random fashion, I don't know. But I'm pretty sure it's not a bug.

OTHER TIPS

You are using the function with 'LM' in the which parameter of the function. Using this settings, you are searching for the eigenvalue of the highest magnitude, I've noticed that the smallest and largest eigenvalue of your matrix are fairly close in absolute value.
Add this to the fact that the algorithm is estimating the values of the eigenvalues and you get the answer to your question.
Try running your code with which = 'LR', this should give you the eigenvalue with highest real value (your eigenvalues are real anyway).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top