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.

Random Fourier Features → SSGP → VSSGP — Three Spectral Views of a Gaussian Process

Open In Colab

This notebook is the GP-flavored companion to the RFF as Neural Networks notebook. Where that notebook treats RFF as a neural network architecture (with frozen, learned, or ensembled weights), here we follow the Bayesian progression that links the same feature map to Gaussian processes:

  1. Learned RFF (MAP baseline) — the deterministic regression model with trainable Ω. No uncertainty over the parameters; just a regularised MSE point estimate.
  2. SSGP — Sparse Spectrum GP (Lázaro-Gredilla et al. JMLR 2010) — analytically marginalise the head β and train Ω on the GP marginal likelihood. Closed-form predictive variance for free.
  3. VSSGP — Variational SSGP (Gal & Turner ICML 2015) — put a variational posterior q(Ω)q(\Omega) on top of q(β)q(\beta), train via reparameterised ELBO. Full posterior uncertainty over both frequencies and weights.

The three are different inferential commitments to the same model class — a linear combination of MM random Fourier features:

yi=Φ(xi;Ω) β+εi,Φ(x;Ω)=1M[cos⁡(Ω⊤x),sin⁡(Ω⊤x)]∈R2M,εi∼N(0,σn2).y_i = \Phi(x_i; \Omega)\,\beta + \varepsilon_i, \qquad \Phi(x; \Omega) = \sqrt{\tfrac{1}{M}}\bigl[\cos(\Omega^\top x), \sin(\Omega^\top x)\bigr] \in \mathbb{R}^{2M}, \qquad \varepsilon_i \sim \mathcal{N}(0, \sigma_n^2).
MethodΩβObjectivePredictive variance?
Learned RFF (MAP)trained (point)trained (point) + L2regularised MSEno
SSGPtrained (point)marginalised analyticallylog marginal likelihoodyes — closed form
VSSGPvariational q(Ω)q(\Omega)variational q(β)q(\beta)tempered ELBOyes — MC over the posterior

Foundation. The Bochner / Rahimi-Recht derivation of the feature map, the kernel-approximation convergence rate, and the equivalence between paired [cos⁡,sin⁡][\cos, \sin] and phased cos⁡(Ω⊤x+b)\cos(\Omega^\top x + b) readouts across RBF / Matérn / Laplace are covered separately in the Kernel Approximation notebook. This notebook treats that map as a black box and focuses on the Bayesian inference layer.

Forward-pointers: the fixed-Ω version (Rahimi-Recht 2007) and the ensemble-of-MAP alternative live in the RFF as Neural Networks notebook. The deep versions are in the Deep Random Feature Expansions notebook.

Background — from features to GPs

A linear combination of MM random Fourier features with Gaussian weights

f(x)=β⊤ϕ(x),β∼N ⁣(0, σβ2 I),ϕ(x)=1M[cos⁡(Ω⊤x),sin⁡(Ω⊤x)]f(x) = \beta^\top \phi(x), \qquad \beta \sim \mathcal{N}\!\bigl(0,\, \sigma_\beta^2\,I\bigr), \qquad \phi(x) = \sqrt{\tfrac{1}{M}}\bigl[\cos(\Omega^\top x), \sin(\Omega^\top x)\bigr]

is itself a Gaussian process with kernel

Cov(f(x),f(y))=σβ2 ϕ(x)⊤ϕ(y)=σβ2 k^(x,y)  →M→∞  σβ2 k(x,y).\mathrm{Cov}(f(x), f(y)) = \sigma_\beta^2\,\phi(x)^\top \phi(y) = \sigma_\beta^2\,\hat{k}(x, y) \;\xrightarrow[M \to \infty]{}\; \sigma_\beta^2\,k(x, y).

This is the Sparse Spectrum GP (SSGP). The marginal likelihood and predictive distribution have closed forms — derived in §3. Per-iteration cost is O(NM2+M3)\mathcal{O}(NM^2 + M^3), linear in the dataset size for fixed MM, the headline win over the O(N3)\mathcal{O}(N^3) exact GP. (For the spectral-density derivation that justifies k^→k\hat k \to k — and the empirical convergence figures across RBF / Matérn / Laplace — see the Kernel Approximation notebook.)

