AI Wiki中文/EN

Positive definite matrices

正定矩阵

Distinguish positivity in every direction from positive entries, construct Cholesky factors, and handle semidefinite and numerical boundaries.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

Positive in every nonzero direction

The matrix A=[4223]A=\begin{bmatrix}4&2\\2&3\end{bmatrix} defines

x⊤Ax=4(x1+x22)2+2x22.x^\top Ax=4\left(x_1+\frac{x_2}{2}\right)^2+2x_2^2.

Every nonzero vector makes this expression positive. If the second square is zero, then x2=0x_2=0, and a nonzero x1x_1 makes the first square positive. This argument covers all directions at once, unlike testing a few points. A real symmetric matrix with this property is positive definite. MML, Section 3.2.3

The condition appears when a quadratic penalty must distinguish every nonzero error, a linear system should have a unique solution, or a nondegenerate Gaussian requires an invertible covariance. These applications use the same matrix property for different purposes.

Positive, semidefinite, and indefinite

Throughout this article, definiteness refers to real symmetric matrices. Write A≻0A\succ0 for positive definite and A⪰0A\succeq0 for positive semidefinite:

A≻0  ⟺  (∀x≠0, x⊤Ax>0),A⪰0  ⟺  (∀x, x⊤Ax≥0).A\succ0\iff (\forall x\ne0,\ x^\top Ax>0),\qquad A\succeq0\iff (\forall x,\ x^\top Ax\geq0).

A positive semidefinite matrix may have nonzero null directions. For A=[1111]A=\begin{bmatrix}1&1\\1&1\end{bmatrix}, the form is (x1+x2)2(x_1+x_2)^2, which vanishes at (1,−1)(1,-1). It is semidefinite and singular. An indefinite symmetric matrix has both positive and negative quadratic directions.

The spectral theorem gives a complete test: positive definiteness means all eigenvalues are positive, and semidefiniteness means all are nonnegative. Symmetry is part of that equivalence. A nonsymmetric matrix can have positive eigenvalues without a positive quadratic form. For example, [11001]\begin{bmatrix}1&10\\0&1\end{bmatrix} has eigenvalues one and one but quadratic value −8 at (1,−1)(1,-1). Convex Optimization, Appendix A.5.2

Another finite test for a symmetric matrix is Sylvester’s criterion: all leading principal determinants must be strictly positive. “Leading” means the top-left k×k block for each k from one to n. For a 2×2 matrix [abbd]\begin{bmatrix}a&b\\b&d\end{bmatrix}, this reduces to a>0a>0 and ad−b2>0ad-b^2>0. Completing the square verifies it directly:

ax12+2bx1x2+dx22=a(x1+bax2)2+ad−b2ax22(a>0).ax_1^2+2bx_1x_2+dx_2^2=a\left(x_1+\frac{b}{a}x_2\right)^2+\frac{ad-b^2}{a}x_2^2\quad(a>0).

Strict positivity cannot be replaced by nonnegativity in the same leading-minor test to characterize semidefiniteness. The matrix diag⁡(0,−1)\operatorname{diag}(0,-1) has both leading determinants zero but is negative on (0,1)(0,1).

A constructive test through Cholesky

Every real symmetric positive-definite matrix has a unique factorization A=LL⊤A=LL^\top with L lower triangular and positive diagonal. Conversely, an invertible L in such a factorization gives x⊤Ax=∥L⊤x∥22>0x^\top Ax=\|L^\top x\|_2^2>0. Matching entries recursively yields

Ljj=Ajj−∑k<jLjk2,Lij=Aij−∑k<jLikLjkLjj(i>j).L_{jj}=\sqrt{A_{jj}-\sum_{k<j}L_{jk}^2},\qquad L_{ij}=\frac{A_{ij}-\sum_{k<j}L_{ik}L_{jk}}{L_{jj}}\quad(i>j).

The same recursion explains the leading-minor criterion: a positive pivot multiplies the previous leading determinant by its squared diagonal factor. Each next positive leading determinant therefore permits the next positive pivot. MML, Section 4.3

The code implements dense Cholesky for finite real arrays and requires exact symmetry in the supplied entries. This explicit input rule avoids silently treating only one triangle as the matrix. Production APIs can use a selected triangle without verifying the other one; check that contract when using them. NumPy Cholesky documentation

import math

def cholesky(a):
    n = len(a)
    if not n or any(len(row) != n for row in a):
        raise ValueError("nonempty square matrix required")
    if any(not math.isfinite(x) for row in a for x in row):
        raise ValueError("finite real entries required")
    if any(a[i][j] != a[j][i] for i in range(n) for j in range(n)):
        raise ValueError("this example requires exactly symmetric input")
    lower = [[0.]*n for _ in range(n)]
    for i in range(n):
        for j in range(i+1):
            residual = a[i][j]-sum(lower[i][k]*lower[j][k] for k in range(j))
            if not math.isfinite(residual):
                raise ArithmeticError("nonfinite factorization")
            if i == j:
                if residual <= 0:
                    raise ValueError("not positive definite in this arithmetic")
                lower[i][j] = math.sqrt(residual)
            else:
                lower[i][j] = residual/lower[j][j]
                if not math.isfinite(lower[i][j]):
                    raise ArithmeticError("factor overflow")
    return lower

