Checking positive semidefiniteness in MATLAB

7.6k Views Asked by At

Let $\mathbf{A}$ be a $n\times n$ matrix. I want to check in MATLAB if it is PSD or not. Which tests, in MATLAB, should I do for this purpose?

I know that if $\mathbf{A}$ is PSD then following holds

  • eigenvalue decomposition: [V,lambda]=eig(A), then $\lambda_i\geq 0 \hspace{2mm}\forall \hspace{2mm} i=1:n$
  • Cholesky decomposition: [R,p] = chol(A) then $p=0$

WHY: I am asking for a list because I am having an issue. I started with a matrix $\mathbf{X}$ that was supposed to be PSD because of the way I was constructing it. However, some of its eigen values were negative. It turned out that floating point arithmetic is to be blamed for this.

Problem: I came across https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd. According to this function, $\mathbf{A}$=nearestSPD($\mathbf{X}$), $\mathbf{A}$ is the nearest SPD to $\mathbf{X}$. In help section, author mentioned that [R,p]=chol(A) will return p=0 which proves that A is SPD. Now the problem is that after getting $\mathbf{A}$, I do get p=0 for chol($\mathbf{A}$), but I am still getting one or more negative eigen values. Although the magnitude of these negative eigen values is very small something like e-15 but still the sign is negative.

So I don't understand why these tests are giving different results and what should one do in such a case.