Post · 7 min read· pp. 691–694

What Is the Nearest Positive Semidefinite Matrix?

Given a symmetric matrix and a nonnegative number δ\delta, what is the nearest symmetric matrix whose eigenvalues are all at least δ\delta? In other words, how can we project a symmetric matrix onto the set of symmetric positive semidefinite matrices with eigenvalues at least δ\delta?

Distance can be measured in any norm, but the most common choice is the Frobenius norm, ∥A∥F=(∑i,j=1n∣aij∣2)1/2\|A\|_F = (\sum_{i,j = 1}^n |a_{ij}|^2)^{1/2}.

This problem occurs in a very wide range of applications. A typical scenario is that a covariance matrix is approximated in some way that does not guarantee positive semidefinitess, for example by treating blocks of the matrix independently. In machine learning, some methods use indefinite kernels and these can require an indefinite similarity matrix to be replaced by a semidefinite one. When δ>0\delta > 0, the problem arises when the matrix is positive definite but ill conditioned and a matrix of smaller condition number is required, or when rounding errors result in small negative eigenvalues and a “safely positive definite” matrix is wanted.

The following theorem gives the solution to the problem for the Frobenius norm.

Theorem (Cheng and Higham, 1998).

Let the symmetric matrix A∈Rn×nA\in\mathbb{R}^{n \times n} have the spectral decomposition A=Q diag(λi) QTA = Q \mskip1mu\mathrm{diag}(\lambda_i) \mskip1muQ^T and let δ≥0\delta \ge 0. The unique matrix with smallest eigenvalue at least δ\delta nearest to AA in the Frobenius norm is given by