a = [[4.,2.],[2.,3.]]
lower = cholesky(a)
assert lower[0] == [2.,0.] and lower[1][0] == 1.
assert abs(lower[1][1]-math.sqrt(2)) < 1e-12
restored = [[sum(lower[i][k]*lower[j][k] for k in range(2)) for j in range(2)] for i in range(2)]
assert max(abs(restored[i][j]-a[i][j]) for i in range(2) for j in range(2)) < 1e-12
assert abs((lower[0][0]*lower[1][1])**2-8.) < 1e-12
assert cholesky([[9.]]) == [[3.]]
for bad in ([[1.,1.],[1.,1.]], [[1.,2.],[2.,1.]], [[0.,0.],[0.,-1.]],
            [[1.,2.],[0.,1.]], [[float('nan')]], []):
    try:
        cholesky(bad)
    except ValueError:
        pass
    else:
        raise AssertionError("invalid or non-SPD matrix accepted")

Dense factorization takes O(n3)O(n^3) arithmetic operations and stores O(n2)O(n^2) entries. For the example, L=[2012]L=\begin{bmatrix}2&0\\1&\sqrt2\end{bmatrix}, giving determinant (22)2=8(2\sqrt2)^2=8. To solve Ax=bAx=b after factorization, solve the two triangular systems Ly=bLy=b and L⊤x=yL^\top x=y; each right-hand side takes O(n2)O(n^2) work without forming an inverse.

Existence does not guarantee numerical comfort

A tiny positive eigenvalue still makes a matrix positive definite, but inversion can greatly amplify noise in that direction. Close to the boundary, rounding can cause Cholesky to encounter a nonpositive pivot even when the exact intended matrix is positive definite. Failure means the factorization did not succeed for the supplied arithmetic, not always a proof about an ideal exact matrix.

Adding δI\delta I shifts every eigenvalue by δ\delta. For δ>0\delta>0, this makes a positive semidefinite matrix positive definite; for an indefinite one it succeeds only when δ>−λmin⁡\delta> -\lambda_{\min}. This changes the problem, so document the adjustment instead of repeatedly adding an unexplained “jitter” until a routine stops failing.

A covariance matrix is always positive semidefinite under finite second moments, but can be singular when variables are linearly dependent. A Gram matrix X⊤XX^\top X is positive definite only when X has full column rank. Positive diagonal entries alone are also insufficient: they test coordinate directions while ignoring combinations.

Find the counterexample

  1. Is [1221]\begin{bmatrix}1&2\\2&1\end{bmatrix} positive definite because every entry is positive? Answer: no; (1,−1)(1,-1) gives −2.
  2. Is the all-ones 2×2 matrix positive semidefinite or positive definite? Answer: semidefinite, with null direction (1,−1)(1,-1).
  3. If the minimum eigenvalue is −3, does adding 2I2I make the matrix positive definite? Answer: no; its new minimum eigenvalue is −1. The shift must exceed three.
  4. Do nonnegative leading determinants alone prove semidefiniteness? Answer: no; diag⁡(0,−1)\operatorname{diag}(0,-1) is the counterexample above.

Read the condition where it is used

Positive definiteness supports metrics, covariance factorizations, and strictly convex quadratic objectives. Positive semidefiniteness supports kernel Gram matrices and convex quadratic objectives that may have flat directions. A nonlinear objective’s Hessian needs separate local or global assumptions; finding a positive-definite matrix somewhere in a model does not prove the entire training problem convex. Return to quadratic forms when interpreting the curvature represented by the matrix.

Make a prediction, then check

Must a symmetric matrix with positive diagonal entries be positive definite?

Choose one answer

Learning objectives

  • Distinguish positive definiteness and semidefiniteness using directions, eigenvalues, and leading minors.
  • Construct and verify Cholesky factors, explaining failure and diagonal-shift boundaries.

Prerequisites & connections

Explore related knowledge →

References

  1. Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
    Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020
  2. Convex Optimization
    Stephen Boyd; Lieven Vandenberghe · 2004
  3. NumPy — numpy.linalg.cholesky
    NumPy developers

Version1 · Editorial status: Recorded technical checks completed

View technical check record (AI)

Codex · 2026-09-05 · Codex technical check: cited-source comparison, derivation and numerical checks, bilingual argument comparison, and executed Python assertions; no independent human peer review.

  • Constructed 48 positive-definite matrices of sizes 1×1 through 6×6 from positive-diagonal triangular factors, checking recovery and exact leading minors.
  • Verified counterexamples for semidefiniteness, positive-diagonal indefiniteness, nonsymmetric positive eigenvalues and nonnegative leading minors; stated diagonal-shift and rounding boundaries.
  • Compared claims, assumptions, examples, exercises and citations across languages; structural validation additionally compares display equations, code and source lists.

Source locations checked:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.