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.

GP Regression with NumPyro + gaussx

This notebook shows how to use gaussx.MultivariateNormal inside a NumPyro model for full Bayesian Gaussian process regression. Because gaussx distributions accept lineax operators as covariances, we get structured linear algebra (Cholesky, Woodbury, etc.) for free while using NumPyro’s MCMC and predictive machinery.

What you’ll learn:

  1. Defining a GP model with gaussx.MultivariateNormal
  2. Inferring kernel hyperparameters via NUTS
  3. Posterior predictive sampling with numpyro.infer.Predictive
  4. Comparing against the analytic GP posterior

Background

In standard GP regression, hyperparameters θ (kernel variance, lengthscale, noise) are set by maximizing the log-marginal likelihood -- a point estimate (type-II ML). Full Bayesian treatment places priors on θ and integrates them out:

p(f∗∣y)=∫p(f∗∣y,θ) p(θ∣y) dθp(f_* | y) = \int p(f_* | y, \theta)\, p(\theta | y)\, d\theta

This integral is intractable, but MCMC (here NUTS; Hoffman & Gelman, 2014) samples from p(θ∣y)p(\theta | y) and averages the GP predictive over those samples. The result is better-calibrated uncertainty that accounts for hyperparameter uncertainty -- important when data is scarce or hyperparameters are poorly identified.

Setup

Generate data

We sample from a smooth function with additive Gaussian noise.

Training points: 30
Test points:     200

Define the NumPyro model

We place log-normal priors on the RBF kernel hyperparameters (variance, lengthscale) and a half-normal prior on the observation noise. The likelihood uses gaussx.MultivariateNormal with a lineax PSD operator.

Run NUTS

We use NumPyro’s NUTS sampler to infer the kernel hyperparameters. The gaussx.MultivariateNormal.log_prob is fully differentiable via JAX, so NUTS can compute gradients through the structured linear algebra.


                     mean       std    median      5.0%     95.0%     n_eff     r_hat
  kernel_length      0.74      0.23      0.70      0.38      1.07    130.19      1.00
     kernel_var      0.75      0.83      0.47      0.12      1.59     83.70      1.01
          noise      0.20      0.04      0.19      0.14      0.26    288.93      1.00

Number of divergences: 0
Posterior shapes:
  kernel_length: (500,)
  kernel_var: (500,)
  noise: (500,)

Posterior hyperparameter distributions

The MCMC samples give us full posteriors over the kernel hyperparameters. Let’s visualize them.

<Figure size 1400x350 with 3 Axes>

Posterior predictive

For each posterior sample of hyperparameters, we compute the analytic GP predictive mean and variance at test locations.

The total predictive variance decomposes via the law of total variance:

Var⁡[y∗∣D]=Eθ[Var⁡[y∗∣θ,D]]⏟aleatoric+Var⁡θ[E[y∗∣θ,D]]⏟hyper. unc.\operatorname{Var}[y_* | \mathcal{D}] = \underbrace{\mathbb{E}_\theta[\operatorname{Var} [y_* | \theta, \mathcal{D}]]}_{\text{aleatoric}} + \underbrace{\operatorname{Var}_\theta[\mathbb{E} [y_* | \theta, \mathcal{D}]]}_{\text{hyper. unc.}}
Predictive mean shape: (200,)
Total std shape:       (200,)

Plot predictions

The shaded bands show ±2σ\pm 2\sigma total predictive uncertainty, combining both hyperparameter uncertainty (from MCMC) and observation noise.

<Figure size 1200x500 with 1 Axes>

Summary

  • gaussx.MultivariateNormal plugs directly into NumPyro models, enabling NUTS inference over GP hyperparameters.
  • The distribution’s log_prob uses gaussx structural dispatch (Cholesky for PSD operators), and is fully differentiable for gradient-based sampling.
  • Posterior predictive predictions combine hyperparameter uncertainty (from MCMC) with observation noise for calibrated uncertainty estimates.
  • The same pattern works with structured covariances (Kronecker, BlockDiag, LowRankUpdate) for scalable GP models.

References

  • Hoffman, M. D. & Gelman, A. (2014). The No-U-Turn Sampler. JMLR, 15, 1593--1623.
  • Rasmussen, C. E. & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press. (Section 5.2 on Bayesian hyperparameter treatment)
  • Phan, D., Pradhan, N., & Jankowiak, M. (2019). Composable effects for flexible and accelerated probabilistic programming in NumPyro. arXiv:1912.11554.