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.

A spatial field and one path per facility: Poisson counts of emission events with NUTS and SVI

Open In Colab

Examples 1, 2 and 3 all had Gaussian observations, so the Kalman filter integrated every latent path out and NUTS only ever saw a handful of hyperparameters. Here the observations are counts. We take a set of oil and gas facilities at known locations in one basin, and for each facility the daily number of methane emission events that its monitoring flagged. The log of a facility’s event rate is a spatial facility effect, a GP over the facility locations (facilities close to each other share operators, equipment and gas composition), plus a temporal path of its own, a Markov GP over the days (maintenance, liquids unloading and other episodes that last days to weeks). With a Poisson likelihood there is no Kalman marginal, so the paths have to be sampled. We sample them through whitened innovations with markov_gp_innovations_sample, inside a numpyro.plate over the facilities, and fit the same model with NUTS and with SVI.

pyrox-ssm capability shown: markov_gp_innovations_sample, which registers standard normal innovations as one sample site f"{name}_eps" and the Markov-GP path built from them as a deterministic site name, with one independent path per index of an enclosing numpyro.plate. The path is the SDE run forward by a lax.scan, so the latent paths are sampled in a whitened (non-centred) form, which is the geometry NUTS needs. Cost: every log-density evaluation is one scan over the TT days per facility, O(NΩTDx3)\mathcal{O}(N_\Omega T D_x^3), against the O(T3)\mathcal{O}(T^3) Cholesky factor (and the T×TT \times T kernel matrix) of the dense markov_gp_sample. We measure both, per gradient and per NUTS draw, in section 8.

What you’ll learn:

  1. How to write a spatio-temporal Poisson model as a plain NumPyro function that composes a pyrox_gp kernel over the facility locations with a pyrox_ssm path over the days.

  2. How to fit it with NUTS and with SVI, and how their posteriors of the five hyperparameters compare with the truth.

  3. How to read the latent rates, the predictions on withheld days and a short forecast, and a map of the spatial facility effect, off the posterior draws.

  4. What the whitened path costs against the dense one, per gradient and per NUTS draw.

The Matérn-3/2 SDE and its state-space form are explained in Example 1, and the plate over sites and the non-centred draws in Example 2. We reuse both here without deriving them again.

Why pyrox_gp and pyrox_ssm appear together

The spatial effect is an ordinary GP over a few points in R2\mathbb{R}^2, which is what pyrox_gp provides: a Matérn kernel whose hyperparameters carry priors (Matern with set_prior), a GPPrior over the facility coordinates and the whitened gp_sample. The temporal paths are Markov GPs, which are pyrox_ssm. Neither package imports the other. Both depend only on pyrox (the bridge), and pyrox-ssm is forbidden from importing pyrox-gp by its import-guard test (AGENTS.md). That rule is about the dependencies between the packages, not about the models people build with them. So a user who imports both and composes them in one NumPyro function, as we do below, is the intended use, and the two packages meet only through NumPyro’s sample sites.

Background

The basin holds a set Ωy\Omega_y of NΩ≡NΩy=8N_\Omega \equiv N_{\Omega_y} = 8 facilities. We index them by n=1,…,NΩn = 1, \ldots, N_\Omega, as we indexed the stations in Examples 2 and 3, and keep s\mathbf{s} for coordinates: facility nn sits at sn∈Ω⊂R2\mathbf{s}_n \in \Omega \subset \mathbb{R}^{2} (km), and Sy∈RNΩ×2\mathbf{S}_y \in \mathbb{R}^{N_\Omega \times 2} stacks the sn\mathbf{s}_n as rows. The days tkt_k, k=1,…,Tk = 1, \ldots, T, form the grid Tx\mathcal{T}_x of T≡NTxT \equiv N_{\mathcal{T}_x} days on which the latent paths live, and yn,k∈{0,1,2,…}y_{n,k} \in \{0, 1, 2, \ldots\} is the number of events at facility nn on day tkt_k, observed when the mask on,k∈{0,1}o_{n,k} \in \{0, 1\} is 1. The counts are Poisson with a log-linear rate,

