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.

Sugar Operations for Gaussians and GPs

gaussx provides “sugar” functions that combine primitives (solve, logdet, trace, etc.) into compound operations common in Gaussian and GP workflows.

What you’ll learn:

  1. quadratic_form and gaussian_log_prob for evaluating distributions
  2. gaussian_entropy and kl_standard_normal for information-theoretic quantities
  3. log_marginal_likelihood for GP model selection
  4. schur_complement and cov_transform for conditional distributions
  5. whiten_covariance and unwhiten for reparameterization

These compound operations appear repeatedly in GP inference (Rasmussen & Williams, 2006), variational inference (Blei et al., 2017), and Bayesian neural networks. gaussx sugar functions combine primitives into numerically stable implementations — for example, log_marginal_likelihood avoids separate computation of the quadratic form and logdet, which can be more efficient with certain solver strategies.

1. Setup

We build a small PSD kernel matrix from an RBF kernel on 20 points and wrap it as a lineax operator.

Sigma shape: (20, 20)

2. Quadratic forms

quadratic_form(Sigma_op, x) computes x⊤Σ−1xx^\top \Sigma^{-1} x via a single solve -- the squared Mahalanobis distance.

quadratic_form: 7358893.914392903
manual v^T Sigma^{-1} v: 7358893.914392903
match: True

3. Log-probability

gaussian_log_prob(mu, Sigma_op, x) evaluates the multivariate normal log-pdf:

log⁡N(x∣μ,Σ)=−12(Nlog⁡2π+log⁡∣Σ∣+(x−μ)⊤Σ−1(x−μ))\log \mathcal{N}(x \mid \mu, \Sigma) = -\tfrac{1}{2}\bigl(N \log 2\pi + \log|\Sigma| + (x - \mu)^\top \Sigma^{-1}(x - \mu)\bigr)
gaussian_log_prob: -3679390.0370853157
manual log-prob:   -3679390.0370851513
match: True

It also works with structured operators. Here we use a Kronecker product to build a 2D covariance:

log-prob (Kronecker): -9.65689134217588

4. Entropy and KL divergence

Entropy of N(μ,Σ)\mathcal{N}(\mu, \Sigma):

H=12(N(1+log⁡2π)+log⁡∣Σ∣)H = \tfrac{1}{2}\bigl(N(1 + \log 2\pi) + \log|\Sigma|\bigr)

KL to the standard normal KL(N(m,S)∥N(0,I))\text{KL}(\mathcal{N}(m, S) \| \mathcal{N}(0, I)):

KL=12(tr(S)+m⊤m−N−log⁡∣S∣)\text{KL} = \tfrac{1}{2}\bigl(\text{tr}(S) + m^\top m - N - \log|S|\bigr)
entropy: -46.920111612965044
KL(q || p): 75.39889196437495
manual KL:  75.39889196437495
match: True

5. GP log-marginal likelihood

For GP regression with observations yy, prior mean μ, and covariance Ky=K+σ2IK_y = K + \sigma^2 I:

log⁡p(y)=−12((y−μ)⊤Ky−1(y−μ)+log⁡∣Ky∣+Nlog⁡2π)\log p(y) = -\tfrac{1}{2}\bigl((y-\mu)^\top K_y^{-1}(y-\mu) + \log|K_y| + N\log 2\pi\bigr)
log marginal likelihood: -11.398561367574223

Differentiating the LML with jax.grad for hyperparameter optimization:

grad w.r.t. log-lengthscale: -2.887548675545294
grad w.r.t. log-noise-var:   1.5543508491525124

6. Schur complement

For GP conditional distributions, the Schur complement gives the conditional covariance:

KX∣Z=KXX−KXZ KZZ−1 KZXK_{X|Z} = K_{XX} - K_{XZ}\, K_{ZZ}^{-1}\, K_{ZX}

schur_complement returns a LowRankUpdate operator that preserves the low-rank structure.

The Schur complement is the key identity behind GP conditional distributions. Given the joint $\begin{pmatrix} f_X \ f_Z \end{pmatrix} \sim \mathcal{N}!\left(0,

(KXXKXZKZXKZZ)\begin{pmatrix} K_{XX} & K_{XZ} \\ K_{ZX} & K_{ZZ} \end{pmatrix}

the conditional fX∣fZf_X \mid f_Z has covariance KXX−KXZ KZZ−1 KZXK_{XX} - K_{XZ}\, K_{ZZ}^{-1}\, K_{ZX}. When M≪NM \ll N, this is a rank-MM correction — exactly a LowRankUpdate.

type: LowRankUpdate
max error: 4.295175326518574e-15

7. Covariance propagation

cov_transform(J, Sigma_op) propagates uncertainty through a linear map: Σ′=J Σ J⊤\Sigma' = J\,\Sigma\,J^\top.

max error: 0.0

8. Whitening and unwhitening

The reparameterization trick for Gaussians: sample z∼N(0,I)z \sim \mathcal{N}(0, I), then compute x=L zx = L\,z to get x∼N(0,Σ)x \sim \mathcal{N}(0, \Sigma) where Σ=LL⊤\Sigma = L L^\top.

whiten_covariance(L, S_tilde) computes L S~ L⊤L\,\tilde{S}\,L^\top, and unwhiten(m_tilde, L) computes L m~L\,\tilde{m}.

The reparameterization trick (Kingma & Welling, 2014) enables gradient-based optimization of variational objectives by expressing samples x=L z+μx = L\,z + \mu where z∼N(0,I)z \sim \mathcal{N}(0, I), making the sampling differentiable w.r.t. the distribution parameters μ and LL.

max error (covariance): 2.220446049250313e-16
max error (sample): 0.0
mean abs error (empirical cov): 0.009049423838171582

Summary

This notebook demonstrated the main gaussx sugar operations:

FunctionWhat it computes
quadratic_formx⊤Σ−1xx^\top \Sigma^{-1} x (Mahalanobis distance squared)
gaussian_log_probMultivariate normal log-pdf
gaussian_entropyH[N(μ,Σ)]H[\mathcal{N}(\mu, \Sigma)]
kl_standard_normalKL(N(m,S)∣N(0,I))\text{KL}(\mathcal{N}(m,S) | \mathcal{N}(0,I))
log_marginal_likelihoodGP log evidence (differentiable)
schur_complementKXX−KXZKZZ−1KZXK_{XX} - K_{XZ} K_{ZZ}^{-1} K_{ZX} as LowRankUpdate
cov_transformJΣJ⊤J \Sigma J^\top (uncertainty propagation)
whiten_covariance / unwhitenReparameterization trick

All operations dispatch on operator structure (diagonal, Kronecker, block-diagonal, low-rank) for efficient computation, and compose seamlessly with jax.jit, jax.vmap, and jax.grad.

References

  • Blei, D. M., Kucukelbir, A., & McAuliffe, J. D. (2017). Variational inference: a review for statisticians. JASA, 112(518), 859-877.
  • Kingma, D. P. & Welling, M. (2014). Auto-encoding variational Bayes. Proc. ICLR.
  • Rasmussen, C. E. & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press.