Positive Definite Matrices in Kernels and Optimization
Learn what positive definiteness means, how to test for it, and why kernels, covariance matrices, Newton steps and convexity depend on it.
Key takeaways
- Positive definite means xᵀAx > 0 for all nonzero x, which for symmetric matrices is the same as all eigenvalues being positive.
- Positive entries do not guarantee positive definiteness, and negative entries do not rule it out.
- A twice differentiable function on a convex domain is convex exactly when its Hessian is positive semidefinite everywhere there.
- Every valid kernel produces positive semidefinite Gram matrices for any set of inputs.
- A Cholesky factorization is the practical test, and a small diagonal addition repairs near-singular cases.
A symmetric matrix A is positive definite if xᵀAx > 0 for every nonzero vector x. It is positive semidefinite if xᵀAx ≥ 0. In words, the quadratic expression built from the matrix never goes below zero, and in the definite case it is zero only at the origin.
This property is what makes a quadratic function bowl-shaped, a covariance matrix legitimate and a kernel usable. When it fails, optimizers can head the wrong way and factorizations stop with an error.
Two examples
Take A with rows (2, −1) and (−1, 2).
xᵀAx = 2x₁² − 2x₁x₂ + 2x₂²
= x₁² + x₂² + (x₁ − x₂)² Every term is a square, and they cannot all vanish unless x is zero. So A is positive definite, despite its negative entries. Its eigenvalues are 1 and 3.
Now take B with rows (1, 2) and (2, 1). All entries are positive, but for x = (1, −1) you get xᵀBx = 1 − 4 + 1 = −2. B is indefinite. Its eigenvalues are 3 and −1.
Equivalent tests
For a real symmetric matrix, these statements all mean positive definite:
- xᵀAx > 0 for every nonzero x.
- All eigenvalues are positive.
- A = LLᵀ for a lower triangular L with positive diagonal entries, the Cholesky factorization.
- All leading principal minors, the determinants of the top-left square blocks, are positive.
The first is the definition, useful in proofs. The eigenvalue version gives intuition. Cholesky is what you run on a computer. The determinant test is handy for tiny matrices by hand. Do not extend it carelessly to the semidefinite case, which requires checking all principal minors, not just the leading ones.
The shape of a quadratic
Consider f(w) = ½ wᵀAw − bᵀw. Its gradient is Aw − b and its Hessian, the matrix of second derivatives, is A.
| Type of A | Shape of f | Minimum |
|---|---|---|
| Positive definite | A bowl | Exactly one, where Aw = b |
| Semidefinite with a zero eigenvalue | A valley with a flat direction | Many, or none if b tilts the flat direction |
| Indefinite | A saddle | None, since f falls without limit |
Stretched bowls and changes of variable
The level sets of ½wᵀAw for a positive definite A are ellipses, or ellipsoids in more dimensions. Their axes point along the eigenvectors, and each half-length is proportional to 1/√λ. A large eigenvalue means a steep, narrow direction, and a small one means a long, shallow direction.
The Cholesky factor turns the ellipse into a circle. Setting u = Lᵀw gives wᵀAw = uᵀu, an ordinary squared length. The same factor works in the other direction for sampling: if z holds independent standard normal numbers, then Lz has covariance LLᵀ = A. This is the usual way to draw correlated Gaussian variables.
A positive definite matrix also defines a distance, √((x − z)ᵀA(x − z)). With A equal to the inverse covariance, this is the Mahalanobis distance. Some metric learning methods learn A itself and keep it valid by writing it as MᵀM, which can never be indefinite. That trick of building the property into the parameterization is common: model a covariance through its Cholesky factor, with positive numbers on the diagonal, and every gradient step stays legal.
Convexity and second-order steps
For a twice differentiable function on a convex domain, the function is convex exactly when its Hessian is positive semidefinite everywhere in that domain. A Hessian that is positive definite everywhere gives strict convexity, although the reverse does not hold: x⁴ is strictly convex with zero second derivative at 0.
Squared error for linear regression has Hessian proportional to XᵀX. That matrix is always semidefinite, because aᵀXᵀXa = ‖Xa‖². It is definite exactly when the columns of X are linearly independent.
Newton's method moves by −H⁻¹g, where g is the gradient. If H is positive definite, this direction goes downhill, because gᵀH⁻¹g > 0. If H is indefinite, the step may head toward a saddle point. A common repair is to use H + λI with λ large enough to make all eigenvalues positive. This topic connects to Newton and quasi-Newton methods.
Gradient descent on a positive definite quadratic converges when the learning rate η satisfies 0 < η < 2/λ_max. The ratio of the largest to smallest eigenvalue then controls how fast.
Deep network losses are generally not convex, and their Hessians usually have both positive and negative eigenvalues at many points.
Gram matrices and kernels
Given vectors x₁ to x_n, the Gram matrix has entries K_ij = x_i · x_j, so K = XXᵀ. For any coefficient vector a, aᵀKa = ‖Xᵀa‖² ≥ 0. Every Gram matrix is semidefinite.
A kernel k(x, z) is a function that acts like a dot product in some feature space. The requirement is that for any finite set of inputs, the matrix of kernel values is symmetric and positive semidefinite. If it is, a corresponding feature space exists. The idea is shown concretely in the kernel trick with a worked example.
| Kernel | Formula | Note |
|---|---|---|
| Linear | x · z | The ordinary Gram matrix |
| Polynomial | (x · z + c)ᵖ | Valid for c ≥ 0 and positive integer p |
| Gaussian (RBF) | exp(−‖x − z‖² / (2ℓ²)) | Definite for distinct inputs in exact arithmetic |
| Sigmoid | tanh(a x · z + c) | Not semidefinite in general |
Sums of kernels, products of kernels and positive multiples of kernels are again kernels. That lets you compose similarity measures safely.
Semidefiniteness is what keeps the optimization behind support vector machines convex.
Solving kernel systems
Kernel ridge regression and Gaussian process regression solve (K + λI)α = y, with K of shape (n, n). If K is semidefinite and λ > 0, every eigenvalue of the sum is at least λ, so the system is positive definite and has a unique solution.
Cholesky is the standard tool. For A with rows (4, 2) and (2, 3):
L = [[2, 0], [1, √2]]
L Lᵀ = [[4, 2], [2, 1 + 2]] = A
det A = (2 · √2)² = 8, matching 4·3 − 2·2 You then solve two triangular systems. The log determinant, needed for a Gaussian likelihood, is twice the sum of the logs of the diagonal of L. The factorization costs about n³/3 operations, which limits exact methods to moderate n.
Why the added term helps numerically
If K has eigenvalues μᵢ, then K + λI has eigenvalues μᵢ + λ with the same eigenvectors. Suppose the largest is 100 and the smallest is 0.000001.
| λ added | Largest | Smallest | Ratio of largest to smallest |
|---|---|---|---|
| 0 | 100 | 0.000001 | 100,000,000 |
| 0.01 | 100.01 | 0.010001 | about 10,000 |
| 1 | 101 | 1.000001 | about 101 |
A smaller ratio means a solution that is less sensitive to rounding and noise. The price is bias: along an eigen-direction, kernel ridge scales the fitted value by μ/(μ + λ), so weak directions are suppressed. Choose λ by validation, as you would any regularization strength, and distinguish it from a tiny jitter added purely to keep the factorization from failing.
Checking in code
NumPy's Cholesky routine reads only one triangle of its input, so test symmetry separately.
import numpy as np
def is_positive_definite(A, tol=1e-10):
if not np.allclose(A, A.T, atol=tol):
return False
try:
np.linalg.cholesky(A)
return True
except np.linalg.LinAlgError:
return False
print(is_positive_definite(np.array([[2.0, -1], [-1, 2]]))) # True
print(is_positive_definite(np.array([[1.0, 2], [2, 1]]))) # False Near the boundary, rounding decides the answer. Two almost identical inputs give an RBF Gram matrix with two almost identical rows, and its smallest computed eigenvalue may be slightly negative. Adding a small jitter to the diagonal is the usual remedy. Increase it gradually, and treat a large required value as a sign of a mistake or poor scaling.
Where invalid matrices come from
- A covariance matrix estimated entry by entry from different subsets of rows, as with pairwise deletion of missing values, can have negative eigenvalues.
- Hand-edited or rounded correlation tables may be impossible: three variables cannot all be strongly negatively correlated with one another.
- A custom similarity score, such as an edit-distance formula, need not be a kernel.
- A numerically computed Hessian may be slightly asymmetric. Averaging it with its transpose restores symmetry but not definiteness.
For three variables with every pairwise correlation equal to ρ, the matrix is semidefinite only if ρ ≥ −0.5.
Common questions
Does positive definite require symmetry?
In machine learning usage it almost always assumes a real symmetric matrix, or a Hermitian one for complex numbers. For a general square matrix, xᵀAx depends only on the symmetric part (A + Aᵀ)/2.
Is the sum of two positive definite matrices positive definite?
Yes, since the two positive quadratic expressions add. The ordinary matrix product of two of them need not even be symmetric.
Do I need to check every kernel matrix?
Not for a standard kernel applied correctly, since the theory guarantees semidefiniteness in exact arithmetic. Check when you write a custom similarity, combine kernels in unusual ways such as subtracting one from another, or see a factorization fail. Print the smallest few eigenvalues: values near zero with a negative sign usually point to rounding, while clearly negative ones point to a function that is not a kernel.