forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 8:45 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Negative eigenvalues of a symmetric matrix
PostPosted: Tue Oct 26, 2010 9:06 am 
Offline

Joined: Tue Oct 26, 2010 8:56 am
Posts: 9
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


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Tue Oct 26, 2010 10:26 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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).


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Tue Oct 26, 2010 10:27 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
P.S. If you have symmetric matrix, I recommend you to use smatrixevd(). General nonsymmetric EVD solvers are always slower and less stable.


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Tue Oct 26, 2010 10:46 am 
Offline

Joined: Tue Oct 26, 2010 8:56 am
Posts: 9
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!


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Tue Oct 26, 2010 10:55 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Tue Oct 26, 2010 12:24 pm 
Offline

Joined: Tue Oct 26, 2010 8:56 am
Posts: 9
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!


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Tue Oct 26, 2010 12:46 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Wed Oct 27, 2010 1:41 am 
Offline

Joined: Tue Oct 26, 2010 8:56 am
Posts: 9
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!


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Wed Oct 27, 2010 7:06 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
 Post subject: Re: Negative eigenvalues of a symmetric matrix
PostPosted: Wed Oct 27, 2010 10:02 am 
Offline

Joined: Tue Oct 26, 2010 8:56 am
Posts: 9
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group