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.

Sparse variational Markov GP — inducing times, non-Gaussian likelihoods and NumPyro

Open In Colab

SparseMarkovGPPrior is the inducing-time-grid analogue of MarkovGPPrior. It wraps any SDEKernel and a sorted inducing grid z\mathbf{z} of length MM, exposing the standard SVGP building blocks (KZZ,KXZ,diag KXX)(K_{ZZ}, K_{XZ}, \mathrm{diag}\,K_{XX}) derived from the SDE autocovariance k(τ)=Hexp⁡(Fτ)P∞H⊤k(\tau) = H \exp(F\tau) P_\infty H^\top.

Because the predictive-block contract matches the dense SparseGPPrior, the variational guides of pyrox_gp (FullRankGuide, MeanFieldGuide, WhitenedGuide) plug straight in. sparse_markov_elbo gives the ELBO in closed form for a Gaussian likelihood and by quadrature (a gaussx integrator) for any other, and sparse_markov_factor registers it in a NumPyro model.

Cost: O(M3+NM)\mathcal{O}(M^3 + N M) per ELBO evaluation. This is the SVGP construction (Titsias, 2009; Hensman, Fusi & Lawrence, 2013) applied to an SDE kernel; it does not yet exploit the Markov structure of q(u)q(\mathbf{u}), which the sparse Markovian algorithms of Adam et al. (2020) and Wilkinson, Solin & Adam (2021) do to make the cost linear in MM.

The notebook covers the Gaussian case with a closed-form q(u)q(\mathbf{u}), a Poisson likelihood fitted by optimising the ELBO, the same model in NumPyro, and, last, how to choose the inducing grid and when the dense Markov path is the better tool.

Setup

Detect Colab and install pyrox-gp[colab] (which pulls in matplotlib and watermark) and pyrox-ssm only when running there. The guides and the Poisson likelihood come from pyrox_gp; the Gaussian likelihood from the pyrox core.

Python implementation: CPython
Python version       : 3.13.12
IPython version      : 9.13.0

jax       : 0.10.2
equinox   : 0.13.8
numpyro   : 0.21.0
pyrox     : 0.3.1
matplotlib: 3.10.9

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

1. Gaussian likelihood: synthetic regression data

Noisy mixture of two sinusoids on an irregular time grid.

<Figure size 800x300 with 1 Axes>

2. Sparse Markov GP setup

Pick M = 12 evenly-spaced inducing times and a Matern-3/2 SDE prior. The variational guide is a FullRankGuide over the inducing function values u=f(Z)u = f(Z).

M (inducing) = 12
N (data)     = 80
prior marginal variance = 1.000
initial ELBO = -767.927

3. Conjugate update — closed-form q(u) for a Gaussian likelihood

For Gaussian observations the optimal q(u)=N(mq,Sq)q(u) = \mathcal{N}(m_q, S_q) is closed-form (Titsias 2009):

Sq=(σ−2KZZ−1KZXKXZKZZ−1+KZZ−1)−1,mq=σ−2SqKZZ−1KZX (y−μ).S_q = (\sigma^{-2} K_{ZZ}^{-1} K_{ZX} K_{XZ} K_{ZZ}^{-1} + K_{ZZ}^{-1})^{-1},\qquad m_q = \sigma^{-2} S_q K_{ZZ}^{-1} K_{ZX}\,(y - \mu).

With B=KZZ+σ−2KZXKXZB = K_{ZZ} + \sigma^{-2} K_{ZX} K_{XZ} the same solution is Sq=KZZB−1KZZS_q = K_{ZZ} B^{-1} K_{ZZ} and mq=σ−2KZZB−1KZX(y−μ)m_q = \sigma^{-2} K_{ZZ} B^{-1} K_{ZX}(y - \mu), which the cell below computes with gaussx solves on a PSD-tagged operator and no explicit inverse. This sidesteps the need to optimise the ELBO and gives the optimal q(u)q(u) at the prescribed inducing locations directly. We use it here to demonstrate the sparse_markov_elbo / ConditionedSparseMarkovGP API on a fitted guide.

closed-form ELBO = -116.684

4. Posterior at the fitted guide

prior.condition(guide) returns the ConditionedSparseMarkovGP, whose predict(t_star) produces the mean / variance at arbitrary test times via the standard SVGP predictive (cost O(M3+∣t∗∣ M)O(M^3 + |t_*|\,M)).

<Figure size 850x360 with 1 Axes>

5. Comparison: sparse vs dense Markov GP

At the closed-form q(u)q(u) and well-spaced inducing points, the SVGP posterior should approximate the dense-Markov posterior. The remaining difference is the inducing-set bias from working with M=12M = 12 pseudo-points instead of all N=80N = 80 data points.

