5
$\begingroup$

I am trying to understand an example from a book, but I seem to get different answers depending on which spectral decomposition function I use in NumPy.

I am trying to find a spectral decomposition $W V W^\top$ of a given symmetric matrix $G$ using NumPy functions eig and eigh, where the latter is for symmetric matrices (which $G$ is):

  • eig returns columns arranged in a different order from the answer in the book.

  • eigh returns a matrix similar to that in the book, but with a different sign in columns $2$ and $4$.

Are these matrices equivalent? They yield different results in downstream use. Any help is appreciated.

G = np.array([[-0.55,  0.3  , 0.   , 0.    ,0.25],
              [ 0.3 , -0.95 , 0.   , 0.4   ,0.25],
              [ 0.  ,  0.   ,-0.55 , 0.3   ,0.25],
              [ 0.  ,  0.4  , 0.3  ,-0.95  ,0.25],
              [ 0.25,  0.25 , 0.25 , 0.25  ,-1.  ]])

v1,w1 = np.linalg.eigh(G)
v2,w2 = np.linalg.eig(G)

print w1
array([[ 0.224,  0.224,  0.5  , -0.671,  0.447],
       [-0.671,  0.224, -0.5  , -0.224,  0.447],
       [-0.224,  0.224,  0.5  ,  0.671,  0.447],
       [ 0.671,  0.224, -0.5  ,  0.224,  0.447],
       [ 0.   , -0.894, -0.   ,  0.   ,  0.447]]))

print w2
array([[-0.447, -0.671, -0.5  ,  0.224, -0.224],
       [-0.447, -0.224,  0.5  , -0.671, -0.224],
       [-0.447,  0.671, -0.5  , -0.224, -0.224],
       [-0.447,  0.224,  0.5  ,  0.671, -0.224],
       [-0.447,  0.   ,  0.   ,  0.   ,  0.894]]))

"Answer in book"
array([[ 0.224, -0.224,  0.5  ,  0.671,  0.447],
       [-0.671, -0.224, -0.5  ,  0.224,  0.447],
       [-0.224, -0.224,  0.5  , -0.671,  0.447],
       [ 0.671, -0.224, -0.5  , -0.224,  0.447],
       [ 0.   ,  0.894, -0.   ,  0.   ,  0.447]]))
$\endgroup$
5
$\begingroup$

Let $w_k$ be the k-th column of W (the kth eigen-vector) and $v_k$ be the k-th element of v (the kth eigen-value). Then we can write: $$ G = \sum_k w_k v_k w_k^T $$ which, element-wise, is equivalent to: $$ G_{i,j} = \sum_k v_k w_{i,k} w_{j,k} $$ where the indices are column-major. Since we always get $w_k$ twice, an overall sign on a column of $W$ cancels. Since the sum over $k$ can occur in any order, the order of the columns of $W$ also doesn't matter as long as the elements of $v$ are in the same order.

If they are yielding different results down-stream, you likely have an error. Were you consistent with using v1 with w1 and v2 with w2? v1 and v2 should have the same elements but a different order. Neither eig nor eigh guarantee an ordering of the eigenvalues.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.