Darkmoor
Darkmoor

Reputation: 902

Sign difference in eigenvectors taken form Matlab and Python

Could you please explain why there is a sign difference in some of the eigenvectors (2-4)? Does this difference affect further calculation in further calculations, e.g dimensionality reduction?

Matlab:

N = 5000;
dataA = rand(N,5);
covA = cov(dataA);
%covA = dataA*dataA'/(length(dataA)-1);
covA = covA + eps.*eye(size(covA));
[~,pA] = chol(covA);
assert(pA==0,'A is not possitive definite')

dataB = rand(N,5);
covB = cov(dataB);
%covB = dataB*dataB'/(length(dataB)-1);
covB = covB + eps.*eye(size(covB));
[~,pB] = chol(covB);
assert(pB==0,'B is not possitive definite')

[V,D] = eig(covA, covB);

V =

   -0.4241   -1.0891    1.8175    2.4067   -1.3032
    1.4445   -1.8960   -1.4118   -0.6514   -2.0075
    0.1214   -2.5039    0.3332   -0.1705    2.3609
   -2.1235   -0.7007    1.1632   -2.1532   -1.0554
   -2.2599   -0.4405   -2.2236    1.2545    0.0760

Python:

from scipy.linalg import eigh
import scipy.io
import numpy as np

cov_mat = scipy.io.loadmat('cov.mat')
covA = cov_mat['covA']
covB = cov_mat['covB']
eigvals, eigvecs = eigh(covA, covB, eigvals_only=False)

np.savetxt('eigvals.txt', eigvals, fmt='%.4f')
np.savetxt('eigvecs.txt', eigvecs, fmt='%.4f')

eigvecs =

   0.4241 1.0891 -1.8175 -2.4067 -1.3032
  -1.4445 1.8960  1.4118  0.6514 -2.0075
  -0.1214 2.5039 -0.3332  0.1705  2.3609
   2.1235 0.7007 -1.1632  2.1532 -1.0554
   2.2599 0.4405  2.2236 -1.2545  0.0760

Upvotes: 4

Views: 1869

Answers (1)

miradulo
miradulo

Reputation: 29710

They are the same eigenvectors, as flipping the signs on eigenvectors does not change their formulation - they will have the same eigenvalue. It will not affect further calculations as a result.

Why are they calculated differently? Most likely because the subroutines that are run vary based on the matrix that is being operated on, and don't care what 'sign' they return because eigenvalues don't have one.


A trivial mathematical proof:

If x is an eigenvector of matrix A with eigenvalue q, then by definition we have that Ax = qx.

It follows that A(-x) = -(Ax) = -(qx) = q(-x) such that -x is an eigenvector with the same eigenvalue.

Upvotes: 6

Related Questions