<Figure size 850x360 with 1 Axes>
max |sparse mean − dense mean| = 0.2458

6. Sweep: how many inducing points do we need?

Increase M and re-fit the closed-form q(u)q(u). Past a kernel-dependent threshold, the sparse posterior is indistinguishable from the dense one.

<Figure size 850x360 with 1 Axes>

The table in §9 makes this threshold quantitative.

7. A non-Gaussian likelihood: counts

For a non-Gaussian likelihood there is no closed-form q(u)q(\mathbf{u}), so we maximise the ELBO directly. The data are event counts on an irregular grid, yn∼Poisson(ef(tn))y_n \sim \mathrm{Poisson}(e^{f(t_n)}), the kind of series a detection product produces (fire detections, lightning flashes or plume detections per overpass). sparse_markov_elbo evaluates the expected log-likelihood Eq(fn)[log⁡p(yn∣fn)]\mathbb{E}_{q(f_n)}[\log p(y_n \mid f_n)] by 20-point Gauss–Hermite quadrature.

The guide is a WhitenedGuide, which parameterises q(v)=N(mv,LvLv⊤)q(\mathbf{v}) = \mathcal{N}(\mathbf{m}_v, L_v L_v^\top) in whitened coordinates u=LZZv\mathbf{u} = L_{ZZ}\mathbf{v}; that keeps the optimisation well conditioned as the kernel hyperparameters move (Hensman, Matthews & Ghahramani, 2015). We optimise the guide and the log-variance and log-lengthscale jointly with Adam.

final ELBO: -407.87
learned variance:    1.100
learned lengthscale: 3.595

As a reference, the dense Markov GP fits the same counts with LaplaceMarkovInference at the learned hyperparameters, using all 200 observation times instead of 30 inducing times.

RMSE of the posterior mean of f, sparse ELBO (M = 30): 0.279
RMSE of the posterior mean of f, dense Laplace (N = 200): 0.276
max |sparse mean - dense mean|: 0.057
<Figure size 1400x380 with 2 Axes>

With 30 inducing times for 200 counts, the sparse variational posterior and the dense Laplace posterior agree closely, and both recover the true rate about equally well. They are different approximations (a variational q(u)q(\mathbf{u}) on a coarse grid against a Laplace approximation at every observation), so they are not expected to coincide exactly.

8. The same model in NumPyro

sparse_markov_factor adds the sparse ELBO to a NumPyro model as a factor. Putting priors on the kernel hyperparameters and registering the whitened guide’s parameters as numpyro.param sites in the model gives a nested bound: SVI with an automatic guide over (σ2,ℓ)(\sigma^2, \ell) maximises

Eq(θ)[Lu(θ)+log⁡p(θ)−log⁡q(θ)]≤log⁡p(y),\mathbb{E}_{q(\boldsymbol{\theta})}\bigl[\mathcal{L}_{\mathbf{u}}(\boldsymbol{\theta}) + \log p(\boldsymbol{\theta}) - \log q(\boldsymbol{\theta})\bigr] \le \log p(\mathbf{y}),

where Lu(θ)\mathcal{L}_{\mathbf{u}}(\boldsymbol{\theta}) is the sparse ELBO. Variational inference is the appropriate choice here, and MCMC is not: the factor is a lower bound on log⁡p(y∣θ)\log p(\mathbf{y} \mid \boldsymbol{\theta}), not the likelihood, so NUTS over θ\boldsymbol{\theta} with this factor would sample the wrong distribution. For MCMC over the hyperparameters of a Gaussian-likelihood model, use the exact markov_gp_factor as in the training notebook. Two hyperparameters make a full-rank guide (AutoMultivariateNormal) cheap.

variance     median 0.836   90% interval [0.755, 0.924]
lengthscale  median 3.054   90% interval [2.760, 3.381]
Adam point estimate: variance 1.100, lengthscale 3.595
<Figure size 800x300 with 1 Axes>

The NumPyro posterior sits at a smaller variance and a shorter lengthscale than the Adam point estimate, which falls outside both 90% intervals. The two are fits to different objectives: the NumPyro model adds priors (the inverse-gamma on ℓ\ell has mean 3) and averages over q(θ)q(\boldsymbol{\theta}), and the shift runs along the variance–lengthscale ridge of the training notebook, since σ2/ℓ3\sigma^2/\ell^3 is similar for both (about 0.029 against 0.024). Treat the intervals as approximate: one q(u)q(\mathbf{u}) is shared by every value of θ\boldsymbol{\theta} the guide proposes, and the variational bound adds its own bias, so they are typically narrower than an exact posterior would be. NumPyro’s AutoMultivariateNormal.sample_posterior does not handle the model’s constrained numpyro.param site here, which is why the draws come from Predictive over the guide.

