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 Ridge Regression with gaussx

A minimal kernel regression example showing how gaussx handles the core linear algebra: solving (K+σ2I)α=y(K + \sigma^2 I) \alpha = y and computing the log-marginal likelihood.

This is the computational backbone of Gaussian process regression.

Background

Gaussian process (GP) regression is a Bayesian nonparametric method for function estimation. The key idea is to place a GP prior over the space of functions, observe noisy evaluations of the function at a finite set of inputs, and then condition on the data to obtain a posterior process. Because the GP prior is conjugate with Gaussian observation noise, the posterior is again a GP and can be computed in closed form.

Given training inputs XX with observations yy and test inputs X∗X_*, the posterior predictive distribution is

μ∗=K∗f(Kff+σ2I)−1y\mu_* = K_{*f}(K_{ff} + \sigma^2 I)^{-1} y
Σ∗=K∗∗−K∗f(Kff+σ2I)−1Kf∗\Sigma_* = K_{**} - K_{*f}(K_{ff} + \sigma^2 I)^{-1} K_{f*}

where KffK_{ff} is the kernel matrix evaluated at training points, K∗fK_{*f} is the cross-covariance between test and training points, K∗∗K_{**} is the kernel at test points, and σ2\sigma^2 is the observation noise variance.

The computational bottleneck is solving the kernel system (Kff+σ2I)α=y(K_{ff} + \sigma^2 I) \alpha = y and computing the log-determinant log⁡∣Kff+σ2I∣\log|K_{ff} + \sigma^2 I|, both of which appear in the log-marginal likelihood. For nn training points, naive dense approaches cost O(n3)O(n^3); structured kernels and iterative solvers can reduce this substantially. See Rasmussen & Williams (2006), Ch. 2 for a thorough treatment.

Generate data

A simple 1D regression problem: noisy samples from a smooth function.

<Figure size 1000x400 with 1 Axes>

Build the kernel matrix

We use a squared-exponential (RBF) kernel:

k(x,x′)=σf2exp⁡(−(x−x′)22ℓ2)k(x, x') = \sigma_f^2 \exp\left(-\frac{(x - x')^2}{2 \ell^2}\right)

Solve with gaussx

The key computation: (K+σ2I)α=y(K + \sigma^2 I) \alpha = y.

We wrap the kernel matrix as a lineax operator and use gaussx.solve which dispatches to the appropriate solver.

alpha shape: (50,)
prediction shape: (200,)

Log-marginal likelihood

log⁡p(y∣X,θ)=−12y⊤(K+σ2I)−1y−12log⁡∣K+σ2I∣−n2log⁡2π\log p(y | X, \theta) = -\frac{1}{2} y^\top (K + \sigma^2 I)^{-1} y - \frac{1}{2} \log |K + \sigma^2 I| - \frac{n}{2} \log 2\pi
Log-marginal likelihood: -18.2919

Optimizing hyperparameters with grad

gaussx primitives are differentiable. We can optimize hyperparameters via gradient descent on the negative log-marginal likelihood.

The analytic gradient of the log-marginal likelihood with respect to a hyperparameter θj\theta_j is

∂∂θjlog⁡p(y∣X,θ)=12α⊤∂K∂θjα−12tr⁡ ⁣(K−1∂K∂θj)\frac{\partial}{\partial \theta_j} \log p(y|X,\theta) = \frac{1}{2} \alpha^\top \frac{\partial K}{\partial \theta_j} \alpha - \frac{1}{2} \operatorname{tr}\!\left(K^{-1} \frac{\partial K}{\partial \theta_j}\right)

where α=Ky−1y\alpha = K_y^{-1} y and Ky=K+σ2IK_y = K + \sigma^2 I. The first term rewards data fit and the second penalizes model complexity (Rasmussen & Williams, 2006, Eq. 5.9).

Rather than implementing this expression manually, gaussx + JAX computes the gradient via automatic differentiation through solve and logdet. This is both simpler and less error-prone.

Gradient of neg-LML: [36.22926626 -2.36412712 -6.9243587 ]

Optimized: lengthscale=0.388, variance=0.177, noise=0.0522

Prediction with optimized hyperparameters

Mean prediction range: [-0.72, 0.90]
Mean std range: [0.0855, 0.3984]
<Figure size 1000x400 with 1 Axes>

Summary

This example showed how gaussx provides the core linear algebra for GP regression:

  1. gaussx.solve — solve the kernel system for weights
  2. gaussx.logdet — log-determinant for the marginal likelihood
  3. jax.grad — differentiate through both to optimize hyperparameters

For large-scale problems, the same code works with structured kernels (Kronecker for grid data, low-rank for inducing points) — gaussx automatically dispatches to the efficient algorithm.

References

  • Rasmussen, C. E. & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press.
  • Williams, C. K. I. & Rasmussen, C. E. (1996). Gaussian processes for regression. Proc. NeurIPS 8.