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.

Pathwise GP Posterior Sampling

This notebook demonstrates pyrox.gp.PathwiseSampler and pyrox.gp.DecoupledPathwiseSampler — callable posterior function draws via Matheron’s rule. Each sampled path is a single deterministic function that can be evaluated at arbitrary inputs without refactorizing a test-set covariance, which is the enabler for Thompson sampling, Bayesian optimization, and posterior visualization.

What you’ll learn:

  1. The Matheron-rule identity behind PathwiseSampler and why the RFF prior draw matters.
  2. Drawing and evaluating posterior function samples from an exact ConditionedGP.
  3. The sparse / decoupled variant over an SVGP-style inducing set.
  4. A minimal Thompson-sampling loop on a 1D toy objective.

Background

Matheron’s rule

For a GP prior f∼GP(μ,k)f \sim \mathrm{GP}(\mu, k) with Gaussian observations y=f(X)+εy = f(X) + \varepsilon, ε∼N(0,σ2I)\varepsilon \sim \mathcal{N}(0, \sigma^2 I), the posterior sample path admits the Matheron decomposition

f^post(⋅)=f~(⋅)⏟prior draw+k(⋅,X) (KXX+σ2I)−1 (y−f~(X)−ε~)⏟correction,\hat{f}_{\mathrm{post}}(\cdot) = \underbrace{\tilde f(\cdot)}_{\text{prior draw}} + \underbrace{k(\cdot, X)\,(K_{XX} + \sigma^2 I)^{-1}\,(y - \tilde f(X) - \tilde\varepsilon)}_{\text{correction}},

where f~\tilde f is drawn from the prior and ε~∼N(0,σ2I)\tilde\varepsilon \sim \mathcal{N}(0, \sigma^2 I) is an independent noise draw at the training inputs. The key insight: if the prior draw f~\tilde f is represented by a closed-form basis (e.g. random Fourier features), then f^post\hat f_{\mathrm{post}} is a single callable function — evaluate it at any x∗x_* in O(F+N)\mathcal{O}(F + N) without touching K∗∗K_{**}.

Random Fourier features for the prior draw

For a stationary kernel k(x,x′)=σ2ρ(∥x−x′∥/ℓ)k(x, x') = \sigma^2 \rho(\|x-x'\|/\ell), Bochner’s theorem gives a spectral density S(ω)S(\omega) such that

k(x,x′)=σ2∫S(ω) cos⁡(ω⊤(x−x′)) dω.k(x, x') = \sigma^2 \int S(\omega)\,\cos(\omega^\top(x - x'))\,d\omega.

Sampling FF frequencies ωj∼S\omega_j \sim S and phases bj∼Unif(0,2π)b_j \sim \mathrm{Unif}(0, 2\pi),

f~(x)=∑j=1Fwj 2σ2/F cos⁡(ωj⊤x/ℓ+bj),wj∼N(0,1),\tilde f(x) = \sum_{j=1}^F w_j\,\sqrt{2\sigma^2/F}\,\cos(\omega_j^\top x / \ell + b_j), \qquad w_j \sim \mathcal{N}(0, 1),

defines a function whose covariance converges to kk as F→∞F \to \infty. pyrox._basis.draw_rff_cosine_basis is the pure shared primitive that implements this for RBF and Matern kernels; the pathwise samplers call into it.

Sparse / decoupled form

For a sparse SparseGPPrior + variational guide q(u)=N(m,S)q(u) = \mathcal{N}(m, S) over inducing values u=f(Z)u = f(Z), the decoupled Matheron sample is

f^post(x∗)=f~(x∗)+k(x∗,Z) KZZ−1 (u(s)−f~(Z)),u(s)∼q(u).\hat f_{\mathrm{post}}(x_*) = \tilde f(x_*) + k(x_*, Z)\,K_{ZZ}^{-1}\,(u^{(s)} - \tilde f(Z)),\qquad u^{(s)} \sim q(u).

DecoupledPathwiseSampler handles WhitenedGuide automatically by unwhitening v∼q(v)v \sim q(v) into u=LZZvu = L_{ZZ} v before forming the correction.

Setup

Toy dataset

Exact pathwise samples

Fit a plain ConditionedGP, then draw 32 callable posterior paths and evaluate them on the dense test grid.

<Figure size 1200x500 with 1 Axes>

The blue paths are independent callable function draws. Reevaluating the same path on a different input batch gives numerically identical values — a property the analytic predictive moments alone cannot provide without refactorizing a joint test-set covariance.

Sanity check: empirical moments from 512 draws match the analytic posterior.

<Figure size 1400x400 with 2 Axes>

Sparse / decoupled pathwise samples

Same story with a sparse GP: a point-inducing SparseGPPrior, a variational guide q(u)=N(m,LL⊤)q(u) = \mathcal{N}(m, L L^\top), and the decoupled Matheron update. The prior draw reuses the RFF basis; the correction is computed in the inducing-point basis, so the resulting function is callable at arbitrary inputs after a one-time inducing solve.

To give the sampler a realistic guide — instead of a hand-picked mean/covariance with no relation to the data — we set q(u)q(u) to the exact conditional p(u∣y)p(u \mid y) of the inducing values given the training data. This is the closed-form Bayes-optimal Gaussian over uu, and it’s what a fully-converged SVGP guide would match. The resulting decoupled paths should reproduce the exact GP posterior samples up to the RFF + inducing-set approximation.

<Figure size 1200x500 with 1 Axes>

Thompson sampling on a 1D toy

The canonical pathwise-sampling use case. At each round:

  1. Draw a single posterior path from the current ConditionedGP.
  2. Pick the argmin of that path (Thompson’s “sample and act” rule for minimization).
  3. Observe the true objective plus noise and condition the GP on the augmented dataset.

Because a sampled path is a single callable function, step 2 is a trivial grid / argmin evaluation — no repeated full-covariance predictives.

<Figure size 1800x800 with 8 Axes>
Best observed value: -0.484
Argmin (true):       x* = -3.000
Argmin (observed):   x  = 0.910

Takeaways

  • A pathwise sample is a callable function, not a vector of values — reusable across arbitrary test sets.
  • PathwiseSampler uses the cached noisy operator from ConditionedGP, so the training Cholesky is shared with the standard predictive path.
  • DecoupledPathwiseSampler composes with any of the FullRankGuide, MeanFieldGuide, WhitenedGuide, NaturalGuide, DeltaGuide guides; whitened guides are unwhitened automatically.
  • The shared RFF prior draw lives in pyrox._basis.draw_rff_cosine_basis + evaluate_rff_cosine_paths — the same primitive other posterior-function workflows (custom BO acquisitions, ensembled spectral estimators) can build on directly.