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.

Many sites, one model: pooled hyperparameters over ragged station records

Open In Colab

Example 1 fitted one station. Here we fit a small network of stations whose records start and end on different days, have different lengths and have their own gaps. This is what a real station archive looks like. Two of the stations have only a month of data. That is too little to learn a kernel from, so on their own their hyperparameters are barely constrained. We pad the records to one shape with pad_sites and run one Markov-GP marginal likelihood per station under jax.vmap. The stations share what they have in common through population-level priors on the kernel hyperparameters, so the short stations borrow strength from the long ones.

pyrox-ssm capability shown: pad_sites, which pads ragged per-station series to one (N_Ω, N) grid and returns the observation mask, and MarkovGPPrior.log_marginal(..., mask=...) vmapped over stations inside a numpyro.plate, so one NumPyro model holds every station with its own hyperparameters, and MarkovGPPrior.condition(..., mask=...).predict(...) vmapped over posterior draws and stations, which gives every station’s posterior predictive on a dense grid. Cost: every log-density evaluation is one Kalman filter pass per station, O(NΩNDx3)\mathcal{O}(N_\Omega N D_x^3) in the number of stations NΩN_\Omega, the padded record length NN and the state dimension DxD_x. The vmapped stations run as one batched filter, so they share the sequential scan over the NN steps. We measure the wall-clock times of every fit, of the predictions and of one gradient in section 8.

What you’ll learn:

  1. How to turn ragged station records into padded arrays and a mask with pad_sites, and why the padding changes no station’s likelihood.

  2. How to write a hierarchical Markov-GP model as a plain NumPyro function, with per-station kernel hyperparameters drawn around population values in a non-centred form.

  3. How to predict every station through its gaps, before its record starts and after it ends, averaged over the posterior draws.

  4. How partial pooling pulls the estimates and the predictions of a short record towards the population, compared with fitting that record on its own.

The single-station model, its priors and the Kalman marginal likelihood are explained in Example 1, and we reuse them here without deriving them again.

Background

The network is a set Ωy\Omega_y of NΩ≡NΩy=6N_\Omega \equiv N_{\Omega_y} = 6 stations, indexed by n=1,…,NΩn = 1, \ldots, N_\Omega. Station nn reported on the days Ty,n\mathcal{T}_{y,n}, and it has Nn≡∣Ty,n∣N_n \equiv \vert \mathcal{T}_{y,n} \vert daily mean temperatures yn,ky_{n,k} (°C) on the days tn,k∈Ty,nt_{n,k} \in \mathcal{T}_{y,n}, k=1,…,Nnk = 1, \ldots, N_n. The days are strictly increasing within a station, and a gap is simply a day that is not in Ty,n\mathcal{T}_{y,n}. Each station follows the model of Example 1,

