Matlab sometimes produce a covariance matrix error with non-postive semidefinite?

31 views (last 30 days)
**The error message: ??? Error using ==> frontcon at 110 Non-positive-
semidefinite covariance input.**
I've received error messages by my covariance matrix was not positive semi-
definite when it should be.
I've checked if the matrix of positive semi-definite was correct or not.The
matrix actually was correct,but the results was error
with uncertainty.it's weird.
what reason will cause it be error??!
this is the following two examples used 3 stocks sample data wuth "5 months"
and "10 months",the "10 months" can be caculated as usual,but the "5 months" still had error of non semi-definite..
10 months covariance matrix(5*5) :(OK)
0.0379600111694145 0.0181198113555440 0.0245391927792438 0.0202414640681373 0.0279268180283850
0.0181198113555440 0.0142124395898704 0.0119903735426172 0.0118857761712285 0.0196158481871085
0.0245391927792438 0.0119903735426172 0.0375108942197987 0.0167779614358132 0.0222940157947426
0.0202414640681373 0.0118857761712285 0.0167779614358132 0.0276863287987090 0.0437773542604056
0.0279268180283850 0.0196158481871085 0.0222940157947426 0.0437773542604056 0.0803892314756841
5 months covariance matrix(5*5) : (error message:Error using ==> frontcon at 110 Non-positive-semidefinite covariance input.)
0.0268462313063243 0.0118712128417407 0.00611131348529208 0.00932646577869477 0.0100659678331899
0.0118712128417407 0.00883612011990003 0.00641799277052432 0.00740291158510098 0.0137403298989520
0.00611131348529208 0.00641799277052432 0.00775523357835324 0.00759492726799908 0.0114642498004625
0.00932646577869477 0.00740291158510098 0.00759492726799908 0.00794974788196056 0.0116172189467572
0.0100659678331899 0.0137403298989520 0.0114642498004625 0.0116172189467572 0.0279127019699328
what's the different in these two cases?is that any problem in 5 months data? why did it calculate error?what reason cause it can't be?How should i improve that?

Answers (4)

Matt J
Matt J on 26 Dec 2012
You posted this question before. The answer was straightforward: your matrices are not positive semi-definite, so the error messages you are getting are completely legitimate.
The logical thing to do would be to question the way you are building your input matrix and examine it for errors.
  1 Comment
Matt J
Matt J on 26 Dec 2012
Edited: Matt J on 26 Dec 2012
Bear in mind, in particular, that your input matrix will need to be distinctly positive definite, so as to avoid numerical issues. For example, the matrix x*x.' should always be positive semi-definite, but as you can see below, floating point computation inaccuracies can make some of its eigenvalues look negative, implying that it is not positive semi-definite
>> x=rand(100,1); y=eig(x*x'); y(y==min(y))
ans =
-2.9587e-15

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Dec 2012
The smallest eigenvalue of the second matrix is calculated as -9.60780289613104e-18 so as far as MATLAB is concerned, the matrix is not positive semi-definite.
As I indicated in your earlier thread, if you matrix is close to being not positive semi-definite then round-off error can lead to it being calculated as not positive semi-definite. Round-off error is inevitable in any floating point system.
How are you calculating these matrices?

Teja Muppirala
Teja Muppirala on 27 Dec 2012
This is just caused by roundoff error. Since you know they really should be zero (and not small negative numbers), you can do this:
% Your original matrix
M = [0.0268462313063243 0.0118712128417407 0.00611131348529208 0.00932646577869477 0.0100659678331899;
0.0118712128417407 0.00883612011990003 0.00641799277052432 0.00740291158510098 0.0137403298989520 ;
0.00611131348529208 0.00641799277052432 0.00775523357835324 0.00759492726799908 0.0114642498004625;
0.00932646577869477 0.00740291158510098 0.00759492726799908 0.00794974788196056 0.0116172189467572;
0.0100659678331899 0.0137403298989520 0.0114642498004625 0.0116172189467572 0.0279127019699328];
% It has a small negative eigenvalue:
min(eig(M))
% Try to fix it:
[V,D] = eig(M);
M_new = V*max(D,0)/V;
% See that it is now fixed
min(eig(M_new))
FRONTCON actually does not check positive definiteness using EIG, but rather LDL, but I think the above process should still work. If it doesn't, you could also just add a very small positive number to the diagonal of M, thus shifting all its eigenvalues up.
M_new = M + eye(size(M))*(-2*min(eig(M)));
Since you are working with financial data, it's not like your numbers are accurate to 16 digits anyways, so it is not that big of a deal to modify the matrices like this.
  1 Comment
Teja Muppirala
Teja Muppirala on 27 Dec 2012
In your case, the matrices were almost positive semidefinite. However, unlike this case, if you matrices were really quite a bit off from being positive-semidefinite, then you might not be able to get away with doing something so simple like just adding something to the diagonal.
In that case, there are more sophisticated algorithms to find the nearest positive semidefinite matrix, and you may be able to search around and find something, or maybe someone else has a recommendation. I don't know much about it.

Sign in to comment.


Greg Heath
Greg Heath on 27 Dec 2012
With knowledge of the physical/mathematical problem you can probably attach a significant meaning to the existence of small negative eigenvalues corresponding to small, nearly zero singular values. In my work(radar signals) I found that some of my variables were linearly dependent.
Then I used two methods to mitigate the problem:
1. Truncated pseudoinversion (keeping 99% of the trace)instead of ordinary matrix inversion
2. Deleted some of the variables that were linearly dependent (and, therefore that added no additional information)
Look at the eigenvectors corresponding to the near zero eigenvalues. The coefficients will tend to indicate which variables are redundant.
Another method, mentioned previously, is regularization. By adding a small multiple of the unit matrix to the original, problems tend to go away. However, you may have to use trial and error to find a near optimal value for the small multiplier (Statisticians seem to like this method better).
Hope this helps.
Thank you for formally accepting my answer.
Greg

Categories

Find more on Strategy & Logic in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!