MATLAB: Make Sample Covariance/Correlation Matrix Positive Definite

Statistics and Machine Learning Toolbox

I provide sample correlation matrix in copularnd() but I get error saying it should be positive definite. 
How to make my non-positive sample correlation matrix positive definite?

Best Answer

The fastest way for you to check if your matrix "A" is positive definite (PD) is to check if you can calculate the Cholesky decomposition (A = L*L') of it. You can calculate the Cholesky decomposition by using the command "chol(...)", in particular if you use the syntax :
[L,p] = chol(A,'lower');
you get a lower trianglular matrix "L"; if the decomposition exists (your matrix is PD) "p" will equal 0. If "A" is not positive definite, then "p" is a positive integer. For more details about this please refer to documentation page:
Sample covariance and correlation matrices are by definition positive semi-definite (PSD), not PD. Semi-positive definiteness occurs because you have some eigenvalues of your matrix being zero (positive definiteness guarantees all your eigenvalues are positive). If you correlation matrix is not PD ("p" does not equal to zero) means that most probably have collinearities between the columns of your correlation matrix, those collinearities materializing in zero eigenvalues and causing issues with any functions that expect a PD matrix.
To fix this the easiest way will be to do calculate the eigen-decomposition of your matrix and set the "problematic/close to zero" eigenvalues to a fixed non-zero "small" value. That can be easily achieved by the following code, given your initial correlation matrix "A":
[V,D] = eig(A); % Calculate the eigendecomposition of your matrix (A = V*D*V') % where "D" is a diagonal matrix holding the eigenvalues of your matrix "A"d= diag(D); % Get the eigenvalues in a vector "d" d(d <= 1e-7) = 1e-7; % Set any eigenvalues that are lower than threshold "TH" ("TH" here being % equal to 1e-7) to a fixed non-zero "small" value (here assumed equal to 1e-7)D_c = diag(d); % Built the "corrected" diagonal matrix "D_c"A_PD = V*D_c*V'; % Recalculate your matrix "A" in its PD variant "A_PD"
Take note that due to issues of numeric precision you might have extremely small negative eigenvalues, when you eigen-decompose a large covariance/correlation matrix. These extremely small negative eigenvalues are "machine zeros". The work-around present above will also take care of them.
This work-around does not take care of the conditioning number issues; it does reduces it but not substantially. Additionally the Frobenius norm between matrices "A_PD" and "A" is not guaranteed to be the minimum. A more mathematically involved solution is available in the reference: "Nicholas J. Higham - Computing the nearest correlation matrix - a problem from finance", IMA Journal of Numerical Analysis Volume 22, Issue 3, p. 329-343 (pre-print available here: http://eprints.ma.man.ac.uk/232/01/covered/MIMS_ep2006_70.pdf )