Setup

Detect Colab and install pyrox[colab] (which pulls in matplotlib and watermark) only when running there.

Python implementation: CPython
Python version       : 3.13.5
IPython version      : 9.10.0

jax       : 0.9.2
equinox   : 0.13.6
numpyro   : 0.20.1
pyrox     : 0.0.7
matplotlib: 3.10.8

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

Shared regression setup

All three regression methods (§2 learned RFF, §3 SSGP, §4 VSSGP) are evaluated on the same target with a held-out gap, so predictive uncertainty in the gap becomes the visual axis of comparison:

yi=sin⁡(3πxi)+0.05 εi,εi∼N(0,1),xi∈[−1,−0.2]∪[0.4,1].y_i = \sin(3\pi x_i) + 0.05\,\varepsilon_i, \qquad \varepsilon_i \sim \mathcal{N}(0, 1), \qquad x_i \in [-1, -0.2] \cup [0.4, 1].

The target frequency is ω⋆=3π≈9.42\omega^\star = 3\pi \approx 9.42. We use a lengthscale prior ℓ=0.3\ell = 0.3 throughout, giving prior frequency standard deviation 1/ℓ≈3.31/\ell \approx 3.3 — comfortable coverage of ω⋆\omega^\star. All three methods use M=64M = 64 Fourier features.

target frequency: ω⋆ = 3π ≈ 9.42
prior bandwidth at ℓ=0.3: 1/ℓ ≈ 3.33
observation count: N = 56, gap removed from x ∈ (-0.2, 0.4)

2. Learned RFF — the MAP baseline

Math. The simplest regression model in the spectral family. Fix nothing as random; treat Ω, ℓ\ell, β, and an intercept bb as PyTree leaves and minimise the regularised MSE

L(Ω,ℓ,β)=12σn2 ∥y−Φ(X;Ω,ℓ) β−b∥2+M2σβ2 ∥β∥2.\mathcal{L}(\Omega, \ell, \beta) = \tfrac{1}{2\sigma_n^2}\,\bigl\|y - \Phi(X; \Omega, \ell)\,\beta - b\bigr\|^2 + \tfrac{M}{2\sigma_\beta^2}\,\|\beta\|^2.

This is the MAP point estimate of the SSGP model below — the same likelihood, but with the head β collapsed to a single value rather than a posterior. Equivalent to “neural-network RFF” in the companion notebook.

Wide initialisation. The activation cos⁡(Ω⊤x/ℓ)\cos(\Omega^\top x / \ell) has gradient −(x/ℓ)sin⁡(Ω⊤x/ℓ)-(x/\ell)\sin(\Omega^\top x / \ell), which is zero at Ω=0\Omega = 0. Initialising Ω∼N(0,1)\Omega \sim \mathcal{N}(0, 1) traps gradient descent at this saddle for high-frequency targets. We initialise Ω∼N(0,σ2)\Omega \sim \mathcal{N}(0, \sigma^2) with σ=5\sigma = 5 (wide enough that Ω⊤x\Omega^\top x already spans a non-trivial phase range, narrow enough that the prior at ℓ=0.3\ell = 0.3 still concentrates near the target).

What MAP cannot give you. A point estimate. No uncertainty. The fit through the held-out gap is whatever the optimiser settles on, with no signal that the model “doesn’t know” there. That gap-uncertainty is precisely what SSGP and VSSGP add.

learned RFF MAP: final loss = 0.0043, learned ℓ = 0.313, MSE = 0.1320
<Figure size 1100x450 with 1 Axes>

Clean fit at the data, but the curve through the held-out gap is just whatever the optimiser settled on — there is no way to read off “the model is uncertain here.” Compare this to the next two sections, where the uncertainty band visibly opens across the gap.

3. SSGP — Sparse Spectrum GP (Lázaro-Gredilla et al. 2010)

