Why does EWSTATS sometimes produce a covariance matrix which isn't positive semi-definite?

2 views (last 30 days)
I receive error messages stating that my covariance matrix is not positive semi-definite when it should be. Sometimes this happens when I've created it with COV or EWSTATS.
ERROR: ??? Error using ==> frontcon
Covariance matrix must be positive semi-definite.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 19 May 2023
Edited: MathWorks Support Team on 19 Apr 2023
These situations can occur when you have given COV or EWSTATS a small number of effective samples, increasing the effects of machine imprecision and causing small negative eigenvalues in the covariance matrix. If you are not familiar with the types of errors present when using computers to perform floating point calculations, see the Related Solution listed at the bottom of the page.
With EWSTATS, this is typically caused by using a small WindowLength input or by having a few observations compared to the number of securities being used. With COV, this means that you need more observations in your data to counter the effects of this round-off error.
In the meantime, one possible alternative is to use a larger value for WindowLength or to use more samples in your data, which is a more numerically sound suggestion for forming the covariance matrix.
Another alternative includes removing the error checking for this type of situation in the FRONTCON or PORTCON functions. The first method is recommended, however if you want to do this you can comment out the lines which look like:
if any( eig(ECov) < 0 )
error('Covariance matrix must be positive semi-definite.');
end
These lines are found in the beginning of the FRONTCON and PORTCON functions.
For some customers who are unable to add more samples, the following File Exchange submission might be useful:
https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!