Inverting a Gaussian (RBF) Kernel Matrix Without Numerical Trouble
At a glance
- 1. Build the kernel
Compute Kᵢⱼ = exp(−‖xᵢ − xⱼ‖² / 2σ²) for your points.
- 2. Add a ridge term
Use K + λI with a small λ (noise variance or jitter) to tame conditioning.
- 3. Factor with Cholesky
Compute K + λI = LLᵀ instead of forming the inverse explicitly.
- 4. Solve, don't invert
Get α = (K + λI)⁻¹y with two triangular solves, then predict with k(x, X)α.
Try it: Matrix calculator
The Gaussian (RBF) kernel matrix for points is
You need its inverse, or more precisely , in kernel ridge regression, Gaussian process regression and RBF interpolation. There are two separate questions: is invertible in theory, and can you invert it reliably in practice?
In theory: always invertible for distinct points
The Gaussian kernel is strictly positive definite. For any distinct points and any non-zero vector ,
This follows from Bochner’s theorem: the Gaussian’s Fourier transform is again a Gaussian, which is positive everywhere. So is symmetric positive definite, all its eigenvalues are positive, and exists. Duplicate points produce identical rows and make singular.
In practice: often numerically singular
The smallest eigenvalue of can be astronomically small. That happens when:
- points are close together compared with , so nearby rows are nearly identical, or
- is large compared with the spread of the data, so every entry is close to 1.
A condition number of or more is common, at which point double-precision arithmetic can’t distinguish from a singular matrix.
Two-point example
With two points at distance , write :
For : and , so
As (or ), and the entries of blow up like . The eigenvalues are , so the condition number is . With points the effect compounds quickly.
What to do instead
1. Add a ridge (nugget, jitter) term. Replace by with . Every eigenvalue moves up by , so the condition number is at most .
- In kernel ridge regression and GP regression, is part of the model: the regularisation strength or the noise variance .
- For pure interpolation, use a tiny jitter such as to times the mean of the diagonal, accepting a tiny loss of exactness.
2. Solve, don’t invert. You almost never need itself. Factor once with Cholesky, , then solve two triangular systems:
import numpy as np
from scipy.linalg import cho_factor, cho_solve
from scipy.spatial.distance import cdist
def rbf_kernel(X, Y, sigma):
return np.exp(-cdist(X, Y, "sqeuclidean") / (2 * sigma**2))
K = rbf_kernel(X, X, sigma)
c, low = cho_factor(K + lam * np.eye(len(X))) # fails loudly if not positive definite
alpha = cho_solve((c, low), y) # alpha = (K + lam*I)^{-1} y
y_pred = rbf_kernel(X_new, X, sigma) @ alpha
Cholesky costs about operations, roughly half the cost of an LU factorisation. It is backward stable for positive definite matrices, and its diagonal gives you for free (needed for GP marginal likelihoods).
3. Choose sensibly. A common starting point is the median pairwise distance (the “median heuristic”). Then tune and by cross-validation or by maximising the marginal likelihood.
4. For large , use low-rank approximations (Nyström, random Fourier features) or iterative solvers (conjugate gradients), which avoid the factorisation.
Common mistakes
- Calling
np.linalg.inv(K)and multiplying. It is slower and less accurate than a solve, and it hides ill-conditioning. - Treating a Cholesky failure as a bug. It is a signal that is numerically singular. Increase or check for duplicate points.
- Using the same after rescaling the data. The appropriate depends on the scale of and .
References
- Rasmussen, C. E. & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning, Section 2.2 and Appendix A.4. MIT Press.
- Wendland, H. (2005). Scattered Data Approximation, Chapter 6 and Chapter 12. Cambridge University Press.
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.