Math. Same model as §2 but treat the head β as latent with prior β∼N(0,σβ2I)\beta \sim \mathcal{N}(0, \sigma_\beta^2 I) and integrate it out. The marginal distribution of yy is Gaussian:

p(y∣X,Ω,ℓ,σβ,σn)=N ⁣(y ∣ 0,  KM+σn2I),KM=σβ2 ΦΦ⊤.p(y \mid X, \Omega, \ell, \sigma_\beta, \sigma_n) = \mathcal{N}\!\bigl(y \,\big|\, 0,\; K_M + \sigma_n^2 I\bigr), \qquad K_M = \sigma_\beta^2\,\Phi\Phi^\top.

This is the GP marginal likelihood with a degenerate rank-2M2M kernel. Maximise it with respect to (Ω,ℓ,σβ,σn)(\Omega, \ell, \sigma_\beta, \sigma_n).

Numerically stable form via the matrix-inversion lemma. Naively inverting KM+σn2IK_M + \sigma_n^2 I is O(N3)\mathcal{O}(N^3); using rank(KM)=2M<N\mathrm{rank}(K_M) = 2M < N collapses it to O(NM2+M3)\mathcal{O}(NM^2 + M^3). Define B=σn2I2M+σβ2 Φ⊤ΦB = \sigma_n^2 I_{2M} + \sigma_\beta^2\,\Phi^\top \Phi, then

y⊤(KM+σn2I)−1y=1σn2∥y∥2−σβ2σn2 (Φ⊤y)⊤B−1(Φ⊤y),y^\top (K_M + \sigma_n^2 I)^{-1} y = \tfrac{1}{\sigma_n^2}\|y\|^2 - \tfrac{\sigma_\beta^2}{\sigma_n^2}\,(\Phi^\top y)^\top B^{-1} (\Phi^\top y),
log⁡ ⁣∣KM+σn2I∣=(N−2M)log⁡σn2+log⁡∣B∣.\log\!\bigl|K_M + \sigma_n^2 I\bigr| = (N - 2M)\log\sigma_n^2 + \log|B|.

Combining,

log⁡p(y)=−12σn2∥y∥2+σβ22σn2∥L−1Φ⊤y∥2−N−2M2log⁡σn2−∑ilog⁡Lii−N2log⁡(2π),\log p(y) = -\tfrac{1}{2\sigma_n^2}\|y\|^2 + \tfrac{\sigma_\beta^2}{2\sigma_n^2}\|L^{-1}\Phi^\top y\|^2 - \tfrac{N - 2M}{2}\log\sigma_n^2 - \sum_i \log L_{ii} - \tfrac{N}{2}\log(2\pi),

where LL⊤=BL L^\top = B is the Cholesky factor.

Predictive distribution. The closed-form posterior on β is β∣y∼N(μβ,Σβ)\beta \mid y \sim \mathcal{N}(\mu_\beta, \Sigma_\beta) with μβ=σβ2 B−1Φ⊤y\mu_\beta = \sigma_\beta^2\,B^{-1}\Phi^\top y and Σβ=σβ2 σn2 B−1\Sigma_\beta = \sigma_\beta^2\,\sigma_n^2\,B^{-1}. The predictive distribution at a test point x⋆x_\star is

p(y⋆∣x⋆,y)=N ⁣(ϕ(x⋆)⊤μβ,  σn2+σβ2σn2 ϕ(x⋆)⊤B−1ϕ(x⋆)).p(y_\star \mid x_\star, y) = \mathcal{N}\!\bigl(\phi(x_\star)^\top \mu_\beta,\; \sigma_n^2 + \sigma_\beta^2 \sigma_n^2\,\phi(x_\star)^\top B^{-1} \phi(x_\star)\bigr).

Why this beats MAP. The marginal likelihood enforces an automatic Occam’s razor through the −12log⁡∣B∣-\tfrac{1}{2}\log|B| term — placing extra features near unnecessary frequencies inflates ∣B∣|B| and is penalised. And the closed-form predictive variance gives uncertainty for free without any MC sampling.

SSGP: final neg-log-ML = -60.13
      learned ℓ = 0.3881, σ_n = 0.0464, σ_β = 0.5022
      predictive MSE = 0.0809