X=Q diag(τi) QT,τi={λi,λi≥δδ,λi<δ.\notag X = Q \mskip1mu \mathrm{diag}(\tau_i) \mskip1mu Q^T, \quad \tau_i = \begin{cases} \lambda_i, & \lambda_i \geq \delta\\ \delta, & \lambda_i < \delta. \end{cases}

The theorem says that there is a unique nearest matrix and that is has the same eigenvectors as AA with any eigenvalues less than δ\delta increased to δ\delta. When δ=0\delta = 0, another way to express this result is in terms of the polar decomposition A=UHA = UH, where UU is orthogonal and HH is symmetric positive definite (with H=Q diag(∣λi∣)QTH = Q \mskip1mu\mathrm{diag}(|\lambda_i|)Q^T, since AA is symmetric): X=(A+H)/2X = (A + H)/2.

One can pose the same nearness problem for nonsymmetric AA. For the Frobenius norm there is again a unique nearest symmetric matrix whose eigenvalues are all at least δ\delta, and it is given by applying the theorem to the symmetric part BB of AA (which is the nearest symmetric matrix to AA), where

A=12(A+AT)+12(A−AT)≡B+C,(B=BT, C=−CT).\notag A = \frac{1}{2} (A + A^T ) + \frac{1}{2} (A - A^T ) \equiv B + C, \quad(B = B^T, ~C = -C^T).

For the 22-norm the answer is known for nonsymmetric AA only for δ=0\delta = 0, so we take δ=0\delta = 0 from this point on. We write X≥0X \ge 0 to denote that the symmetric matrix XX is positive semidefinite and use a superscript 1/21/2 to denote the positive semidefinite square root.

Theorem (Halmos, 1972).

The 22-norm distance from A∈Rn×nA\in\mathbb{R}^{n \times n} to the nearest positive semidefinite matrix is

d2(A)=min⁡{ r≥0:r2I+C2≥0 and G(r)≥0 },\notag d_2(A) = \min\{\, r \ge 0: r^2I + C^2 \ge 0 ~\mathrm{and}~ G(r) \ge 0 \,\},

where

G(r)=B+(r2I+C2)1/2,\notag G(r) = B + \bigl(r^2I + C^2\bigr)^{1/2},

and a nearest matrix is

X=B+(d2(A)2I+C2)1/2.\notag X = B + (d_2(A)^2I + C^2)^{1/2}.

When AA is symmetric, so that C=0C = 0, the nearest matrix and the distance reduce to

X=B+d2(A)I,d2(A)=max⁡{ ∣λi∣:λi<0 }.\notag X = B + d_2(A)I, \quad d_2(A) = \max\{\, |\lambda_i| : \lambda_i < 0 \,\}.

Clearly, XX is not, in general, unique because when AA is symmetric, instead of shifting BB by a multiple of II we could just perturb the negative eigenvalue to zero, as with the optimal Frobenius norm perturbation, without changing the 22-norm of the perturbation. A distinguishing feature of XX in Halmos’s theorem is that X−P≥0X-P \ge 0 for any other nearest positive semidefinite matrix PP (Bouldin, 1973, Theorem 4.2); thus XX has the minimum number of zero eigenvalues over all nearest positive semidefinite matrices.

The Frobenius norm solution XFX_F is an approximate minimizer of the 22-norm distance since (Higham, 1988)

d2(A)≤∥A−XF∥F≤2d2(A).\notag d_2(A) \le \| A - X_F \|_F \le 2 d_2(A).

Halmos’s theorem simplifies the computation of d2(A)d_2(A) because it reduces the minimization problem to one dimension. However, the problem is nonlinear and has no closed form solution for general AA, so iterative methods are needed to compute d2(A)d_2(A). Two algorithms are developed in Higham (1988). Both are based on the following properties of the matrix G(r)G(r) in the theorem: λmin⁡(G(r))\lambda_{\min}(G(r)) is monotone increasing on [ ρ(C),∞)[\,\rho(C), \infty), and either λmin⁡(G(ρ(C)))≥0\lambda_{\min}(G(\rho(C))) \ge 0, in which case d2(A)=ρ(C)d_2(A) = \rho(C), or λmin⁡(G(r))\lambda_{\min}(G(r)) has a unique zero r∗=d2(A)>ρ(C)r^* = d_2(A) > \rho(C). The first algorithm uses the bisection method, determining the sign of λmin⁡(G(r))\lambda_{\min}(G(r)) by attempting a Cholesky factorization of G(r)G(r): the sign is nonnegative if the factorization exists and negative otherwise. This approach is attractive if d2(A)d_2(A) is required only to low accuracy. For higher accuracy computations it is better to apply a more rapidly converging zero finder to f(r)=λmin⁡(G(r))=0f(r) = \lambda_{\min} (G(r)) = 0. A hybrid Newton–bisection method is used in Higham (1988).

Example

For a numerical example (in MATLAB), we take the Jordan block

A=[0100000100000100000100000].\notag A = \begin{bmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 \end{bmatrix}.

The symmetric part of AA, B=(A+AT)/2B = (A+A^T)/2, has two negative eigenvalues and a zero eigenvalue:

text
>> A = gallery('jordbloc',5,0); As = sym(A); eig_B = eig((As+As')/2)'
eig_B =
[-1/2, 0, 1/2, -3^(1/2)/2, 3^(1/2)/2]
>> double(eig_B)
ans =
  -5.0000e-01            0   5.0000e-01  -8.6603e-01   8.6603e-01

The nearest symmetric positive semidefinite matrix to AA in the Frobenius norm can be computed in MATLAB as

text
B = (A + A')/2; [Q,D] = eig(B); d = diag(D); X_F = Q*diag(max(d,0))*Q';

We can improve this code by using the implicit expansion feature of MATLAB to avoid forming a diagonal matrix. Since the computed result is not exactly symmetric because of rounding errors, we also need to replace it by the nearest symmetric matrix:

text
[Q,d] = eig(B,'vector'); X_F = Q*(max(d,0).*Q'); X_F = (X_F + X_F')/2;

We obtain

text
X_F = 
 1.972e-01   2.500e-01   1.443e-01  -4.163e-17  -5.283e-02  
 2.500e-01   3.415e-01   2.500e-01   9.151e-02  -1.665e-16  
 1.443e-01   2.500e-01   2.887e-01   2.500e-01   1.443e-01  
-4.163e-17   9.151e-02   2.500e-01   3.415e-01   2.500e-01  
-5.283e-02  -1.665e-16   1.443e-01   2.500e-01   1.972e-01

which has eigenvalues

text
-3.517e-17   7.246e-17   9.519e-17   5.000e-01   8.660e-01  

Notice that the three eigenvalues that would be zero in exact arithmetic are of order the unit roundoff. A nearest matrix in the 22-norm, as given by Halmos’s theorem. is

text
X_2 = 
 8.336e-01   5.000e-01   1.711e-01   0.000e+00  -1.756e-02  
 5.000e-01   6.625e-01   5.000e-01   1.887e-01   0.000e+00  
 1.711e-01   5.000e-01   6.450e-01   5.000e-01   1.711e-01  
 0.000e+00   1.887e-01   5.000e-01   6.625e-01   5.000e-01  
-1.756e-02   0.000e+00   1.711e-01   5.000e-01   8.336e-01 

and its eigenvalues are

text
-7.608e-17   1.281e-01   5.436e-01   1.197e+00   1.769e+00  

The distances are

1.732=∥A−XF∥F<∥A−X2∥F=2.207,0.9872=∥A−X2∥2<∥A−XF∥2=1.0355.\notag \begin{aligned} 1.732 &= \|A - X_F\|_F < \|A - X_2\|_F = 2.207,\\ 0.9872 &= \|A - X_2\|_2 < \|A - X_F\|_2 = 1.0355. \end{aligned}

References

This is a minimal set of references, which contain further useful references within.

This article is part of the “What Is” series, available from https://nhigham.com/category/what-is and in PDF form from the GitHub repository https://github.com/higham/what-is.