yn,k∼Poisson(ρn,k)y_{n,k} \sim \mathrm{Poisson}(\rho_{n,k})
log⁡ρn,k=c+g(sn)+fn(tk)\log \rho_{n,k} = c + g(\mathbf{s}_n) + f_n(t_k)
g∼GP(0,kMateˊrn-3/2(s,s′;σg2,ℓg))g \sim \mathcal{GP}(0, k_\text{Matérn-3/2}(\mathbf{s}, \mathbf{s}'; \sigma_g^2, \ell_g))
fn∼GP(0,kMateˊrn-3/2(t,t′;σf2,ℓ))f_n \sim \mathcal{GP}(0, k_\text{Matérn-3/2}(t, t'; \sigma_f^2, \ell))

where ρn,k∈R>0\rho_{n,k} \in \mathbb{R}_{>0} is the event rate (events per day), c∈Rc \in \mathbb{R} is the intercept (the log rate of a typical facility), gg is the spatial facility effect and fnf_n is facility nn’s temporal path. The paths f1,…,fNΩf_1, \ldots, f_{N_\Omega} are independent of each other and of gg, and they share one kernel, as the local paths of Example 3 did. The kernel variances σg2\sigma_g^2 and σf2\sigma_f^2 (in log-rate units squared), the spatial lengthscale ℓg\ell_g (km) and the temporal lengthscale ℓ\ell (days) are all in R>0\mathbb{R}_{>0}, so θ=(c,σg2,ℓg,σf2,ℓ)∈R5\boldsymbol{\theta} = (c, \sigma_g^2, \ell_g, \sigma_f^2, \ell) \in \mathbb{R}^{5}. With Y∈{0,1,2,…}NΩ×T\mathbf{Y} \in \{0, 1, 2, \ldots\}^{N_\Omega \times T} the counts and ρ∈R>0NΩ×T\boldsymbol{\rho} \in \mathbb{R}_{>0}^{N_\Omega \times T} the rates, the log likelihood keeps only the observed entries,

log⁡p(Y∣ρ)=∑n=1NΩ∑k=1Ton,k(yn,klog⁡ρn,k−ρn,k−log⁡yn,k!)\log p(\mathbf{Y} \mid \boldsymbol{\rho}) = \sum_{n=1}^{N_\Omega} \sum_{k=1}^{T} o_{n,k} (y_{n,k} \log \rho_{n,k} - \rho_{n,k} - \log y_{n,k}!)

The Poisson likelihood is not Gaussian, so we cannot integrate the latent values out with a Kalman filter as in Examples 1 to 3, and NUTS has to sample them. Sampled directly, the latent values and their hyperparameters form a funnel, as τθ\boldsymbol{\tau}_\theta and θn\boldsymbol{\theta}_n did in Example 2. So we sample both in a whitened form, with standard normal values that are a priori independent of θ\boldsymbol{\theta}. For the spatial effect at the facilities, g=g(Sy)∈RNΩ\mathbf{g} = g(\mathbf{S}_y) \in \mathbb{R}^{N_\Omega},

g=LSSzg\mathbf{g} = \mathbf{L}_{SS} \mathbf{z}_g
zg∼N(0,INΩ)\mathbf{z}_g \sim \mathcal{N}(\mathbf{0}, \mathbf{I}_{N_\Omega})

where LSS∈RNΩ×NΩ\mathbf{L}_{SS} \in \mathbb{R}^{N_\Omega \times N_\Omega} is the lower Cholesky factor of the kernel matrix KSS∈RNΩ×NΩ\mathbf{K}_{SS} \in \mathbb{R}^{N_\Omega \times N_\Omega}, [KSS]nn′=kMateˊrn-3/2(sn,sn′;σg2,ℓg)[\mathbf{K}_{SS}]_{nn'} = k_\text{Matérn-3/2}(\mathbf{s}_n, \mathbf{s}_{n'}; \sigma_g^2, \ell_g). This is what pyrox_gp.gp_sample(..., whitened=True) registers. For the temporal paths we run the Matérn-3/2 SDE of Example 1 forward from standard normal innovations zn,k∈RDx\mathbf{z}_{n,k} \in \mathbb{R}^{D_x}, with the state xn,k∈RDx\mathbf{x}_{n,k} \in \mathbb{R}^{D_x} and Dx=2D_x = 2,

xn,1=Lx,∞zn,1\mathbf{x}_{n,1} = \mathbf{L}_{x,\infty} \mathbf{z}_{n,1}
xn,k=Fkxn,k−1+Lη,kzn,k\mathbf{x}_{n,k} = \mathbf{F}_k \mathbf{x}_{n,k-1} + \mathbf{L}_{\eta,k} \mathbf{z}_{n,k}
fn(tk)=Hxn,kf_n(t_k) = \mathbf{H} \mathbf{x}_{n,k}
zn,k∼N(0,IDx)\mathbf{z}_{n,k} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}_{D_x})

where Fk∈RDx×Dx\mathbf{F}_k \in \mathbb{R}^{D_x \times D_x} is the transition over Δtk=tk−tk−1\Delta t_k = t_k - t_{k-1}, H∈R1×Dx\mathbf{H} \in \mathbb{R}^{1 \times D_x} the measurement row, and Lx,∞,Lη,k∈RDx×Dx\mathbf{L}_{x,\infty}, \mathbf{L}_{\eta,k} \in \mathbb{R}^{D_x \times D_x} the Cholesky factors of the stationary covariance Σx,∞\boldsymbol{\Sigma}_{x,\infty} and of the model noise covariance Ση,k=Σx,∞−FkΣx,∞Fk⊤\boldsymbol{\Sigma}_{\eta,k} = \boldsymbol{\Sigma}_{x,\infty} - \mathbf{F}_k \boldsymbol{\Sigma}_{x,\infty} \mathbf{F}_k^\top. The map from the innovations to the path is linear, so the path fn=fn(t1:T)∈RNTx\mathbf{f}_n = f_n(t_{1:T}) \in \mathbb{R}^{N_{\mathcal{T}_x}} has exactly the prior N(0,Ktt)\mathcal{N}(\mathbf{0}, \mathbf{K}_{tt}), with Ktt∈RT×T\mathbf{K}_{tt} \in \mathbb{R}^{T \times T} the Matérn-3/2 kernel matrix over the days, but it costs O(TDx3)\mathcal{O}(T D_x^3) and never forms Ktt\mathbf{K}_{tt}. This is markov_gp_innovations_sample; markov_gp_sample instead samples fn∼N(0,Ktt)\mathbf{f}_n \sim \mathcal{N}(\mathbf{0}, \mathbf{K}_{tt}) directly, which is centred and needs Ktt\mathbf{K}_{tt} and its Cholesky factor on every evaluation. The API calls the zn,k\mathbf{z}_{n,k} innovations, which are not the Kalman residuals of the same name. NUTS then samples θ\boldsymbol{\theta}, zg\mathbf{z}_g and the NΩTN_\Omega T innovations zn,k\mathbf{z}_{n,k}, that is 5+NΩ+NΩTDx5 + N_\Omega + N_\Omega T D_x unconstrained values.

Setup

We detect Colab and install pyrox, pyrox-gp[colab] (which pulls in matplotlib and watermark) and pyrox-ssm only when the notebook runs there.

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
gaussx    : 0.6.15
pyrox     : 0.2.1
pyrox_gp  : 0.2.0
pyrox_ssm : 0.1.0
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. Eight facilities in one basin

We simulate the model itself, so that we know every true value. The basin is 100 km by 100 km and the eight facilities sit at random locations in it. A typical facility has two events per day (c=log⁡2c = \log 2). The spatial effect has a variance of 0.5, so the baseline rates of different facilities differ by a factor of about two either way, and it decorrelates over about 30 km. Each facility’s path has a variance of 0.4 and decorrelates over about a week. We draw the spatial effect jointly on a 40 by 40 grid of map cells and at the facilities, so that we can score the map in section 7.

The record runs for T=80T = 80 days. The facilities report on days 0 to 64, and days 65 to 79 are left for a forecast. On each reporting day a facility’s monitoring is down with probability 0.1, and facility 3 has a 15-day outage from day 40. A day without monitoring is a count we never see, so its mask entry is 0, and we keep its true count to score the predictions.

facility    east km  north km   g(s_n)   mean ρ  days
1              74.7      48.9     0.09     3.84    57
2              17.0      99.8    -0.83     0.89    59
3              66.1      60.2    -0.86     1.37    45
4              30.6      20.0     0.29     2.64    54
5              46.2      38.6     0.47     4.37    56
6              72.2      53.5    -0.27     1.84    63
7              56.1      86.0    -0.57     1.36    55
8              35.1      12.6    -0.09     2.72    59
observed facility days: 448 of 520
largest daily count: 14
<Figure size 1500x480 with 4 Axes>

2. The model as a plain NumPyro function

The intercept gets c∼N(0,1)c \sim \mathcal{N}(0, 1), so a typical facility has between about 0.14 and 7 events per day (two standard deviations either way). The spatial kernel is pyrox_gp.Matern(nu=1.5), and its hyperparameters get their priors with set_prior, so the kernel itself registers them as the sample sites "spatial.variance" and "spatial.lengthscale": σg2∼LogNormal(log⁡0.5,1)\sigma_g^2 \sim \mathrm{LogNormal}(\log 0.5, 1) and ℓg∼LogNormal(log⁡30,0.5)\ell_g \sim \mathrm{LogNormal}(\log 30, 0.5). Eight facilities say very little about a spatial lengthscale, so its prior states the scale of the basin (between about 10 and 80 km). The temporal kernel gets σf2∼LogNormal(log⁡0.5,1)\sigma_f^2 \sim \mathrm{LogNormal}(\log 0.5, 1) and Example 1’s prior for the lengthscale, ℓ∼LogNormal(log⁡7,1)\ell \sim \mathrm{LogNormal}(\log 7, 1).

Then gp_sample(..., whitened=True) draws zg\mathbf{z}_g as the site "facility_effect_u" and records g\mathbf{g} as the deterministic "facility_effect". Inside numpyro.plate("facility", N_Ω), markov_gp_innovations_sample("path", ...) draws one (T,Dx)(T, D_x) block of innovations per facility as the site "path_eps" and records the paths as the deterministic "path". The Poisson site "y" is masked with .mask(mask), so an unobserved day adds nothing to the log density, and its count, stored as 0, is never read. Its last dimension, the days, is an event dimension (.to_event(1)), so the facility plate owns the first one. We also record the rates as a deterministic site "rate", so the posterior samples carry ρn,k\rho_{n,k} on every day, observed or not.

sample_path defaults to markov_gp_innovations_sample; section 8 passes markov_gp_sample instead, which has the same signature, to fit the dense, centred version of the same model.

site 'intercept'              Normal         value shape=()
site 'spatial.variance'       LogNormal      value shape=()
site 'spatial.lengthscale'    LogNormal      value shape=()
site 'facility_effect_u'      Normal         value shape=(8,)
site 'facility_effect'        deterministic  value shape=(8,)
site 'temporal_variance'      LogNormal      value shape=()
site 'temporal_lengthscale'   LogNormal      value shape=()
site 'facility'               plate          value shape=(8,)
site 'path_eps'               Normal         value shape=(8, 80, 2)
site 'path'                   deterministic  value shape=(8, 80)
site 'rate'                   deterministic  value shape=(8, 80)
site 'y'                      Poisson        value shape=(8, 80)
unconstrained values for NUTS: 1293

So the trace holds the five scalar sample sites of θ\boldsymbol{\theta} (two of them registered by the pyrox_gp kernel under its scope "spatial"), the whitened spatial values "facility_effect_u", the innovations "path_eps" with one (T,Dx)(T, D_x) block per facility, three deterministic sites and the masked Poisson site. Unlike Examples 1 to 3, NUTS now sees every latent value, 5+NΩ+NΩTDx=1,2935 + N_\Omega + N_\Omega T D_x = 1{,}293 of them.

3. NUTS

We run two chains of 150 draws after 150 warm-up steps, started at the prior medians with init_to_median, which puts every whitened value at 0. A dense mass matrix over all 1,293 values would have about 1.7 million entries to adapt. However, only a few of them are strongly correlated in the posterior: the intercept trades off against the mean of the facility effects (a higher cc with lower g(sn)g(\mathbf{s}_n) gives the same rates), and σg2\sigma_g^2 against the scale of zg\mathbf{z}_g. So we give the 13 values outside the paths (θ\boldsymbol{\theta} and zg\mathbf{z}_g) one dense 13×1313 \times 13 block of the mass matrix, with dense_mass=[(...)], and keep the 1,280 innovations on the diagonal, where their unit prior scale suits it. The chains run one after the other with chain_method="sequential", as in Example 1. The wall-clock time includes JIT compilation. We print the summary for the five hyperparameters, and for the spatial effects and the rates on the observed facility days only the worst R^\hat{R} and the smallest effective sample size.


                            mean       std    median      5.0%     95.0%     n_eff     r_hat
             intercept      0.30      0.40      0.32     -0.26      1.00    141.19      1.00
      spatial.variance      0.56      0.38      0.45      0.09      1.04    177.24      1.01
   spatial.lengthscale     36.90     19.26     33.13     11.06     63.99    293.37      0.99
     temporal_variance      0.39      0.10      0.38      0.25      0.56     96.91      1.01
  temporal_lengthscale      6.55      1.77      6.34      3.85      9.07    100.38      0.99

g(s_n) and the observed-day rates: largest r_hat = 1.024, smallest n_eff = 48
NUTS wall-clock: 83.4 s for 2 chains x 300 iterations, 1 divergences
leapfrog steps after warm-up: 18899, 63.0 per draw (one scan over 80 days per facility and its gradient each)

We get one divergence in 300 draws, and every R^\hat{R} of the five hyperparameters is between 0.99 and 1.01. The smallest effective sample size among them is 97, for σf2\sigma_f^2. Over the spatial effects and the 448 rates of the observed facility days the largest R^\hat{R} is 1.024 and the smallest effective sample size 48, so the chains are usable but not long. After warm-up every draw took exactly 63 leapfrog steps, a tree of depth 6, and the whole run took 83.4 s.

4. SVI with a mean-field guide

With 1,293 latent values a full-rank Gaussian guide, as in Example 1, would have about a million covariance parameters. In the whitened coordinates, however, the prior is a standard normal with no correlations at all, which is what makes a mean-field guide workable for the latent values, and the reason gp_sample has its whitened=True mode. Whether it is good enough for the hyperparameters we check against NUTS in section 5. AutoNormal puts an independent Gaussian on every unconstrained value, the five hyperparameters included, and we start it at the prior medians. The guide is q(θ,zg,Z;ν)q(\boldsymbol{\theta}, \mathbf{z}_g, \mathbf{Z}; \boldsymbol{\nu}) with ν\boldsymbol{\nu} its means and scales, and SVI minimises LELBO(ν)=−ELBO\mathcal{L}_\text{ELBO}(\boldsymbol{\nu}) = -\mathrm{ELBO}. Predictive with the guide then gives the deterministic sites, the paths and the rates, for 1,000 guide draws.

SVI wall-clock: 14.7 s for 3000 steps x 2 particles
final negative ELBO (mean of last 200 steps): 835.5
<Figure size 1200x300 with 1 Axes>

5. The hyperparameters against the truth

For each hyperparameter we print the posterior mean, standard deviation and 90 % interval from NUTS and from SVI, next to the value we simulated the data with.

parameter                truth      NUTS mean ± sd       NUTS 90% interval       SVI mean ± sd        SVI 90% interval
intercept                 0.69      0.298 ± 0.397   [ -0.369,   0.902]  in      0.493 ± 0.034   [  0.438,   0.547] out
spatial.variance          0.50      0.560 ± 0.375   [  0.150,   1.295]  in      0.459 ± 0.063   [  0.362,   0.567]  in
spatial.lengthscale      30.00     36.905 ± 19.224  [ 13.487,  71.406]  in     22.432 ± 3.403   [ 17.281,  28.204] out
temporal_variance         0.40      0.394 ± 0.104   [  0.260,   0.577]  in      0.123 ± 0.018   [  0.095,   0.155] out
temporal_lengthscale      7.00      6.547 ± 1.768   [  4.142,   9.561]  in      2.940 ± 0.271   [  2.520,   3.412] out
<Figure size 1700x330 with 5 Axes>

NUTS has all five true values inside its 90 % intervals. The temporal kernel is the best determined, with σf2\sigma_f^2 at 0.394 ± 0.104 (true 0.4) and ℓ\ell at 6.55 ± 1.77 days (true 7), because each of the eight facilities contributes its own path to it. The spatial kernel rests on only eight points. So σg2\sigma_g^2 is uncertain (0.56 ± 0.38, true 0.5) and the 90 % interval of ℓg\ell_g runs from 13.5 to 71.4 km, which is mostly the prior. The intercept, 0.30 ± 0.40 against log⁡2=0.69\log 2 = 0.69, is wide because it trades off against the mean of the eight facility effects, which section 7 shows.

SVI does much worse on the hyperparameters. Only σg2\sigma_g^2 falls inside its 90 % interval. The temporal kernel comes out at under a third of the true variance (σf2\sigma_f^2 = 0.123 ± 0.018) and less than half the true lengthscale (ℓ\ell = 2.94 ± 0.27 days), with standard deviations about six times smaller than those of NUTS. The mean-field guide makes every value independent of every other, whereas in the posterior the temporal hyperparameters and the 1,280 innovations depend strongly on each other (the same path can come from a larger σf2\sigma_f^2 with smaller innovations, or the other way round). So we take the NUTS posterior as the reference here, and SVI as a fast approximation of the latent rates, which we check next.

6. Latent rates and predictions at every facility

Every posterior draw e∈Ee \in \mathcal{E} carries the whole rate field, ρn,k(e)=exp⁡(c(e)+g(e)(sn)+fn(e)(tk))\rho_{n,k}^{(e)} = \exp(c^{(e)} + g^{(e)}(\mathbf{s}_n) + f_n^{(e)}(t_k)), on every day of the grid. The paths were sampled on all TT days, and the mask only kept the unobserved days out of the likelihood, so the withheld days and the forecast need no extra step: they are the days on which the posterior of the path is informed by its neighbours (or, in the forecast, by nothing but the prior after the last observation). The rate is a nonlinear function of the latent values, so we summarise it with the posterior mean ρˉn,k=1NE∑e∈Eρn,k(e)\bar{\rho}_{n,k} = \frac{1}{N_{\mathcal{E}}} \sum_{e \in \mathcal{E}} \rho_{n,k}^{(e)} and the 2.5 % and 97.5 % quantiles over the draws, the 95 % band. For a new count we draw yn,k(e)∼Poisson(ρn,k(e))y_{n,k}^{(e)} \sim \mathrm{Poisson}(\rho_{n,k}^{(e)}) once per draw and take the same quantiles, which gives the 95 % predictive interval of the count.

Each panel below is one facility. The blue band is the NUTS 95 % band of the latent rate ρn,k\rho_{n,k} with its posterior mean, and the dashed green lines are the SVI band. The thin black line is the true rate, the orange dots are the observed counts and the red crosses the counts on the days the monitoring was down (including the forecast after the dotted line), which the model never saw. Facility 3’s outage is shaded.

<Figure size 1500x1200 with 8 Axes>

Where a facility reports, the NUTS band follows the true rate and its mean smooths the noisy counts. Over facility 3’s outage the band widens and still holds the true rate, as the days on either side of the outage pin the path down at its ends. In the forecast after day 64 the NUTS band widens towards the stationary spread of the path, whereas the SVI band stays narrow. Its temporal kernel is too small and too short (section 5), so its forecast relaxes to the baseline quickly and with too little spread. It misses the late rise at facility 4, which the NUTS band holds, and neither band reaches the rise at facility 7 in the last days of the forecast.

Because the data are simulated, we know the true rate on every day and the count each facility would have reported on the days its monitoring was down. Below we score both methods: the fraction of facility days on which the true rate is inside the 95 % band, and the fraction of withheld counts inside the 95 % predictive interval, split into the isolated downtime days, facility 3’s outage and the forecast.

facility days               n   ρ in 95% band           y in 95% interval
                                 NUTS     SVI            NUTS     SVI
observed days             448   95.8%   97.1%               -       -
downtime (isolated)        57   91.2%   93.0%           98.2%   96.5%
outage (facility 3)        15  100.0%  100.0%          100.0%   93.3%
forecast (days 65-79)     120   85.0%   63.3%           94.2%   87.5%

On the 448 observed facility days the true rate is inside the 95 % band on 95.8 % of the days for NUTS and 97.1 % for SVI, and on the 57 isolated downtime days on 91.2 % and 93.0 %. Over facility 3’s 15-day outage both bands hold the true rate on every day. The difference is in the forecast. Over its 120 facility days the true rate is inside the NUTS band on 85.0 % of the days and inside the SVI band on only 63.3 %. The 95 % predictive intervals of the withheld counts include the Poisson noise, so they are wider and cover more: 98.2 % (NUTS) and 96.5 % (SVI) of the downtime counts, and 94.2 % and 87.5 % of the forecast counts.

7. The spatial facility effect on a map

The posterior draws give the spatial effect only at the eight facilities, g(e)∈RNΩ\mathbf{g}^{(e)} \in \mathbb{R}^{N_\Omega}. To map it, we condition the spatial GP of each draw on its own values at the facilities and predict at the map cells, which pyrox_gp does with GPPrior(...).condition(g, 0).predict(...), noise-free apart from the prior’s 10-6 jitter. For a map cell s∗∈Ω\mathbf{s}_* \in \Omega, with K∗S∈R1×NΩ\mathbf{K}_{*S} \in \mathbb{R}^{1 \times N_\Omega} the kernel row between the cell and the facilities under draw ee, this gives the mean μ∗(e)=K∗SKSS−1g(e)∈R\mu_*^{(e)} = \mathbf{K}_{*S} \mathbf{K}_{SS}^{-1} \mathbf{g}^{(e)} \in \mathbb{R} and the variance (σ∗(e))2=(σg(e))2−K∗SKSS−1K∗S⊤∈R≥0(\sigma_*^{(e)})^2 = (\sigma_g^{(e)})^2 - \mathbf{K}_{*S} \mathbf{K}_{SS}^{-1} \mathbf{K}_{*S}^\top \in \mathbb{R}_{\ge 0}, and we average them over NE=100N_{\mathcal{E}} = 100 NUTS draws as in Example 1. The posterior of the whole field then carries the uncertainty of the hyperparameters, of g\mathbf{g} and of the field between the facilities.

maps of 1600 cells x 100 draws, NUTS and SVI: 1.8 s
cells               n  sd of truth  RMSE NUTS  RMSE SVI  ±2σ NUTS  ±2σ SVI
all cells        1600         0.72       0.58      0.58     98.6%    92.0%
within 15 km      572         0.58       0.47      0.42     96.7%    83.6%
beyond 15 km     1028         0.77       0.63      0.65     99.6%    96.7%

NUTS: mean posterior sd at the facilities, g(s_n) 0.46, c + g(s_n) 0.26
SVI: mean posterior sd at the facilities, g(s_n) 0.11, c + g(s_n) 0.12

The first two panels share one colour scale: the true field and the NUTS posterior mean of the field, with the facilities as triangles. The third panel is the posterior standard deviation. The last panel compares the posterior at the facilities with the truth, for the spatial effect g(sn)g(\mathbf{s}_n) and for the facility’s baseline log rate c+g(sn)c + g(\mathbf{s}_n), each with its 95 % interval.

<Figure size 1900x440 with 7 Axes>

The posterior mean recovers the large-scale pattern of the field, low in the north and high in the south, but not its detail, and it relaxes to zero away from the facilities, where its standard deviation grows towards the prior spread. In numbers, the NUTS posterior mean is 0.58 RMSE from the true field over the whole map, against a spread of the true field of 0.72. Within 15 km of a facility it is 0.47 (spread 0.58) and beyond 15 km 0.63 (spread 0.77), so eight facilities inform the field only near themselves. The NUTS bands are honest about this, with 98.6 % of the cells inside ±2σ\pm 2\sigma, while the SVI bands hold only 83.6 % of the cells within 15 km of a facility.

The last panel shows why the intercept was uncertain. At the facilities the mean NUTS posterior standard deviation of g(sn)g(\mathbf{s}_n) is 0.46, but that of the baseline c+g(sn)c + g(\mathbf{s}_n) is 0.26: the data fix each facility’s baseline rate much better than they fix how it splits into an intercept and a spatial effect. The SVI standard deviations (0.11 and 0.12) are too small for both, and miss the trade-off between the two.

8. Cost: the whitened path against the dense one

markov_gp_innovations_sample and markov_gp_sample give the paths the same prior. The dense one registers fn∼N(0,Ktt)\mathbf{f}_n \sim \mathcal{N}(\mathbf{0}, \mathbf{K}_{tt}) as one MultivariateNormal site, batched over the facility plate. So every log-density evaluation builds the T×TT \times T kernel matrix Ktt\mathbf{K}_{tt} (one matrix exponential per pair of days, O(T2Dx3)\mathcal{O}(T^2 D_x^3)) and its Cholesky factor (O(T3)\mathcal{O}(T^3)), and NUTS samples the paths in the centred form. The whitened one runs one scan of TT steps per facility. We compare them in two ways. First, we time one jitted log joint density and its gradient for the full model (all eight facilities, the same priors), with every sample site at its prior median, for records of 30 to 240 days. Then we run NUTS on both versions of the model for the first 20 days of the record, one chain of 60 draws after 60 warm-up steps each, and count the leapfrog steps per draw. We use only 20 days because the dense gradient grows so quickly with TT (the first table below) that a dense fit of the full record does not fit in this notebook’s time budget.

 T days   whitened, ms   dense, ms   dense / whitened
     30           0.67        2.91                4.3
     60           0.93       39.06               41.9
    120           2.05      119.12               58.2
    240           3.33      306.83               92.0

One log density and its gradient cost 0.67 ms at 30 days and 3.33 ms at 240 days with the whitened paths, so eight times the days cost five times as much. With the dense paths they cost 2.91 ms at 30 days and 306.83 ms at 240 days, 105 times as much, and at 240 days the dense gradient is 92.0 times the whitened one. The dense path pays for T2T^2 matrix exponentials and an O(T3)\mathcal{O}(T^3) Cholesky factor on every evaluation, while the whitened one pays for TT steps of a scan.

path        NUTS wall-clock  steps per draw  divergences
whitened             19.2 s            49.6            2
dense                35.5 s           151.5            0

full NUTS fit :   83.4 s  (2 chains x 300 iterations, 63.0 leapfrog steps per draw)
SVI           :   14.7 s  (3000 steps x 2 particles)
maps          :    1.8 s  (2 x 100 draws x 1600 cells)

On the first 20 days the dense gradient is still cheap, but NUTS needed 151.5 leapfrog steps per draw with the dense, centred paths against 49.6 with the whitened ones, three times as many. The dense run took 35.5 s and the whitened one 19.2 s, both including JIT compilation, and the whitened run had two divergences in its 60 draws. So the whitened path saves twice: each gradient is cheaper, and its non-centred geometry needs fewer of them. On the full 80-day record the fit took 83.4 s of NUTS, 14.7 s of SVI and 1.8 s for the two maps, all on CPU.

Summary

  • A spatio-temporal Poisson model is a plain NumPyro function that composes a pyrox_gp Matérn kernel over the facility coordinates (its priors set with set_prior, its values drawn with the whitened gp_sample) with one pyrox_ssm Markov-GP path per facility, drawn by markov_gp_innovations_sample under a numpyro.plate. The two packages never import each other; the user composes them through NumPyro’s sites.

  • The trace holds the five hyperparameters, the whitened spatial values "facility_effect_u", the innovations "path_eps" with one (T,Dx)(T, D_x) block per facility, and the deterministic "facility_effect", "path" and "rate". So the posterior draws, from NUTS or from Predictive with a guide, carry the rates on every day, and the withheld days and the forecast need no extra step.

  • NUTS recovers all five hyperparameters within their 90 % intervals, in 83.4 s with 63 leapfrog steps per draw. Mean-field SVI took 14.7 s and gets the rates on the observed days right (97.1 % coverage), but not the hyperparameters: its temporal kernel is far too small and short, and in the forecast its band holds the true rate on only 63.3 % of the facility days, against 85.0 % for NUTS.

  • Conditioning the spatial GP of each draw on its values at the facilities maps the facility effect, with bands that say how little eight facilities tell about the field away from themselves (98.6 % of the map cells inside ±2σ\pm 2\sigma for NUTS).

  • Per gradient, the whitened path cost 0.67 to 3.33 ms for records of 30 to 240 days and the dense markov_gp_sample 2.91 to 306.83 ms. On the first 20 days NUTS needed 49.6 leapfrog steps per draw with the whitened paths against 151.5 with the dense, centred ones.

For the plate over sites and the non-centred draws see Example 2, for a shared signal that couples the sites Example 3, and for the Matérn SDE and the Kalman filter Example 1.