Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Kernel Approximations: Nystrom & Random Fourier Features

Full kernel matrices cost O(N2)O(N^2) memory and O(N3)O(N^3) time to factorize. For large-scale GP inference we need structured approximations that preserve the low-rank nature of the kernel.

This notebook showcases six gaussx utilities:

FunctionReturnsPurpose
nystrom_operatorLowRankUpdateNystrom low-rank approximation
rff_operatorLowRankUpdateRandom Fourier features approximation
center_kernelMatrixLinearOperatorCenter a kernel matrix (HKHHKH)
centering_operatorLowRankUpdateCentering matrix H=I−11T/nH = I - 11^T/n
hsicscalarHilbert-Schmidt Independence Criterion
mmd_squaredscalarMaximum Mean Discrepancy (squared)

Background

Nystrom approximation. Given NN data points and M≪NM \ll N inducing (landmark) points ZZ, the Nystrom method approximates the full kernel matrix as

Kff≈Qff=KfMKMM−1KMfK_{ff} \approx Q_{ff} = K_{fM} K_{MM}^{-1} K_{Mf}

This is a rank-MM positive semi-definite matrix. Internally, gaussx stores it as a LowRankUpdate with zero base and factor U=KfMLMM−⊤U = K_{fM} L_{MM}^{-\top} where LMML_{MM} is the Cholesky factor of KMMK_{MM}, so that Qff=UU⊤Q_{ff} = U U^\top. Adding noise gives Qff+σ2IQ_{ff} + \sigma^2 I, a LowRankUpdate that can be solved in O(NM2)O(NM^2) via the Woodbury identity.

Random Fourier Features (RFF). Rahimi & Recht (2007) showed that for shift-invariant kernels the kernel function can be approximated via a finite-dimensional random feature map:

k(x,x′)≈ϕ(x)⊤ϕ(x′),ϕj(x)=2Dcos⁡(ωj⊤x+bj)k(x, x') \approx \phi(x)^\top \phi(x'), \quad \phi_j(x) = \sqrt{\frac{2}{D}} \cos(\omega_j^\top x + b_j)

where ωj\omega_j is drawn from the spectral density of the kernel and bj∼Uniform(0,2π)b_j \sim \mathrm{Uniform}(0, 2\pi). For the RBF kernel with lengthscale ℓ\ell, the spectral density is ω∼N(0,ℓ−2I)\omega \sim \mathcal{N}(0, \ell^{-2} I). The feature matrix Φ∈RN×D\Phi \in \mathbb{R}^{N \times D} gives a rank-DD approximation K≈ΦΦ⊤K \approx \Phi \Phi^\top, again stored as a LowRankUpdate.

Setup: 1D GP regression data

We generate 500 noisy observations from a smooth function and define an RBF kernel helper.

<Figure size 1000x400 with 1 Axes>

RBF kernel helper

3. Nystrom Approximation

We select MM inducing points on an evenly-spaced grid and build the Nystrom low-rank approximation.

Nystrom operator type: LowRankUpdate

Approximation quality

Compare the Nystrom approximation against the full kernel via the Frobenius norm error and a diagonal comparison.

Frobenius norm error:    0.0000
Relative Frobenius err:  0.0000
<Figure size 1000x400 with 1 Axes>

GP regression with the Nystrom approximation

The system to solve is (Knys+σ2I)α=y(K_{\mathrm{nys}} + \sigma^2 I) \alpha = y. Because KnysK_{\mathrm{nys}} is a LowRankUpdate, gaussx exploits the Woodbury identity automatically, giving O(NM2)O(NM^2) cost instead of O(N3)O(N^3).

<Figure size 1000x400 with 1 Axes>

4. Random Fourier Features

For the RBF kernel with lengthscale ℓ\ell, we sample ω∼N(0,ℓ−2I)\omega \sim \mathcal{N}(0, \ell^{-2} I) and b∼Uniform(0,2π)b \sim \mathrm{Uniform}(0, 2\pi).

RFF operator type: LowRankUpdate
RFF Frobenius error (D=500): 24.3464
RFF relative error:              0.1189

Convergence with increasing feature dimension

As DrffD_{\mathrm{rff}} grows the approximation improves at O(1/D)O(1 / \sqrt{D}) rate.

  D_rff =   100  |  relative error = 0.3016
  D_rff =   500  |  relative error = 0.0947
  D_rff =  1000  |  relative error = 0.0488
<Figure size 1000x400 with 1 Axes>

5. Kernel Centering

Centering a kernel matrix is required for kernel PCA and many kernel methods. The centered kernel is K~=HKH\tilde{K} = H K H where H=I−1n11⊤H = I - \frac{1}{n}\mathbf{1}\mathbf{1}^\top is the centering matrix.

Centering operator type: LowRankUpdate
H shape: (500, 500)
H matches manual: True

Center the kernel matrix

Centered kernel type: MatrixLinearOperator
Matches manual HKH:  True
Centered row sums ~ 0: 8.26e-14

6. HSIC: Independence Testing

The Hilbert-Schmidt Independence Criterion measures dependence between two random variables via their kernel embeddings:

HSIC(F,G)=1n2tr⁡(KfHKgH)\mathrm{HSIC}(F, G) = \frac{1}{n^2} \operatorname{tr}(K_f H K_g H)

HSIC =0= 0 if and only if FF and GG are independent (in the population, with characteristic kernels). We compare a correlated pair (x,y=x+ϵ)(x, y = x + \epsilon) with an independent pair (x,z)(x, z).

HSIC (x, y_correlated):  0.055927   (should be > 0)
HSIC (x, z_independent): 0.002026   (should be ~ 0)

7. MMD: Two-Sample Testing

The Maximum Mean Discrepancy measures the distance between two distributions in a reproducing kernel Hilbert space:

MMD2(P,Q)=E[k(x,x′)]+E[k(y,y′)]−2 E[k(x,y)]\mathrm{MMD}^2(P, Q) = \mathbb{E}[k(x,x')] + \mathbb{E}[k(y,y')] - 2\,\mathbb{E}[k(x,y)]

We compare P=N(0,1)P = \mathcal{N}(0, 1) vs Q=N(1,1)Q = \mathcal{N}(1, 1) (different) and PP vs PP (same).

MMD^2(P, Q) [different]: 0.148538   (should be > 0)
MMD^2(P, P) [same]:      0.004874   (should be ~ 0)

8. Plots: Approximation Quality

A side-by-side comparison of the kernel matrix approximations.

<Figure size 1200x350 with 3 Axes>

9. Summary

FunctionOutputCost
nystrom_operator(K_XZ, K_ZZ_op)LowRankUpdateO(NM2)O(NM^2)
rff_operator(X, omega, b)LowRankUpdateO(ND)O(ND)
center_kernel(K)MatrixLinearOperatorO(N2)O(N^2)
centering_operator(n)LowRankUpdateO(1)O(1)
hsic(K_f, K_q)scalarO(N2)O(N^2)
mmd_squared(K_xx, K_yy, K_xy)scalarO(N2)O(N^2)

Key takeaways:

  • Both Nystrom and RFF produce LowRankUpdate operators, so downstream solves and log-determinants exploit the Woodbury identity automatically.
  • center_kernel and centering_operator handle the centering boilerplate needed by kernel PCA and HSIC.
  • hsic and mmd_squared are one-line kernel statistics for independence and two-sample testing.