9. Choosing the inducing grid, and when not to go sparse

9.1 How dense must the inducing grid be?

The inducing times must resolve the posterior, so their spacing Δz\Delta z has to be small relative to the lengthscale ℓ\ell, and how small depends on the kernel’s roughness. The cell below draws 1000 noisy observations over 50 lengthscales from Matern GPs of each order and compares the closed-form sparse posterior mean on an evenly spaced grid with the exact dense Markov posterior mean, for spacings from 2ℓ2\ell down to ℓ/8\ell/8. The error is the RMSE between the two means, relative to the standard deviation of the dense mean.

spacing dz / ell        2.0      1.0      0.5     0.25    0.125
Matern-1/2            0.553    0.422    0.277    0.167    0.084
Matern-3/2            0.668    0.294    0.098    0.018    0.002
Matern-5/2            0.373    0.140    0.028    0.001    0.000
<Figure size 700x380 with 1 Axes>

The smooth kernels converge fast: at Δz=ℓ/4\Delta z = \ell/4 the Matern-5/2 posterior is within 0.1% of the dense one and the Matern-3/2 posterior within 2%. The rough Matern-1/2 kernel converges slowly and is still about 8% off at Δz=ℓ/8\Delta z = \ell/8: its paths have structure at every scale, so a grid of inducing values cannot summarise them. This is the sparse counterpart of the smoothness choice in the SDE-kernel notebook: a rough kernel needs either a very dense grid or the dense Markov path.

9.2 Sparse or dense Markov, for geoscience records?

For a GP over time, the dense Markov path is already linear in NN. Going sparse pays off only in specific situations, and the O(M3)\mathcal{O}(M^3) cost of this SVGP construction sets a hard limit:

SituationBetter pathWhy
Long record with many decorrelation times, e.g. a decade of daily data with a week-long memorydense MarkovMM must grow like T/ΔzT/\Delta z; at Δz=ℓ/4\Delta z = \ell/4 and T=500ℓT = 500\ell that is M=2000M = 2000 and about 1010 operations per ELBO, against O(N d3)\mathcal{O}(N\,d^3) for the filter
Many observations per decorrelation time, e.g. a 1 Hz sensor whose signal of interest varies over hourssparsea few inducing times per ℓ\ell summarise thousands of points, and the O(NM)\mathcal{O}(NM) term stays cheap
Rough signals (Matern-1/2)dense Markovthe inducing grid converges slowly (§9.1)
Non-Gaussian likelihood inside a larger NumPyro model fitted by SVIsparsesparse_markov_factor is a variational objective that composes with other SVI components; the dense strategies are fixed-point loops
Hyperparameter posterior by MCMCdense Markovmarkov_gp_factor is the exact marginal likelihood; the sparse factor is a bound

The sparse Markovian algorithms of Adam et al. (2020) and Wilkinson, Solin & Adam (2021) remove the O(M3)\mathcal{O}(M^3) limit by running Kalman recursions over the inducing times, which would make the first row a case for going sparse as well.

Summary

  • SparseMarkovGPPrior(sde_kernel, Z) exposes the SVGP predictive blocks for any state-space kernel, so pyrox_gp’s variational guides apply unchanged.

  • For a Gaussian likelihood, the optimal q(u)q(\mathbf{u}) is closed-form; for any other, sparse_markov_elbo with a gaussx integrator is optimised directly, and on Poisson counts 30 inducing times match a dense Laplace fit on 200 points closely.

  • sparse_markov_factor puts the sparse ELBO in a NumPyro model; SVI is the right inference there, because the factor is a bound, not a likelihood.

  • The inducing spacing must be a fraction of the lengthscale: about ℓ/4\ell/4 for smooth kernels (errors of 0.1% to 2% against the dense posterior), and finer than ℓ/8\ell/8 for Matern-1/2. With the current O(M3)\mathcal{O}(M^3) cost, long records with many decorrelation times are better served by the dense Markov path.

References.

  1. Adam, V., Eleftheriadis, S., Artemev, A., Durrande, N. & Hensman, J. (2020). Doubly sparse variational Gaussian processes. AISTATS, PMLR 108, 2874–2884.

  2. Hensman, J., Fusi, N. & Lawrence, N. D. (2013). Gaussian processes for big data. UAI.

  3. Hensman, J., Matthews, A. G. de G. & Ghahramani, Z. (2015). Scalable variational Gaussian process classification. AISTATS, PMLR 38, 351–360.

  4. Titsias, M. (2009). Variational learning of inducing variables in sparse Gaussian processes. AISTATS, PMLR 5, 567–574.

  5. Wilkinson, W. J., Solin, A. & Adam, V. (2021). Sparse algorithms for Markovian Gaussian processes. AISTATS, PMLR 130, 1747–1755.