forum.alglib.net
http://forum.alglib.net/

Negative eigenvalues of a symmetric matrix
http://forum.alglib.net/viewtopic.php?f=2&t=88
Page 1 of 2

Author:  comix [ Tue Oct 26, 2010 9:06 am ]
Post subject:  Negative eigenvalues of a symmetric matrix

Hi, i have a problem using alglib function smatrixevd and rmatrixevd...
I have a 200x200 symmetric matrix...Because the matrix is symmetric i should have only positive eigenvalues...But is not so!
How i fix this problem?

Thanks

Author:  Sergey.Bochkanov [ Tue Oct 26, 2010 10:26 am ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

Symmetricity guarantees that eigenvalues are real and eigenvector are orthogonal. It does not guarantees, that they are positive.

If you have symmetric positive definite matrix, then eigenvalues should be positive (although numerical errors may lead to spurious negative vals).

Author:  Sergey.Bochkanov [ Tue Oct 26, 2010 10:27 am ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

P.S. If you have symmetric matrix, I recommend you to use smatrixevd(). General nonsymmetric EVD solvers are always slower and less stable.

Author:  comix [ Tue Oct 26, 2010 10:46 am ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

Well...The matrix is a correlation matrix, i believe that is positive-semidefinite matrix!
However I only used rmatrixevd to check the results (which in fact coincide)... Maybe the matrix is too large? Or it is ill-conditioned?
P.S. Thanks for support!

Author:  Sergey.Bochkanov [ Tue Oct 26, 2010 10:55 am ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

Hmmm, you are right - it should be at least semidefinite.

What is the ratio of negative eigenvalues to the largest eigenvalue? Is it somewhere about 1.0E-14 (close to machine precision), or significantly larger?

P.S. matrix size should not heavily influence results as long as its eigenvalues are far from zero.

Author:  comix [ Tue Oct 26, 2010 12:24 pm ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

I've about 50% of eigenvalues negative... The biggest negative is -374495 while the biggest positive is 440218! I don't understand where i go wrong!

Author:  Sergey.Bochkanov [ Tue Oct 26, 2010 12:46 pm ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

Maybe you have error in your code for calculation of correlation matrix? Can you post it here along with your data?

I think it is the most probable scenario because results of smatrixevd() and rmatrixevd() coincide - i.e. it is not error in the EVD algo.

Author:  comix [ Wed Oct 27, 2010 1:41 am ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

I hope that the code is easy to understand!
int pixel[200][200][2]; // here i store data
double average[200][200];
for(int n=0;n<2;n++){
for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
average[i][j]+= (double) pixel[i][j][n];
}//end for j
}//end for i
}//end for n

for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
average[i][j]=average[i][j]/2;
}//end for j
}//end for i
double stdDev[200][200][2];
for(int n=0;n<2;n++){
for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
stdDev[i][j][n]=pixel[i][j][n]-average[i][j];
}//end for j
}//end for i
}//end for n
for(int n=0;n<2;n++){
for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
cov[i][j]+= stdDev[i][j][n]* stdDev[j][i][n];
}//end for j
}//end for i
}//end for n
double cov[200][200];
for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
cov[i][j]=cov[i][j]/2;
}
}
double corr[200][200];
for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
if(i!=j) corr[i][j]=cov[i][j]/(sqrt(cov[i][i]*cov[j][j]));
}
}
for(int i=0;i<200;i++){
for(int j=0;j<200;j++){
if(i==j) corr[i][j]=1;
}
}
...And now i call the function smatrixevd! I've search all day for errors without find nothing...For the data just use two 200x200 px grayscale images (values between 0-255).
PS the correlation matrix (corr) is the main argument of smatrixevd!
PPS thanks in advance for the time you're spending on this problem!

Author:  Sergey.Bochkanov [ Wed Oct 27, 2010 7:06 am ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

I see a lot of uninitialized arrays - average, for example, is not set to zero on the beginning. Also, I recommend you to rewrite your code for 3x3 arrays instead of 200x200 and check correlation matrix by hand.

Author:  comix [ Wed Oct 27, 2010 10:02 am ]
Post subject:  Re: Negative eigenvalues of a symmetric matrix

You're right...In fact i've not posted the initialization part! I've also checked the code by hand (1000 times) before asking for help... In short, I tried everything possible! Just one question, the last, i hope! I've controlled results, and the covariance matrix and correlation matrix are symmetric. Now, we known that these matrix are positive semidefinite. Can i suppose that (semidefinite property) is verified? How can i test that? (naturally without the eigenvalues and eigenvectors)
PS on the web i found out that sometimes other software (like matlab) retrieve negative eigenvalues for semidefinite matrix because the sum of eigenvalues must be equal to the trace of matrix (or something, i don't understand it well).
Thanks again

Page 1 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/