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 Derivatives — The Gradient Posterior

Open In Colab

A fitted GP emulator gives more than predictions: the gradient of a GP is itself a GP, so the posterior over ∇f(x∗)\nabla f(x_*) is Gaussian and exact. Its mean is the sensitivity map of Johnson et al. (2020); its covariance puts error bars on that map. This notebook uses ConditionedGP.predict_gradient, predict_var_gradient and predict_mean_hessian.

What you’ll learn:

  1. The gradient posterior and how it reuses the cached training solve.

  2. A 1-D posterior slope with a ±2σ\pm 2\sigma band, against the slope of the truth.

  3. A 2-D sensitivity map ∥E∇f∥\lVert \mathbb{E}\nabla f \rVert with its marginal uncertainty, and the ∇σ2\nabla \sigma^2 acquisition arrows.

Background

The gradient posterior

Take f∼GP(m,k)f \sim \mathcal{GP}(m, k), observations y=f(X)+εy = f(X) + \varepsilon, ε∼N(0,σ2I)\varepsilon \sim \mathcal{N}(0, \sigma^2 I), and write K^=K(X,X)+(jitter+σ2)I\hat K = K(X, X) + (\text{jitter} + \sigma^2) I and α=K^−1(y−m(X))\alpha = \hat K^{-1}(y - m(X)) (the solve GPPrior.condition caches). Differentiation is linear, so (∇f(x∗),y)(\nabla f(x_*), y) is jointly Gaussian with cross-covariance G(x∗)=∂1K(x∗,X)∈RD×NG(x_*) = \partial_1 K(x_*, X) \in \mathbb{R}^{D \times N}, Gdn=∂k(x∗,xn)/∂x∗,dG_{dn} = \partial k(x_*, x_n) / \partial x_{*,d} (Solak et al. 2003; Rasmussen & Williams 2006, §9.4). Conditioning gives

E[∇f(x∗)∣y]=∇m(x∗)+G(x∗) α,Cov⁡[∇f(x∗)∣y]=∇x∇x′⊤k(x∗,x∗)−G(x∗) K^−1G(x∗)⊤.\mathbb{E}[\nabla f(x_*) \mid y] = \nabla m(x_*) + G(x_*)\,\alpha, \qquad \operatorname{Cov}[\nabla f(x_*) \mid y] = \nabla_x \nabla_{x'}^\top k(x_*, x_*) - G(x_*)\,\hat K^{-1} G(x_*)^\top .

The mean equals ∇x∗μ∗(x∗)\nabla_{x_*} \mu_*(x_*), the derivative of the predictive mean that Johnson et al. (2020, §3) use for sensitivity analysis. With the cached Cholesky factor K^=LL⊤\hat K = L L^\top the downdate is V⊤VV^\top V with V=L−1G⊤V = L^{-1} G^\top: triangular solves only, no new factorisation.

The gradient of the predictive variance

From σ∗2(x)=k(x,x)−k(x,X) K^−1k(X,x)\sigma^2_*(x) = k(x, x) - k(x, X)\,\hat K^{-1} k(X, x),

∇xσ∗2(x)=[∂1k+∂2k](x,x)−2 G(x) K^−1k(X,x),\nabla_x \sigma^2_*(x) = \big[\partial_1 k + \partial_2 k\big](x, x) - 2\,G(x)\,\hat K^{-1} k(X, x),

whose first term vanishes for a stationary kernel. Ascending it moves a candidate towards where the model is least certain (uncertainty sampling).

Pseudocode (what predict_gradient does)

k    = freeze(kernel)                          # one hyperparameter draw per call
G    = [Derivative(k, dx=d)(X*, X) for d]      # (D*M, N), rows (d m)
mean = rearrange(G @ alpha, "(d m) -> m d") + grad m(X*)
V    = L^-1 G^T                                # cached factor; else K^-1 G^T via the solver
cov  = H(X*) - V^T V                           # per-point (M, D, D) blocks by default

References. Johnson, Laparra, Pérez-Suay, Mahecha & Camps-Valls (2020), Kernel methods and their derivatives: Concept and perspectives for the earth system sciences, PLOS ONE 15(10): e0235885, Johnson et al. (2020). Solak, Murray-Smith, Leithead, Leith & Rasmussen (2003), Derivative observations in Gaussian process models of dynamic systems, NeurIPS 15. Rasmussen & Williams (2006), Gaussian Processes for Machine Learning, §9.4.

Setup

Python implementation: CPython
Python version       : 3.13.5
IPython version      : 9.13.0

jax       : 0.10.2
equinox   : 0.13.8
numpyro   : 0.21.0
gaussx    : 0.6.5
kernellib : 0.0.17
pyrox     : 0.1.1
matplotlib: 3.10.9

Compiler    : GCC 11.2.0
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

1-D: the posterior slope

Fit an RBF GP to noisy draws of f(x)=sin⁡(3x)+0.3x2f(x) = \sin(3x) + 0.3x^2, then ask for the gradient posterior on a grid. cov="marginal" (the default) returns one D×DD \times D block per point, here 1×11 \times 1.

mean (300, 1) cov (300, 1, 1)
<Figure size 1800x500 with 2 Axes>

The band widens outside the data, where the slope is unconstrained, and tightens between the points. Within the data the true slope stays inside the ±2σ\pm 2\sigma band.

2-D: a sensitivity map with uncertainty

On a 2-D input the gradient mean is a vector field; its norm ∥E∇f∥\lVert \mathbb{E}\nabla f \rVert is the sensitivity map, and the trace of the marginal covariance gives a scalar uncertainty for it. The input x2x_2 matters only through a weak trend, so the map should be dominated by x1x_1.

<Figure size 1800x500 with 6 Axes>

The sensitivity map follows ∣2.5cos⁡(2.5x1)∣|2.5\cos(2.5 x_1)| inside the data and its uncertainty grows outside it. The ∇σ2\nabla\sigma^2 arrows point away from the training inputs, towards the gaps an active-learning loop should sample next.

Checks

The gradient mean is exactly the derivative of the predictive mean, and the mean Hessian is the Jacobian of the gradient mean.

max |E grad f - grad mu|      : 7.915890165577366e-14
max |hessian - jac(grad mean)|: 8.049116928532385e-14

Summary

  • predict_gradient(X_star) returns a GradientPosterior: the gradient mean (M, D) and its per-point covariance (M, D, D); cov="full" gives the joint (M, D, M, D) and cov="none" the mean only.

  • predict_var_gradient is the acquisition gradient for uncertainty sampling; predict_mean_hessian the curvature of the predictive mean.

  • All three reuse the cached training solve and work for matrix-free priors through the prior’s solver. The kernel must be mean-square differentiable (Matern(nu=0.5) raises).

References
  1. Johnson, J. E., Laparra, V., Pérez-Suay, A., Mahecha, M. D., & Camps-Valls, G. (2020). Kernel methods and their derivatives: Concept and perspectives for the earth system sciences. PLOS ONE, 15(10), e0235885. 10.1371/journal.pone.0235885