yn,k=cn+μ(tn,k)+fn(tn,k)+εn,ky_{n,k} = c_n + \mu(t_{n,k}) + f_n(t_{n,k}) + \varepsilon_{n,k}
μ(t)=acos⁡(ωt)+bsin⁡(ωt)\mu(t) = a \cos(\omega t) + b \sin(\omega t)
fn∼GP(0,kMateˊrn-3/2(t,t′;σf,n2,ℓn))f_n \sim \mathcal{GP}(0, k_\text{Matérn-3/2}(t, t'; \sigma_{f,n}^2, \ell_n))
εn,k∼N(0,σε2)\varepsilon_{n,k} \sim \mathcal{N}(0, \sigma_\varepsilon^2)

with ω=2π/365.25 days−1\omega = 2\pi / 365.25 \text{ days}^{-1} as before, where we have moved the mean temperature cn∈Rc_n \in \mathbb{R} (°C) out of the seasonal cycle μ(t)\mu(t). We pool the parameters at three levels, each for a physical reason:

  1. The offset cnc_n is not pooled at all, as it depends on the station’s elevation and siting.

  2. The seasonal amplitudes a,b∈Ra, b \in \mathbb{R} (°C) are shared by every station (complete pooling), because the stations sit in one region and see the same annual cycle. So is the noise variance σε2∈R>0\sigma_\varepsilon^2 \in \mathbb{R}_{>0}, because every station has the same type of sensor.

  3. The weather anomaly differs from station to station, but not by much, so its kernel variance σf,n2\sigma_{f,n}^2 and lengthscale ℓn\ell_n (days) are partially pooled.

For the partial pooling we write the two hyperparameters in log form, θn=(log⁡σf,n2,log⁡ℓn)∈R2\boldsymbol{\theta}_n = (\log \sigma_{f,n}^2, \log \ell_n) \in \mathbb{R}^{2}, and draw them around a population mean μθ∈R2\boldsymbol{\mu}_\theta \in \mathbb{R}^{2} with a population spread τθ∈R>02\boldsymbol{\tau}_\theta \in \mathbb{R}^{2}_{>0},

θn∼N(μθ,diag(τθ2))\boldsymbol{\theta}_n \sim \mathcal{N}(\boldsymbol{\mu}_\theta, \mathrm{diag}(\boldsymbol{\tau}_\theta^2))

If τθ\boldsymbol{\tau}_\theta is small the stations share one kernel, and if it is large each station is on its own, so the data decide how much the stations pool. With six stations τθ\boldsymbol{\tau}_\theta is poorly known. In this centred form the posterior of each θn\boldsymbol{\theta}_n narrows as τθ\boldsymbol{\tau}_\theta shrinks, which makes a funnel that NUTS cannot step through. So we sample the equivalent non-centred form, in which the standardised offsets zn∈R2\mathbf{z}_n \in \mathbb{R}^{2} are a priori independent of the population,

θn=μθ+τθ⊙zn\boldsymbol{\theta}_n = \boldsymbol{\mu}_\theta + \boldsymbol{\tau}_\theta \odot \mathbf{z}_n
zn∼N(0,I2)\mathbf{z}_n \sim \mathcal{N}(\mathbf{0}, \mathbf{I}_2)

The offset cnc_n gets Example 1’s prior N(10,102)\mathcal{N}(10, 10^2). A Gaussian constant plus a GP is again a GP, so we do not sample cnc_n at all and fold it into the anomaly’s prior instead,

cn+fn∼GP(10,102+kMateˊrn-3/2(t,t′;σf,n2,ℓn))c_n + f_n \sim \mathcal{GP}(10, 10^2 + k_\text{Matérn-3/2}(t, t'; \sigma_{f,n}^2, \ell_n))

In state-space form the constant kernel is a one-dimensional state that never changes (ConstantSDE), which SumSDE stacks with the two-dimensional Matérn-3/2 state, so Dx=3D_x = 3 and the Kalman filter integrates cnc_n out together with the path. This removes six sampled values that would be strongly correlated with aa and bb, which matters for NUTS (section 4).

To vmap one Kalman filter over the stations, every station needs a record of the same length. pad_sites extends station nn after its last day to N=max⁡nNnN = \max_n N_n steps at its median spacing δn\delta_n, so tn,Nn+j=tn,Nn+jδnt_{n,N_n+j} = t_{n,N_n} + j \delta_n for j=1,…,N−Nnj = 1, \ldots, N - N_n. It also returns the mask on,k∈{0,1}o_{n,k} \in \{0, 1\}, which is 1 on the NnN_n real days. A masked step runs only the filter’s predict step, so the padding adds exactly zero to the log marginal. The model’s log likelihood is then a sum of Example 1’s masked log marginals over the stations,

log⁡p(Y∣a,b,Θ,σε2)=∑n=1NΩ∑k=1Non,klog⁡N(yn,k;10+μ(tn,k)+Hμx,n,k∣k−1,HΣx,n,k∣k−1H⊤+σε2)\log p(\mathbf{Y} \mid a, b, \boldsymbol{\Theta}, \sigma_\varepsilon^2) = \sum_{n=1}^{N_\Omega} \sum_{k=1}^{N} o_{n,k} \log \mathcal{N}(y_{n,k}; 10 + \mu(t_{n,k}) + \mathbf{H} \boldsymbol{\mu}_{x,n,k \mid k-1}, \mathbf{H} \boldsymbol{\Sigma}_{x,n,k \mid k-1} \mathbf{H}^\top + \sigma_\varepsilon^2)

where Y∈RNΩ×N\mathbf{Y} \in \mathbb{R}^{N_\Omega \times N} stacks the padded records as rows and Θ∈RNΩ×2\boldsymbol{\Theta} \in \mathbb{R}^{N_\Omega \times 2} the θn\boldsymbol{\theta}_n. Here μx,n,k∣k−1∈RDx\boldsymbol{\mu}_{x,n,k \mid k-1} \in \mathbb{R}^{D_x} and Σx,n,k∣k−1∈RDx×Dx\boldsymbol{\Sigma}_{x,n,k \mid k-1} \in \mathbb{R}^{D_x \times D_x} are station nn’s one-step-ahead state mean and covariance, and the measurement row H∈R1×Dx\mathbf{H} \in \mathbb{R}^{1 \times D_x} adds the constant to the Matérn-3/2 value.

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.7.0
pyrox     : 0.3.1
pyrox_ssm : 0.2.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. A network of ragged station records

We simulate one year of a six-station network from the model itself, so that we know every true parameter. The seasonal cycle is Example 1’s (an 8 °C annual cycle with its coldest point near 1 January). The sensor noise is again 0.5 °C, and the station offsets range from 7.5 to 12 °C. The population of anomaly kernels is centred on Example 1’s values, a variance of (2 °C)2(2 \text{ °C})^2 and a four-day lengthscale. Its spread is 0.3 in log units (about ±35 %), and each station’s θn\boldsymbol{\theta}_n is one draw from it.

The stations then report on their own schedules. Stations 1 to 4 each cover about four months, starting and ending in different seasons. Station 2 has a 25-day logger outage, station 3 misses another 30 % of its days at random, and station 4 has a 15-day outage. Stations 5 and 6 span only 35 and 30 days. As in Example 1, every station also misses each day with probability 0.08. We keep only the days a station reported. So each record is a pair of arrays (days and values) of its own length, and the records cannot be stacked into one array.

station   first day  last day   N_n    σ_f²      ℓ
1                 0       119   104    5.48   3.83
2                60       189    95    3.68   4.83
3               151       287    87    6.79   4.80
4               240       363   102    3.57   2.97
5               170       204    34    2.54   3.00
6               320       349    28    4.16   3.62
<Figure size 1200x1000 with 6 Axes>

2. Padding the records with pad_sites

pad_sites takes one array of days and one array of values per station. It returns three (N_Ω, N) arrays: the padded days T\mathbf{T}, the padded values Y\mathbf{Y} (zero after each record ends) and the mask O\mathbf{O}. We print the mask’s row sums, which must equal the record lengths. We also look at the end of a short station’s row, where the padded days continue at its median spacing.

times (6, 104), y (6, 104), mask (6, 104) bool
N = max_n N_n = 104
mask row sums: [104, 95, 87, 102, 34, 28]
record lengths: [104, 95, 87, 102, 34, 28]
station 6, steps k = 26 to 31:
  t    [347.0, 348.0, 349.0, 350.0, 351.0, 352.0]
  y    [-0.67, -0.04, -1.51, 0.0, 0.0, 0.0]
  mask [True, True, True, False, False, False]
<Figure size 1300x360 with 2 Axes>

Each row is filled from the left, and every station keeps its own calendar. Step kk is a different day at each station, and a gap inside a record is a longer step, not a masked entry. Padding happens only after the last observation, so the padded steps are pure forecasts that the mask leaves out of the likelihood.

The per-station prior below is the one the model uses. station_prior builds the summed SDE of the background section, with the prior mean 10+μ(t)10 + \mu(t) as mean_fn, and station_log_marginal calls its log_marginal with the station’s mask row. We check the padding claim on station 6 with its true parameters, comparing the padded and masked log marginal with the one on its own unpadded record.

station 6, padded to 104 steps: log p = -42.182642701530
station 6, its own 28 days: log p = -42.182642701530
difference: 0.00e+00

The two log marginals agree exactly in float64, because the padded steps only run the filter’s predict step and add nothing to the sum.

3. The hierarchical model as a plain NumPyro function

The global parameters are ordinary sample sites with Example 1’s priors, a,b∼N(0,102)a, b \sim \mathcal{N}(0, 10^2) and σε2∼LogNormal(log⁡0.1,1)\sigma_\varepsilon^2 \sim \mathrm{LogNormal}(\log 0.1, 1). The population mean μθ\boldsymbol{\mu}_\theta gets a normal prior centred on Example 1’s prior medians for σf2\sigma_f^2 and ℓ\ell (2 °C² and 7 days) with unit standard deviation. So before any data a station’s θn\boldsymbol{\theta}_n is at least as uncertain as it was in Example 1. The spread gets τθ,d∼HalfNormal(0.5)\tau_{\theta,d} \sim \mathrm{HalfNormal}(0.5) for d=1,2d = 1, 2, which says that stations in one region rarely differ by more than a factor of three. Inside numpyro.plate("station", N_Ω), every sample site has one value per station.

pyrox-ssm has no helper for the non-centred step yet, so we write it inline in noncentred_log_normal. It samples zn,dz_{n,d} from a standard normal, shifts and scales it, and records exp⁡(θn,d)\exp(\theta_{n,d}) as a deterministic site, so the posterior of σf,n2\sigma_{f,n}^2 and ℓn\ell_n ends up in the trace. (NumPyro’s LocScaleReparam(centered=0) does the same rewrite on a centred Normal site.) Then jax.vmap runs station_log_marginal over the station axis of the padded arrays, with the shared aa, bb and σε2\sigma_\varepsilon^2 broadcast. Finally, numpyro.factor adds the NΩN_\Omega log marginals as one factor site under the plate.

We do not call markov_gp_factor here. It registers its own factor site for one prior, so it cannot run under jax.vmap. The vmapped log_marginal with one factor is the pattern its docstring shows for ragged sites.

site 'cos_amp'             Normal         value shape=()
site 'sin_amp'             Normal         value shape=()
site 'noise_var'           LogNormal      value shape=()
site 'variance_loc'        Normal         value shape=()
site 'variance_scale'      HalfNormal     value shape=()
site 'lengthscale_loc'     Normal         value shape=()
site 'lengthscale_scale'   HalfNormal     value shape=()
site 'station'             plate          value shape=(6,)
site 'variance_z'          Normal         value shape=(6,)
site 'variance'            deterministic  value shape=(6,)
site 'lengthscale_z'       Normal         value shape=(6,)
site 'lengthscale'         deterministic  value shape=(6,)
site 'y'                   factor         value shape=(6, 0)

So the trace holds seven global sample sites, two sample sites with one value per station, the two deterministic per-station hyperparameters and one factor with one term per station. The latent paths and the offsets are integrated out by the Kalman filters, so NUTS sees 7+2NΩ=197 + 2 N_\Omega = 19 unconstrained values whatever the record lengths.

4. NUTS

We run two chains of 150 draws after 250 warm-up steps, with a dense mass matrix as in Example 1. So warm-up adapts to the correlations between the seasonal amplitudes and the kernel hyperparameters. We start the chains at the prior medians (init_to_median). The warm-up is longer than Example 1’s, because the dense mass matrix now has 19×1919 \times 19 entries to adapt. We split the CPU into two XLA devices at import (numpyro.set_host_device_count(2)), so chain_method="parallel" runs the two chains on two cores at once. The wall-clock time includes JIT compilation.


                         mean       std    median      5.0%     95.0%     n_eff     r_hat
            cos_amp     -7.93      0.57     -7.95     -8.83     -7.01    210.54      1.00
    lengthscale_loc      1.45      0.14      1.44      1.25      1.70    476.14      1.00
  lengthscale_scale      0.16      0.12      0.13      0.00      0.31    159.54      0.99
   lengthscale_z[0]     -0.14      0.93     -0.13     -1.40      1.62    261.96      1.01
   lengthscale_z[1]      0.73      0.86      0.81     -0.78      2.08    264.35      1.01
   lengthscale_z[2]     -0.11      0.79     -0.16     -1.51      0.98    303.33      1.00
   lengthscale_z[3]     -0.46      0.81     -0.53     -1.72      0.76    350.46      1.00
   lengthscale_z[4]     -0.19      1.01     -0.17     -1.94      1.28    231.05      1.00
   lengthscale_z[5]     -0.10      0.84     -0.09     -1.62      1.17    379.80      0.99
          noise_var      0.27      0.04      0.27      0.21      0.34    415.51      1.00
            sin_amp     -1.01      0.66     -1.02     -2.08     -0.05    216.60      1.01
       variance_loc      1.35      0.18      1.34      1.06      1.58    435.35      0.99
     variance_scale      0.16      0.12      0.13      0.00      0.32    149.73      1.00
      variance_z[0]     -0.05      0.93     -0.09     -1.82      1.15    361.11      1.00
      variance_z[1]     -0.04      0.91     -0.11     -1.61      1.28    263.87      1.00
      variance_z[2]     -0.08      0.87     -0.08     -1.72      1.06    247.19      1.00
      variance_z[3]      0.30      0.84      0.35     -1.16      1.58    198.46      1.00
      variance_z[4]     -0.15      0.98     -0.19     -1.73      1.47    294.46      1.01
      variance_z[5]      0.18      0.98      0.22     -1.39      1.66    209.63      0.99

Number of divergences: 0
NUTS wall-clock: 96.9 s for 2 chains x 400 iterations, 0 divergences
leapfrog steps after warm-up: 6660, 22.2 per draw (one batched Kalman filter pass over 6 stations and its gradient each)

Both chains mixed well: we get no divergences, every R^\hat{R} is between 0.99 and 1.01, and the smallest effective sample size is about 150, for the population spreads τθ\boldsymbol{\tau}_\theta, which are the hardest part of any hierarchical model. After warm-up NUTS took 22.2 leapfrog steps per draw, against 6.2 for Example 1’s six-parameter posterior, and the whole run took 96.9 s.

5. Pooled estimates against the truth

For every station we compare the posterior of σf,n2\sigma_{f,n}^2 and ℓn\ell_n with the value we simulated it with. For the shared parameters and the population we do the same with aa, bb, σε2\sigma_\varepsilon^2, exp⁡(μθ)\exp(\boldsymbol{\mu}_\theta) and τθ\boldsymbol{\tau}_\theta. Each row gives the posterior mean, the standard deviation and the 90 % interval.

parameter                  truth          mean ± sd         90% interval
cos_amp                    -8.00    -7.928 ± 0.570   [ -8.810,  -6.959]  in
sin_amp                    -1.50    -1.011 ± 0.656   [ -2.048,  -0.017]  in
noise_var                   0.25     0.270 ± 0.038   [  0.211,   0.341]  in
exp(variance_loc)           4.00     3.932 ± 0.791   [  2.943,   5.009]  in
variance_scale              0.30     0.156 ± 0.120   [  0.014,   0.404]  in
exp(lengthscale_loc)        4.00     4.303 ± 0.623   [  3.345,   5.402]  in
lengthscale_scale           0.30     0.156 ± 0.116   [  0.021,   0.371]  in

variance[1] (N_n=104)       5.48     3.948 ± 0.958   [  2.720,   5.405] out
lengthscale[1] (N_n=104)    3.83     4.202 ± 0.662   [  3.114,   5.305]  in
variance[2] (N_n=95)        3.68     3.926 ± 0.999   [  2.723,   5.735]  in
lengthscale[2] (N_n=95)     4.83     4.898 ± 0.928   [  3.653,   6.694]  in
variance[3] (N_n=87)        6.79     3.831 ± 0.839   [  2.774,   5.257] out
lengthscale[3] (N_n=87)     4.80     4.234 ± 0.671   [  3.221,   5.378]  in
variance[4] (N_n=102)       3.57     4.227 ± 0.940   [  3.040,   6.043]  in
lengthscale[4] (N_n=102)    2.97     4.008 ± 0.582   [  3.145,   5.066] out
variance[5] (N_n=34)        2.54     3.841 ± 0.906   [  2.663,   5.108] out
lengthscale[5] (N_n=34)     3.00     4.224 ± 0.893   [  2.882,   5.745]  in
variance[6] (N_n=28)        4.16     4.117 ± 1.002   [  2.854,   5.881]  in
lengthscale[6] (N_n=28)     3.62     4.253 ± 0.716   [  3.248,   5.509]  in

All seven shared and population parameters have the truth inside their 90 % intervals. We recover the noise variance (0.27 ± 0.04 against 0.25) and the population lengthscale (4.30 ± 0.62 days against 4) well, because every station contributes to them. The population spreads are the weakest part. Both have a posterior mean of 0.16, about half the true 0.3, and their intervals run from almost zero to about 0.4. Six stations with three to four months of data each cannot tell how different the stations really are, so the model pools more strongly than the truth would justify.

We see the effect in the per-station rows. Eight of the twelve intervals contain the true value. All four misses are stations whose truth lies far from the population and whose estimate is pulled towards it: the variances of stations 1 and 3 (true 5.48 and 6.79 °C², posterior means 3.95 and 3.83), the variance of station 5 (true 2.54 °C², posterior mean 3.84) and the lengthscale of station 4 (true 2.97 days, posterior mean 4.01).

6. Prediction at every station

A posterior draw ee gives a(e)a^{(e)}, b(e)b^{(e)}, (σε(e))2(\sigma_\varepsilon^{(e)})^2 and every station’s θn(e)\boldsymbol{\theta}_n^{(e)}. For one draw, MarkovGPSitesPrior(sde, times, mask=mask, mean_fn=...) holds every station’s padded record, with the kernel’s per-station hyperparameters as (NΩ,)(N_\Omega,) leaves, and condition(Y, noise_var) runs each station’s Kalman filter and RTS smoother. Then predict(t_star) gives the posterior mean and variance of the latent cn+μ(t∗)+fn(t∗)c_n + \mu(t_*) + f_n(t_*) at any test times t∗t_*, as in Example 1. The constant state is smoothed together with the path, so the offset stays integrated out and the bands include the uncertainty of cnc_n. We do this for NE=100N_{\mathcal{E}} = 100 NUTS draws, e∈Ee \in \mathcal{E}, with one jax.vmap over the draws; the prior handles the stations. With μn,∗(e)∈R\mu_{n,*}^{(e)} \in \mathbb{R} and (σn,∗(e))2∈R>0(\sigma_{n,*}^{(e)})^2 \in \mathbb{R}_{>0} the posterior mean and variance at station nn under draw ee, we average as in Example 1,

E[cn+μ(t∗)+fn(t∗)∣Y]≈μˉn,∗=1NE∑e∈Eμn,∗(e)\mathbb{E}[c_n + \mu(t_*) + f_n(t_*) \mid \mathbf{Y}] \approx \bar{\mu}_{n,*} = \frac{1}{N_{\mathcal{E}}} \sum_{e \in \mathcal{E}} \mu_{n,*}^{(e)}
V[cn+μ(t∗)+fn(t∗)∣Y]≈1NE∑e∈E(σn,∗(e))2+1NE∑e∈E(μn,∗(e)−μˉn,∗)2\mathbb{V}[c_n + \mu(t_*) + f_n(t_*) \mid \mathbf{Y}] \approx \frac{1}{N_{\mathcal{E}}} \sum_{e \in \mathcal{E}} (\sigma_{n,*}^{(e)})^2 + \frac{1}{N_{\mathcal{E}}} \sum_{e \in \mathcal{E}} (\mu_{n,*}^{(e)} - \bar{\mu}_{n,*})^2

and a new observation yn,∗y_{n,*} adds (σε(e))2(\sigma_\varepsilon^{(e)})^2 to each (σn,∗(e))2(\sigma_{n,*}^{(e)})^2. The test grid has a point every 12 hours from day 0 to day 380. So it covers each station’s record and its gaps, the days before the station started and after it stopped (when other stations were reporting), and 15 days past the end of the year. A station’s prediction conditions on its own record only. The other stations reach it through the shared aa, bb and σε2\sigma_\varepsilon^2 and through the pooled kernel hyperparameters, because in this model the anomalies of different stations are independent.

prediction at 761 times for 6 stations x 100 draws: 7.3 s (with JIT)

Each panel below is one station. The dark band is ±2σ\pm 2\sigma for the latent cn+μ+fnc_n + \mu + f_n and the light band ±2σ\pm 2\sigma for a new observation. The thin black line is the true latent over the days the station existed, the dots are the days it reported and the red crosses the days it missed, which the model never saw. The dotted vertical lines mark the station’s first and last reported day, and the grey ticks along the bottom are its padded steps, which the mask keeps out of the likelihood.

<Figure size 1200x1300 with 6 Axes>

Inside each record the band is narrow and follows the observations, and it widens over the two outages (stations 2 and 4) and over the days a station missed. Before a station starts and after it stops, the posterior mean relaxes to the station’s climatology cn+μ(t)c_n + \mu(t), with the seasonal cycle that the network learned from all six stations. Within a few lengthscales the band widens to the stationary anomaly spread plus the uncertainty of cnc_n. Station 1, for instance, stopped on day 119, and its prediction for the rest of the year is the shared annual cycle shifted by its own offset.

Because the data are simulated, we know the values the stations would have reported on the days they missed. Below we compute, for each station, the fraction of its missed days whose value falls inside the ±2σ\pm 2\sigma band for yn,∗y_{n,*}, which should be close to 95 %, and the RMSE of the posterior mean on those days.

station   withheld in outage  coverage of ±2σ     RMSE
1               16         0          100.0 %   0.71 °C
2               35        25          100.0 %   1.79 °C
3               53         0           92.5 %   1.09 °C
4               23        15           95.7 %   1.59 °C
5                1         0          100.0 %   0.57 °C
6                2         0          100.0 %   0.35 °C

outages          n =  40  coverage of ±2σ =  97.5 %  RMSE = 2.01 °C
random dropouts  n =  90  coverage of ±2σ =  95.6 %  RMSE = 0.95 °C
all withheld     n = 130  coverage of ±2σ =  96.2 %  RMSE = 1.36 °C

Over all 130 missed days, 96.2 % fall inside the ±2σ\pm 2\sigma band for a new observation: 97.5 % of the 40 outage days and 95.6 % of the 90 random dropouts. As in Example 1, the error is larger inside the outages (RMSE 2.01 °C) than at isolated missing days (0.95 °C). The lowest coverage is at station 3 (92.5 %), which missed 53 of its 140 days, more than any other station.

7. The short stations on their own

To see what pooling buys, we fit stations 5 and 6 without the network, with Example 1’s model. Each station gets its own aa, bb, σf2\sigma_f^2, ℓ\ell and σε2\sigma_\varepsilon^2 with Example 1’s priors, and its offset is integrated out as above, so nothing is shared. The two fits then have independent posteriors. So we run them as one pair of NUTS chains on a plate of two stations (padded with pad_sites again), which costs one batched filter per gradient rather than two.

Note that the fit on its own has to learn the seasonal cycle and the noise from a month of data as well as the kernel. The network model gets aa, bb and σε2\sigma_\varepsilon^2 from all six stations, so this comparison shows everything a short record gains from the network, not only what it gains from pooling θn\boldsymbol{\theta}_n.

independent NUTS wall-clock: 21.1 s, 0 divergences, 11.6 leapfrog steps per draw

parameter                  truth          mean ± sd         90% interval
variance[5] network         2.54     3.841 ± 0.906   [  2.663,   5.108] out
variance[5] on its own      2.54     2.563 ± 1.536   [  1.030,   5.488]  in
lengthscale[5] network      3.00     4.224 ± 0.893   [  2.882,   5.745]  in
lengthscale[5] on its own    3.00     3.535 ± 2.192   [  1.372,   8.314]  in
variance[6] network         4.16     4.117 ± 1.002   [  2.854,   5.881]  in
variance[6] on its own      4.16     6.085 ± 3.639   [  2.347,  13.501]  in
lengthscale[6] network      3.62     4.253 ± 0.716   [  3.248,   5.509]  in
lengthscale[6] on its own    3.62     4.371 ± 1.268   [  2.642,   6.537]  in

The forest plot below shows every station’s posterior median and 90 % interval for σf,n2\sigma_{f,n}^2 and ℓn\ell_n, sorted by record length. Blue is the network model and orange, for the two short stations, the fit on their own. The black crosses are the true values, and the grey band is the posterior median and 90 % interval of the population value exp⁡(μθ,d)\exp(\mu_{\theta,d}).

<Figure size 1300x420 with 2 Axes>

On their own, the short stations constrain their kernels only loosely, and we see this in the width of every orange interval. Station 6’s variance has a 90 % interval of [2.35, 13.50] °C² on its own and [2.85, 5.88] °C² in the network, and station 5’s lengthscale [1.37, 8.31] days on its own and [2.88, 5.75] days in the network. Over the four hyperparameters of the two short stations, the network’s 90 % intervals are 1.7 to 3.7 times narrower. Station 6’s estimates also move closer to the truth, as on its own a month of data puts the posterior mean of its variance at 6.09 °C² (true 4.16) and in the network at 4.12.

The price is the bias of shrinkage. Station 5 has the smallest true variance in the network (2.54 °C²). The network pulls its estimate up to 3.84, so its interval misses the truth, whereas the fit on its own has a posterior mean of 2.56 and an interval 1.8 times wider that contains it. Partial pooling trades this bias at the unusual stations for much tighter estimates at the typical ones.

The hyperparameters matter because of what they do to the predictions. Below we predict stations 5 and 6 from their fits on their own, with the same posterior_predict, where each draw now carries the station’s own aa, bb and σε2\sigma_\varepsilon^2: StationMean then holds one aa and bb per station, an (NΩ′,)(N_\Omega',) leaf that MarkovGPSitesPrior maps over the stations as it does the kernel’s. We compare with the network’s prediction over each record and the 30 days on either side. The true latent was never simulated outside a station’s record, so there we draw the true climatology cn+μ(t)c_n + \mu(t) as a dashed line instead.

<Figure size 1400x450 with 2 Axes>

In numbers, for the 30 days before each short record and the 30 days after it, we compare the mean half-width of the latent ±2σ\pm 2\sigma band and the RMSE of the posterior mean against the true climatology cn+μ(t)c_n + \mu(t). Inside the record we compare the RMSE of the posterior mean against the true latent on the days the station existed.

station  window           2σ network      2σ alone  RMSE network    RMSE alone
5        30 d before         4.09 °C       4.81 °C       0.80 °C       1.63 °C
5        30 d after          4.07 °C       4.82 °C       0.46 °C       0.35 °C
5        own record          0.75 °C       0.84 °C       0.39 °C       0.39 °C
6        30 d before         4.27 °C       7.44 °C       0.58 °C       1.37 °C
6        30 d after          4.27 °C       7.52 °C       0.56 °C       0.64 °C
6        own record          0.76 °C       0.69 °C       0.39 °C       0.39 °C

Inside the two records the predictions agree. The posterior mean is 0.39 °C RMSE from the true latent for both fits at both stations. The mean half-widths of the bands differ by about 10 % (0.75 against 0.84 °C at station 5, 0.76 against 0.69 °C at station 6), because a month of daily data pins the path down wherever there are data. The difference is away from the data. On its own, a station learns its seasonal cycle from one month, so its band keeps widening with the distance from the record. Over the 30 days on either side its mean half-width is 4.81 to 7.52 °C. The network knows the seasonal cycle from the other stations, so its band stops growing once the anomaly has forgotten the last observation, at a mean half-width of 4.07 to 4.27 °C. Its posterior mean is also closer to the true climatology in three of the four windows, for example 0.58 against 1.37 °C RMSE in the 30 days before station 6. In the fourth it is slightly further (0.46 against 0.35 °C after station 5).

8. Cost

Each NUTS leapfrog step costs one Kalman filter pass per station over the NN padded steps, plus its reverse-mode gradient. Under jax.vmap the stations run as one filter on a batch of NΩN_\Omega three-dimensional states, so the sequential scan over the NN steps is shared. Below we time one jitted log likelihood and its gradient for a single station at the padded length NN and for the whole network, and we list the wall-clock times of the fits and the predictions.

one station    N = 104:   2.88 ms per log likelihood + gradient (1.0x one station)
all 6 stations N = 104:   4.52 ms per log likelihood + gradient (1.6x one station)

network NUTS    :   96.9 s  (2 chains x 400 iterations, 6660 leapfrog steps after warm-up)
independent NUTS:   21.1 s  (2 short stations, 2 chains x 400 iterations, 3492 leapfrog steps)
network prediction    :   7.3 s  (6 stations x 100 draws x 761 times, with JIT)
independent prediction:   4.3 s  (2 stations x 100 draws x 761 times, with JIT)

One log likelihood and its gradient cost 2.88 ms for one station and 4.52 ms for all six, so six times the records cost 1.6 times as much. This is because the vmapped filters share the sequential scan over the NN padded steps. Most of the cost of the network fit is in the number of gradients that NUTS needs for the hierarchical posterior (22.2 leapfrog steps per draw after warm-up), not in the extra stations. The network fit took 96.9 s with the two chains on two cores, and the two short stations on their own took 21.1 s. The predictions are cheap next to the fits: 7.3 s for 100 draws at all six stations on the 761-point grid, and 4.3 s for the two short stations on their own, both including JIT compilation.

Summary

  • Ragged station records go into one model through pad_sites, which pads every record after its last day and returns the mask. The masked padding changes no station’s log marginal, so the gaps stay in the days and the padding stays out of the likelihood.

  • A hierarchical Markov-GP model is a plain NumPyro function: global sample sites, a numpyro.plate over stations, a non-centred draw of each station’s kernel hyperparameters around the population, and MarkovGPPrior.log_marginal(..., mask=...) vmapped over the stations into one factor site.

  • The latent paths and the station offsets stay integrated out, so NUTS runs on 7+2NΩ7 + 2 N_\Omega values for any record length. The network fit took 96.9 s of NUTS on CPU, and one gradient over six stations costs 1.6 times one station.

  • The two short stations borrow strength from the network: the 90 % intervals of their kernel hyperparameters are 1.7 to 3.7 times narrower than when each is fitted on its own. With six stations the population spread is poorly known (posterior mean 0.16 against a true 0.3), so the model also pulls unusual stations towards the population, and four of the twelve per-station intervals miss the truth on that side.

  • MarkovGPSitesPrior(...).condition(...).predict(...), vmapped over posterior draws, predicts every station through its gaps and beyond its record, and 96.2 % of the 130 withheld days fall inside the ±2σ\pm 2\sigma band for a new observation. Away from their records, the short stations’ bands in the network have a mean half-width of 4.07 to 4.27 °C against 4.81 to 7.52 °C on their own, because the seasonal cycle comes from the whole network.

The next example keeps the stations but adds a regional anomaly that all of them share, on top of each station’s own anomaly. For the single-station model and its predictions see Example 1.