Plot the predictive mean and ±2σ\pm 2\sigma band.

<Figure size 1100x450 with 1 Axes>

Two important things just happened.

  1. Hyperparameters fitted themselves. ML-II picked σn\sigma_n close to the true noise standard deviation 0.05, and ℓ\ell close to a value that puts prior support over the target frequency — no manual tuning. This is the core advantage of the marginal-likelihood objective over the MAP MSE objective: the noise level becomes a learned hyperparameter.
  2. Predictive variance opens across the gap. No ensembling, no Monte Carlo sampling — the band is a pure function of ϕ(x⋆)⊤B−1ϕ(x⋆)\phi(x_\star)^\top B^{-1} \phi(x_\star). In the gap, x⋆x_\star is far from the training data in feature space, so B−1B^{-1} inflates the variance. Outside ∣x∣>1|x| > 1 (extrapolation) the band continues to widen, mirroring exact-GP behaviour.

4. VSSGP — Variational SSGP (Gal & Turner 2015)

Math. SSGP fixes Ω as a point estimate. VSSGP makes Ω itself a latent random variable with a prior p(Ω)=N(0,I)p(\Omega) = \mathcal{N}(0, I) (in lengthscale-1 units; the spectral density of the RBF kernel) and a learnable mean-field posterior qϕ(Ω)=N(μΩ,diag(σΩ2))q_\phi(\Omega) = \mathcal{N}(\mu_\Omega, \mathrm{diag}(\sigma_\Omega^2)). The head β is similarly variational: qϕ(β)=N(μβ,diag(σβ2))q_\phi(\beta) = \mathcal{N}(\mu_\beta, \mathrm{diag}(\sigma_\beta^2)).

Tempered ELBO. With both Ω and β variational,

Lβ(ϕ)=Eqϕ(Ω)qϕ(β) ⁣[log⁡p(y∣X,Ω,β)]−β (KL ⁣[qϕ(Ω) ∥ p(Ω)]+KL ⁣[qϕ(β) ∥ p(β)]).\mathcal{L}_\beta(\phi) = \mathbb{E}_{q_\phi(\Omega) q_\phi(\beta)}\!\bigl[\log p(y \mid X, \Omega, \beta)\bigr] - \beta\,\Bigl(\mathrm{KL}\!\bigl[q_\phi(\Omega) \,\Vert\, p(\Omega)\bigr] + \mathrm{KL}\!\bigl[q_\phi(\beta) \,\Vert\, p(\beta)\bigr]\Bigr).

Both KLs have closed forms (Gaussian-Gaussian). The data term is estimated by reparameterising Ω=μΩ+σΩ⊙εΩ\Omega = \mu_\Omega + \sigma_\Omega \odot \varepsilon_\Omega and β=μβ+σβ⊙εβ\beta = \mu_\beta + \sigma_\beta \odot \varepsilon_\beta with ε∼N(0,I)\varepsilon \sim \mathcal{N}(0, I), and Monte-Carlo’ing over a few SS samples per step.

Why the temperature β∈(0,1]\beta \in (0, 1]? When the target’s spectral content lies far in the tail of p(Ω)p(\Omega), a strict β=1\beta = 1 ELBO can trap the posterior near the prior. Down-weighting the KL during training (β-VAE / KL-annealing) lets the posterior escape, after which we can ramp β→1\beta \to 1 if calibration matters. Here we use β=0.05\beta = 0.05 throughout for a clean comparison.

Predictive distribution. Sample SS realisations of (Ω,β)(\Omega, \beta) from qϕq_\phi, compute SS predictive means f(s)(x⋆)=ϕ(x⋆;Ω(s))⊤β(s)f^{(s)}(x_\star) = \phi(x_\star; \Omega^{(s)})^\top \beta^{(s)}, then take empirical mean and variance plus the observation noise. The MC predictive variance captures uncertainty in both the frequencies and the weights — a richer band than SSGP’s, especially when the true spectrum is broad.

VSSGP: final tempered-ELBO = 30.70, σ_n = 0.4011, MSE = 0.0163
<Figure size 1100x450 with 1 Axes>

Same target, similar fit quality, richer uncertainty: the grey member traces are independent draws from q(Ω)×q(β)q(\Omega) \times q(\beta), and they pull apart in the gap exactly like an exact-GP posterior would. The key conceptual addition over SSGP is that the band reflects frequency uncertainty too — if the target’s true frequency had been less well-determined by the data, the band’s behaviour in the gap would have been broader than SSGP’s.

Spectrum migration. Plot ∣ωj∣=∣μΩ,j∣/ℓ|\omega_j| = |\mu_{\Omega,j}|/\ell for SSGP (point estimate) vs VSSGP (posterior mean) to see how the trained frequencies relate to the target’s ω⋆=3π\omega^\star = 3\pi.

<Figure size 800x450 with 1 Axes>

Both models concentrate frequency mass around ω⋆=3π\omega^\star = 3\pi — SSGP via the marginal-likelihood gradient, VSSGP via the data-fit term of the ELBO (the KL pulls μΩ\mu_\Omega back toward the prior at ω=0\omega = 0, but the data wins for the frequencies that matter).

5. Three methods, one plot — the uncertainty hierarchy

Same target, same data, three methods, three predictive bands.

<Figure size 1800x450 with 3 Axes>
SSGP: gap/data std ratio = 6.1x
VSSGP: gap/data std ratio = 1.0x

Reading the figure left-to-right:

  • Learned RFF MAP (left) — clean fit, but no signal that the gap is uncertain. The curve through x∈(−0.2,0.4)x \in (-0.2, 0.4) is just whatever the optimiser found.
  • SSGP (centre) — same fit quality, plus a closed-form ±2σ\pm 2\sigma band that opens visibly across the gap. The marginal-likelihood objective also tuned σn\sigma_n and ℓ\ell for free.
  • VSSGP (right) — same fit + an MC band that captures uncertainty over both Ω and β. The grey member traces show how the posterior draws disagree in the gap.

Cost ladder. MAP: O(NM⋅iters)\mathcal{O}(NM \cdot \text{iters}) Adam steps, no per-prediction overhead. SSGP: O(NM2+M3)\mathcal{O}(NM^2 + M^3) per ML-II step, O(M2)\mathcal{O}(M^2) per prediction (closed-form). VSSGP: O(S⋅NM)\mathcal{O}(S \cdot NM) per ELBO step (with SS MC samples), O(S⋅M)\mathcal{O}(S \cdot M) per prediction. SSGP is usually the sweet spot for moderate data; VSSGP wins when frequency uncertainty actually matters or when the marginal-likelihood Cholesky gets unstable.

Takeaways

  • The same RFF feature map (whose kernel-approximation properties are pinned down in the Kernel Approximation notebook) admits three Bayesian regimes, all with trained Ω:
    • Learned RFF MAP — point estimates everywhere, regularised MSE. Cheapest, no uncertainty.
    • SSGP (Lázaro-Gredilla et al. 2010) — analytic marginalisation of the head β, train Ω on the GP marginal likelihood. Closed-form predictive variance for free; σn\sigma_n and ℓ\ell tuned by ML-II.
    • VSSGP (Gal & Turner 2015) — variational posteriors over both Ω and β, tempered ELBO with reparameterisation. MC predictive band that captures frequency uncertainty.
  • Predictive uncertainty hierarchy. MAP gives no band. SSGP gives a closed-form band on the head’s posterior. VSSGP gives an MC band on the joint posterior over frequencies and head — the richest, at the cost of variational machinery.

Where to next.

  • The non-Bayesian / NN flavors of RFF (fixed-Ω with closed-form ridge, ensemble-of-MAP for cheap predictive uncertainty) live in the RFF as Neural Networks notebook.
  • The deep / hierarchical versions (deep RFF, deep SSGP, deep VSSGP — Cutajar et al. 2017) live in the Deep Random Feature Expansions notebook, which builds on the per-layer SSGP / VSSGP primitives derived here.