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.

Regional plus local: one signal shared by every station and one signal per station

Open In Colab

Example 2 gave every station its own weather anomaly and let the stations share only their hyperparameters. However, stations a few tens of kilometres apart see the same weather systems pass over them, so a large part of their anomalies is one regional signal. Here we split each station’s anomaly into a slow regional part that every station shares and a faster local part of its own. The regional part couples the stations, so they can no longer be fitted one at a time. We put all of them into one state-space model on the merged grid of the days that any station reported, with SharedPlusSiteMarkovGPPrior, and the Kalman filter integrates out the regional path and every local path at once. A station’s gaps are then filled by its neighbours, and the posterior splits each record into its regional and local parts.

pyrox-ssm capability shown: SharedPlusSiteMarkovGPPrior.from_sites, which merges ragged station records onto one grid with a (T, N_Ω) mask, log_marginal, which is the exact Kalman log marginal of the observed (day, station) pairs under one block-diagonal linear-Gaussian SSM, and decompose together with as_lgssm(...).smooth(...), which give the posterior of the regional and the local parts. Cost: every log-density evaluation is one Kalman filter pass over the TT merged days with a state of dimension Dx=Dg+NΩDhD_x = D_g + N_\Omega D_h, so O(TDx3)\mathcal{O}(T D_x^3), cubic in the number of stations NΩN_\Omega. We measure the wall-clock times of the fits, of the predictions and of one gradient for growing networks in section 8.

What you’ll learn:

  1. How to write a regional-plus-local model for a station network as a plain NumPyro function, with from_sites for the merged grid and log_marginal as one factor.

  2. How to recover the regional signal and each station’s local signal, with their uncertainty, from the posterior averaged over the NUTS draws.

  3. How the regional signal lets the network predict a station through its outages and after it stops, compared with Example 2’s model, in which the stations do not share their anomalies.

The single-station model, its priors and the Kalman marginal likelihood are explained in Example 1, and the ragged records and pad_sites in Example 2. We reuse both here without deriving them again.

Background

The network is again a set Ωy\Omega_y of NΩ≡NΩyN_\Omega \equiv N_{\Omega_y} stations, indexed by n=1,…,NΩn = 1, \ldots, N_\Omega, and station nn reported on the days Ty,n\mathcal{T}_{y,n}. We work with daily mean temperature anomalies (°C), each station’s record minus its climatology, which we take as known (it is Example 2’s cn+μ(t)c_n + \mu(t)). The anomaly that Example 2 modelled as one independent GP fnf_n per station is now a regional anomaly gg, shared by every station, plus a local anomaly hnh_n,

yn(t)=g(t)+hn(t)+εn(t)y_n(t) = g(t) + h_n(t) + \varepsilon_n(t)
g∼GP(0,kMateˊrn-3/2(t,t′;σg2,ℓg))g \sim \mathcal{GP}(0, k_\text{Matérn-3/2}(t, t'; \sigma_g^2, \ell_g))
hn∼GP(0,kMateˊrn-3/2(t,t′;σh2,ℓh))h_n \sim \mathcal{GP}(0, k_\text{Matérn-3/2}(t, t'; \sigma_h^2, \ell_h))
εn(t)∼N(0,σε2)\varepsilon_n(t) \sim \mathcal{N}(0, \sigma_\varepsilon^2)

with gg, h1,…,hNΩh_1, \ldots, h_{N_\Omega} and the noise all independent. The kernel variances σg2\sigma_g^2 and σh2\sigma_h^2 (°C²), the lengthscales ℓg\ell_g and ℓh\ell_h (days) and the noise variance σε2\sigma_\varepsilon^2 (°C²) are all in R>0\mathbb{R}_{>0}, and the local kernel is pooled over the stations, so θ=(σg2,ℓg,σh2,ℓh,σε2)∈R5\boldsymbol{\theta} = (\sigma_g^2, \ell_g, \sigma_h^2, \ell_h, \sigma_\varepsilon^2) \in \mathbb{R}^{5} are the five parameters that NUTS infers. We expect the regional signal to be the slower and the larger of the two, but the model does not impose it.

Because gg is shared, the stations are no longer independent given θ\boldsymbol{\theta}, and Example 2’s one filter per station does not apply. Instead we merge the station days into one grid, Ty=⋃nTy,n={t1<…<tT}\mathcal{T}_y = \bigcup_n \mathcal{T}_{y,n} = \{t_1 < \ldots < t_T\} with T≡NTyT \equiv N_{\mathcal{T}_y}, on which the day index kk is the same for every station. So station nn’s anomaly on day tkt_k is yn,ky_{n,k}, observed when the mask on,k∈{0,1}o_{n,k} \in \{0, 1\} is 1. The state stacks the Matérn-3/2 states of gg and of every hnh_n,

xk=(xg,k,xh,1,k,…,xh,NΩ,k)∈RDx\mathbf{x}_k = (\mathbf{x}_{g,k}, \mathbf{x}_{h,1,k}, \ldots, \mathbf{x}_{h,N_\Omega,k}) \in \mathbb{R}^{D_x}

where xg,k,xh,n,k∈R2\mathbf{x}_{g,k}, \mathbf{x}_{h,n,k} \in \mathbb{R}^{2}, so Dg=Dh=2D_g = D_h = 2 and Dx=Dg+NΩDhD_x = D_g + N_\Omega D_h. The independent processes give block-diagonal transitions and model noise covariances,

Fk=blockdiag(Fg,k,Fh,1,k,…,Fh,NΩ,k)\mathbf{F}_k = \mathrm{blockdiag}(\mathbf{F}_{g,k}, \mathbf{F}_{h,1,k}, \ldots, \mathbf{F}_{h,N_\Omega,k})

with Fk,Ση,k∈RDx×Dx\mathbf{F}_k, \boldsymbol{\Sigma}_{\eta,k} \in \mathbb{R}^{D_x \times D_x} and each block the discretisation of its SDE over Δtk=tk−tk−1\Delta t_k = t_k - t_{k-1} (Example 1), and Ση,k\boldsymbol{\Sigma}_{\eta,k} built in the same way. Station nn observes the sum of the regional block and its own block, so row nn of the measurement matrix H∈RNΩ×Dx\mathbf{H} \in \mathbb{R}^{N_\Omega \times D_x} is (Hg,0,…,Hh,…,0)(\mathbf{H}_g, \mathbf{0}, \ldots, \mathbf{H}_h, \ldots, \mathbf{0}), with Hg,Hh∈R1×2\mathbf{H}_g, \mathbf{H}_h \in \mathbb{R}^{1 \times 2} the Matérn-3/2 measurement rows and Hh\mathbf{H}_h in block nn. On day tkt_k only the stations Ok={n:on,k=1}\mathcal{O}_k = \{n : o_{n,k} = 1\} reported, and the Kalman filter updates with those rows only. With yOk∈RNOk\mathbf{y}_{\mathcal{O}_k} \in \mathbb{R}^{N_{\mathcal{O}_k}} their anomalies and HOk∈RNOk×Dx\mathbf{H}_{\mathcal{O}_k} \in \mathbb{R}^{N_{\mathcal{O}_k} \times D_x} their rows of H\mathbf{H},

log⁡p(Y∣θ)=∑k=1Tlog⁡N(yOk;HOkμx,k∣k−1,HOkΣx,k∣k−1HOk⊤+σε2INOk)\log p(\mathbf{Y} \mid \boldsymbol{\theta}) = \sum_{k=1}^{T} \log \mathcal{N}(\mathbf{y}_{\mathcal{O}_k}; \mathbf{H}_{\mathcal{O}_k} \boldsymbol{\mu}_{x,k \mid k-1}, \mathbf{H}_{\mathcal{O}_k} \boldsymbol{\Sigma}_{x,k \mid k-1} \mathbf{H}_{\mathcal{O}_k}^\top + \sigma_\varepsilon^2 \mathbf{I}_{N_{\mathcal{O}_k}})

where Y∈RT×NΩ\mathbf{Y} \in \mathbb{R}^{T \times N_\Omega} holds the anomalies with one row per merged day and one column per station, and μx,k∣k−1∈RDx\boldsymbol{\mu}_{x,k \mid k-1} \in \mathbb{R}^{D_x} and Σx,k∣k−1∈RDx×Dx\boldsymbol{\Sigma}_{x,k \mid k-1} \in \mathbb{R}^{D_x \times D_x} are the one-step-ahead moments of the joint state. This is the dense Gaussian of all the observed (day, station) pairs, computed in O(TDx3)\mathcal{O}(T D_x^3) instead of O((∑nNn)3)\mathcal{O}((\sum_n N_n)^3), where Nn≡∣Ty,n∣N_n \equiv \vert \mathcal{T}_{y,n} \vert.

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 that shares its weather

We simulate 135 days of a five-station network from the model itself, so that we know every true value. The regional anomaly has Example 1’s 2 °C spread but decorrelates over about eight days, as a passing weather system would. Each station adds a local anomaly of 1 °C that decorrelates over about two days, and the sensor noise is again 0.5 °C. We simulate all the paths on every day, including days that no station reported, so that we can score the predictions later.

The stations report on their own schedules over the first 120 days, and days 120 to 134 are left for a forecast. Stations 1 and 2 cover the whole window, and station 2 has a 20-day logger outage from day 80, which falls in the coldest regional spell of the record. Station 3 is read by hand every second day from day 10 to day 110, station 4 starts on day 30 and misses another 30 % of its days, and station 5 stops after day 80. As in Example 2, every station also misses each day with probability 0.08, and we keep only the days a station reported.

station   first day  last day   N_n
1                 0       119   116
2                 0       119    93
3                10       110    50
4                30       119    58
5                 1        80    74
regional anomaly: sd over the days = 1.93 °C
local anomalies: sd over days and stations = 0.93 °C
<Figure size 1200x1000 with 5 Axes>

The green line is the same in every panel. The stations follow it closely and differ from it by their fast local wiggles, so on a day when station 2 is in its outage, the stations that still report say a lot about its anomaly. In Example 2’s model they told it nothing about its path, only about its kernel.

2. The merged grid with from_sites

SharedPlusSiteMarkovGPPrior.from_sites takes the regional kernel, the local kernel and one array of days and one array of values per station, as pad_sites did. It returns the model on the merged grid Ty\mathcal{T}_y and the (T,NΩ)(T, N_\Omega) array Y\mathbf{Y}, which is NaN wherever a station did not report. The model keeps the merged days and the mask. Here we build it with the true kernels, and inside the NumPyro model we rebuild it on the same grid with the sampled ones. Every entry that a station did not report is masked, so it enters neither the value of the log marginal nor its gradient.

y_grid (120, 5), mask (120, 5) bool
T = 120 merged days, from day 0 to 119
observed (day, station) pairs: 391 = 391
NaN entries in y_grid: 209
days with 1, …, 5 stations reporting: [4, 24, 42, 37, 13]
state dimension D_x = 2 + 5 x 2 = 12
<Figure size 1200x260 with 1 Axes>

Unlike Example 2’s padded rows, every column of the mask is the same day at every station, which is what lets the regional block of the state link the stations day by day. A day with three stations reporting is one Kalman update with three rows of H\mathbf{H}.

As a check of the exactness claim, we compare log_marginal at the true parameters with the dense Gaussian over all the observed (day, station) pairs, whose covariance is kg(t,t′)+[n=n′]kh(t,t′)+[n=n′,t=t′]σε2k_g(t, t') + [n = n'] k_h(t, t') + [n = n', t = t'] \sigma_\varepsilon^2 for the pairs (t,n)(t, n) and (t′,n′)(t', n'). The dense one builds the Matérn-3/2 kernel matrices with kernellib and evaluates the Gaussian with gaussx, on a matrix with one row per observation.

N_obs = 391 observed pairs
dense Gaussian : log p = -552.8164935945
Kalman, merged : log p = -552.8164935945
relative difference: 2.2e-16

3. The model as a plain NumPyro function

We give the regional and the local kernel the same priors, Example 1’s priors for σf2\sigma_f^2 and ℓ\ell: σg2,σh2∼LogNormal(log⁡2,1)\sigma_g^2, \sigma_h^2 \sim \mathrm{LogNormal}(\log 2, 1) and ℓg,ℓh∼LogNormal(log⁡7,1)\ell_g, \ell_h \sim \mathrm{LogNormal}(\log 7, 1). So nothing in the priors says which part is slow or large, and only the data separate the regional signal from the local ones. The noise variance keeps Example 1’s prior too, σε2∼LogNormal(log⁡0.1,1)\sigma_\varepsilon^2 \sim \mathrm{LogNormal}(\log 0.1, 1). The model rebuilds SharedPlusSiteMarkovGPPrior on the merged days and mask of network_true with the sampled kernels, and shared_plus_site_factor adds its log_marginal as one factor site named "y". The local kernel has scalar hyperparameters, which SharedPlusSiteMarkovGPPrior pools over the stations. A local kernel whose leaves have a leading axis of length NΩN_\Omega would give each station its own, which is how Example 2’s hierarchy would go on top of this model.

site 'regional_variance'      LogNormal  value shape=()
site 'regional_lengthscale'   LogNormal  value shape=()
site 'local_variance'         LogNormal  value shape=()
site 'local_lengthscale'      LogNormal  value shape=()
site 'noise_var'              LogNormal  value shape=()
site 'y'                      factor     value shape=(0,)

So the trace holds five scalar sample sites and one factor. The regional path and the five local paths, Dx=12D_x = 12 numbers on each of the TT days, are integrated out by the one Kalman filter, so NUTS sees five unconstrained values whatever the number of days.

4. NUTS

We run two chains of 200 draws after 200 warm-up steps with a dense mass matrix, started at the prior medians (init_to_median), and run the chains in parallel on two CPU devices as in Example 2. The wall-clock time includes JIT compilation.


                            mean       std    median      5.0%     95.0%     n_eff     r_hat
     local_lengthscale      1.69      0.24      1.68      1.30      2.05    239.58      1.02
        local_variance      1.12      0.16      1.09      0.84      1.34    232.43      1.01
             noise_var      0.14      0.06      0.14      0.04      0.23    226.84      1.01
  regional_lengthscale      6.92      1.46      6.70      4.57      9.06    434.12      1.00
     regional_variance      3.81      1.38      3.51      1.84      5.91    258.26      1.00

Number of divergences: 0
NUTS wall-clock: 55.5 s for 2 chains x 400 iterations, 0 divergences
leapfrog steps after warm-up: 4076, 10.2 per draw (one Kalman filter pass over a 12-dimensional state and its gradient each)

The chains mixed well: we get no divergences, every R^\hat{R} is between 1.00 and 1.02, and the smallest effective sample size is 227 (the noise variance). NUTS took 10.2 leapfrog steps per draw after warm-up, and the whole run took 55.5 s.

5. The hyperparameters against the truth

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

parameter                  truth          mean ± sd         90% interval
regional_variance           4.00     3.809 ± 1.375   [  2.096,   6.588]  in
regional_lengthscale        8.00     6.917 ± 1.461   [  4.943,   9.473]  in
local_variance              1.00     1.115 ± 0.165   [  0.881,   1.383]  in
local_lengthscale           2.00     1.694 ± 0.238   [  1.306,   2.077]  in
noise_var                   0.25     0.139 ± 0.057   [  0.044,   0.233] out
<Figure size 1600x320 with 5 Axes>

Four of the five 90 % intervals contain the true value. The local kernel is the best determined, with σh2\sigma_h^2 at 1.12 ± 0.17 (true 1) and ℓh\ell_h at 1.69 ± 0.24 days (true 2), because every station contributes its own local path to it. The regional kernel is much less certain, σg2\sigma_g^2 at 3.81 ± 1.38 (true 4) and ℓg\ell_g at 6.92 ± 1.46 days (true 8). There is only one regional path, and 120 days of it span only 15 of its lengthscales. The miss is the noise variance, at 0.14 ± 0.06 against 0.25, with an interval that stops at 0.233. The local anomaly and the noise are both fast and both private to a station, so the posterior trades a little of the noise for a slightly shorter and larger local anomaly, as we see in the two local rows.

6. Decomposition and prediction at every station

A posterior draw e∈Ee \in \mathcal{E} gives θ(e)\boldsymbol{\theta}^{(e)}. For one draw we build the model on an extended grid, the merged days together with the test times t∗t_*, every 12 hours from day 0 to day 134.5. A test time that is not a station day is a row of the mask with no station reporting, so the filter only predicts there. decompose runs the Kalman filter and RTS smoother on that model and returns the smoothed means of the regional part, E[g(t∗)∣Y;θ(e)]\mathbb{E}[g(t_*) \mid \mathbf{Y}; \boldsymbol{\theta}^{(e)}], and of every local part, E[hn(t∗)∣Y;θ(e)]\mathbb{E}[h_n(t_*) \mid \mathbf{Y}; \boldsymbol{\theta}^{(e)}]. decompose returns no variances, so we take them from the smoothed state covariances Σx,∗∣T∈RDx×Dx\boldsymbol{\Sigma}_{x,* \mid T} \in \mathbb{R}^{D_x \times D_x} of as_lgssm(noise_var).smooth(...), which its docstring points to. With hn∈RDx\mathbf{h}_n \in \mathbb{R}^{D_x} row nn of H\mathbf{H}, and hg,hh,n∈RDx\mathbf{h}_g, \mathbf{h}_{h,n} \in \mathbb{R}^{D_x} the same row with only its regional or only its local block kept, so that hn=hg+hh,n\mathbf{h}_n = \mathbf{h}_g + \mathbf{h}_{h,n},

V[g(t∗)∣Y;θ(e)]=hg⊤Σx,∗∣Thg\mathbb{V}[g(t_*) \mid \mathbf{Y}; \boldsymbol{\theta}^{(e)}] = \mathbf{h}_g^\top \boldsymbol{\Sigma}_{x,* \mid T} \mathbf{h}_g
V[hn(t∗)∣Y;θ(e)]=hh,n⊤Σx,∗∣Thh,n\mathbb{V}[h_n(t_*) \mid \mathbf{Y}; \boldsymbol{\theta}^{(e)}] = \mathbf{h}_{h,n}^\top \boldsymbol{\Sigma}_{x,* \mid T} \mathbf{h}_{h,n}
V[g(t∗)+hn(t∗)∣Y;θ(e)]=hn⊤Σx,∗∣Thn\mathbb{V}[g(t_*) + h_n(t_*) \mid \mathbf{Y}; \boldsymbol{\theta}^{(e)}] = \mathbf{h}_n^\top \boldsymbol{\Sigma}_{x,* \mid T} \mathbf{h}_n

The last one is not the sum of the first two, because given the data the regional and the local part of a station are correlated: a warm reading can be either. A new observation yn,∗y_{n,*} adds (σε(e))2(\sigma_\varepsilon^{(e)})^2. We do this for NE=100N_{\mathcal{E}} = 100 NUTS draws with jax.vmap and average each mean and variance over the draws as in Example 1, so every band carries the hyperparameter uncertainty as well as the uncertainty of the paths.

decomposition at 270 times x 5 stations for 100 draws: 3.9 s (with JIT)
max |E[g] + E[h_n] - E[g + h_n]| = 1.8e-15

Each row of the figure below is one station. On the left, the dark band is ±2σ\pm 2\sigma for the latent g+hng + h_n and the light band ±2σ\pm 2\sigma for a new observation. The thin black line is the true g+hng + h_n, the dots are the days the station reported and the red crosses the days it missed (including the forecast after the dotted line), which the model never saw. On the right is the decomposition of the same station: the regional part in green and the local part in purple, each with its ±2σ\pm 2\sigma band and its truth as a thin line of the same colour.

<Figure size 1500x1400 with 10 Axes>

Inside each record the band is narrow and follows the observations. The interesting part is where a station has no data. During station 2’s outage (days 80 to 99) the posterior mean follows the cold spell that stations 1, 3 and 4 saw, and the right-hand panel shows why: the regional part stays as narrow as elsewhere, while the station’s local part falls back to zero with its prior band of ±2σh\pm 2 \sigma_h. The same happens before station 4 starts and after station 5 stops. Station 3 is read every second day, and between two readings its band barely widens. In the forecast after day 119 the regional and the local parts both relax to zero.

The regional signal on its own, with every station’s anomalies behind it, is the same green band as in the right-hand panels. No station measures gg directly, and yet the posterior mean follows the true path through every regional swing of the reporting days, including the cold spell around day 90, because the local parts of different stations average out. In the forecast it relaxes towards zero and its band widens to the prior spread.

<Figure size 1200x400 with 1 Axes>

Because the data are simulated, we know the regional and the local paths on every day and the value each station would have reported on every day it missed. Below we score the decomposition, with the RMSE of each posterior mean against its true path and the fraction of days on which the truth falls inside the ±2σ\pm 2\sigma band, over the 120 reporting days and over the 15-day forecast. The local part of a station is scored over the days from its first to its last report.

component                   days      RMSE   coverage of ±2σ
regional g, days 0-119       120   0.47 °C           97.5 %
regional g, forecast          15   1.18 °C          100.0 %
local h_1, days 0-119        120   0.52 °C           98.3 %
local h_2, days 0-119        120   0.74 °C           94.2 %
local h_3, days 10-110       101   0.58 °C           95.0 %
local h_4, days 30-119        90   0.58 °C           97.8 %
local h_5, days 1-80          80   0.60 °C           93.8 %

Over the reporting days the posterior mean of the regional signal is 0.47 °C RMSE from the truth, and the truth is inside its ±2σ\pm 2\sigma band on 97.5 % of the days. The local parts are harder to recover, with RMSEs of 0.52 to 0.74 °C and coverage between 93.8 and 98.3 %, because each of them is a 1 °C signal that decorrelates over two days and that only one station sees. The largest local error is at station 2, whose local part is unconstrained during its 20-day outage. In the forecast the regional RMSE grows to 1.18 °C, and its band contains the truth on all 15 days.

For the predictions we score every day a station did not report, against the value it would have reported, yn,∗=g+hn+εny_{n,*} = g + h_n + \varepsilon_n. We split these days into four kinds: station 2’s outage, the isolated gaps inside a record (random dropouts and station 3’s days between readings), the reporting days outside a station’s own record (before station 3 or 4 started, after station 3 or 5 stopped), when the other stations were still reporting, and the 15-day forecast after day 119.

withheld days               days      RMSE   coverage of ±2σ
outage (station 2)            20   1.11 °C          100.0 %
gaps inside a record         100   0.77 °C           98.0 %
outside its own record        89   1.08 °C           96.6 %
forecast (days 120-134)       75   1.48 °C          100.0 %

Every kind of withheld day has at least 96.6 % of its values inside the ±2σ\pm 2\sigma band for a new observation, slightly more than the nominal 95 %. The isolated gaps are the easiest (RMSE 0.77 °C), as in Examples 1 and 2. Inside station 2’s outage and outside a station’s own record the RMSE is 1.11 and 1.08 °C, close to σh2+σε2=1.12\sqrt{\sigma_h^2 + \sigma_\varepsilon^2} = 1.12 °C at the true values. This is the error we expect when the regional part is known from the other stations and only the station’s own local part and its noise are not. The forecast, where nobody reports, has the largest error (1.48 °C).

7. What the regional signal buys

To see what the joint model buys, we fit the network with Example 2’s structure instead, in which each station has one anomaly 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)) of its own and the anomalies of different stations are independent. We pool σf2\sigma_f^2, ℓ\ell and σε2\sigma_\varepsilon^2 over the stations (Example 1’s priors), so this model has three parameters and shares only them. As in Example 2, pad_sites pads the records, and one vmapped MarkovGPPrior.log_marginal per station goes into one factor. Both models see the same data, and we score their predictions on the same withheld days.

independent NUTS wall-clock: 18.0 s, 0 divergences, 4.9 leapfrog steps per draw

parameter                  truth          mean ± sd         90% interval
variance                    5.00     3.897 ± 0.550   [  3.059,   4.874] out
noise_var                   0.25     0.219 ± 0.045   [  0.155,   0.299]  in
lengthscale                    -     3.509 ± 0.345   [  2.980,   4.138]

The independent fit has three parameters, so it is cheap (18.0 s, 4.9 leapfrog steps per draw). Its one kernel has to describe both time scales, and it settles in between, at ℓ=3.51±0.35\ell = 3.51 \pm 0.35 days, between the true regional 8 and local 2 days. Its variance σf2=3.90±0.55\sigma_f^2 = 3.90 \pm 0.55 falls short of the prior variance of g+hng + h_n (σg2+σh2=5\sigma_g^2 + \sigma_h^2 = 5), and its 90 % interval misses it, while the noise variance is close to the truth (0.22 against 0.25).

We predict every station from the independent fit with Example 2’s pattern, MarkovGPPrior.condition(...).predict(...) vmapped over the draws and the stations, on the same 12-hour grid.

independent prediction: 3.6 s (with JIT)

withheld days               days   RMSE joint   RMSE indep.   ±2σ joint   ±2σ indep.
outage (station 2)            20      1.11 °C       2.63 °C     100.0 %       90.0 %
gaps inside a record         100      0.77 °C       0.79 °C      98.0 %       98.0 %
outside its own record        89      1.08 °C       2.74 °C      96.6 %       84.3 %
forecast (days 120-134)       75      1.48 °C       1.61 °C     100.0 %      100.0 %

The figure zooms on the two places where the difference is largest: station 2 around its outage and station 5 after it stopped. Blue is the joint model and orange the independent one, each with the ±2σ\pm 2\sigma band for the latent signal.

<Figure size 1500x450 with 2 Axes>

Inside the records the two models agree, with an RMSE of 0.77 and 0.79 °C on the isolated gaps, because a station’s own neighbouring days pin its value down in either model. Away from a station’s own data they differ a lot. During station 2’s outage the independent model relaxes to zero within a few days, while the station’s true anomaly goes down to −6 °C, so its RMSE is 2.63 °C against 1.11 °C for the joint model, and its band holds only 90 % of the withheld values. On the days outside a station’s own record the RMSE is 2.74 against 1.08 °C, and the coverage of the independent band drops to 84.3 %. Its band has the right width for one day, but the days outside a record are not independent, and in a cold spell most of them miss on the same side. In the forecast neither model has data, and the two are close (1.48 against 1.61 °C).

8. Cost

Each NUTS leapfrog step costs one Kalman filter pass over the TT merged days with the DxD_x-dimensional joint state, plus its reverse-mode gradient, so O(TDx3)\mathcal{O}(T D_x^3) with Dx=2+2NΩD_x = 2 + 2 N_\Omega. Below we time one jitted log density and its gradient for networks of 1, 2, 5, 10 and 20 stations on the same merged grid, which we build by repeating this network’s mask and records, and compare with Example 2’s vmapped independent filters on the same number of stations.

 stations   D_x   joint, ms   independent, ms
        1     4        3.86              2.42
        2     6        6.18              3.26
        5    12       11.90              3.85
       10    22       19.23              6.42
       20    42       44.08             11.88

joint NUTS      :   55.5 s  (2 chains x 400 iterations, 4076 leapfrog steps after warm-up)
independent NUTS:   18.0 s  (2 chains x 400 iterations, 1948 leapfrog steps after warm-up)
joint decomposition   :   3.9 s  (100 draws x 270 times, with JIT)
independent prediction:   3.6 s  (5 stations x 100 draws x 270 times, with JIT)

One log density and its gradient cost 11.90 ms for this network of five stations (Dx=12D_x = 12), against 3.85 ms for Example 2’s five independent filters, so the joint model costs 3.1 times as much per gradient. It also needs more gradients, 10.2 leapfrog steps per draw against 4.9, as its posterior has five parameters instead of three, and the joint fit took 55.5 s against 18.0 s. The decomposition of 100 draws on the 270-point grid took 3.9 s, including JIT compilation.

With growing networks the joint gradient went from 3.86 ms for one station (Dx=4D_x = 4) to 44.08 ms for 20 stations (Dx=42D_x = 42), while the independent filters went from 2.42 to 11.88 ms. Between 10 and 20 stations the joint time grew by a factor of 2.3, whereas Dx3D_x^3 grew by (42/22)3≈7(42/22)^3 \approx 7, so at these sizes the fixed cost of each filter step still outweighs the Dx3D_x^3 term. We did not time networks beyond 20 stations. The docstring of SharedPlusSiteMarkovGPPrior points to gaussx’s mean-field Kalman filter, which ignores the posterior correlation between the regional and the local parts, and to an ensemble Kalman filter for the point where Dx3D_x^3 does dominate.

Summary

  • A regional signal plus one local signal per station is a plain NumPyro function: priors on two SDE kernels and the noise, SharedPlusSiteMarkovGPPrior rebuilt on the merged grid that from_sites returns, and shared_plus_site_factor, which adds its log_marginal as one factor site. The log marginal equals the dense Gaussian over all 391 observed (day, station) pairs to a relative difference of 2.2e-16.

  • The Kalman filter integrates out the regional path and every local path, so NUTS runs on five parameters. It recovered four of them within their 90 % intervals, and the local kernel much more tightly than the regional one, since every station sees its own local path but there is only one regional path.

  • decompose, with the variances read from as_lgssm(...).smooth(...) and averaged over 100 NUTS draws, splits every station into its regional and local parts. The regional signal comes back with an RMSE of 0.47 °C and 97.5 % coverage over the reporting days.

  • The shared signal is what fills a station’s outage and the days outside its record. There the joint model’s RMSE is 1.11 and 1.08 °C, against 2.63 and 2.74 °C for Example 2’s independent stations, and its bands keep their coverage.

  • Each gradient costs 11.90 ms for five stations (Dx=12D_x = 12), 3.1 times Example 2’s independent filters, and it grows with the number of stations because the joint state does. The fit took 55.5 s of NUTS on CPU.

The next example moves from a handful of stations to a spatial field, where the regional signal varies in space as well as in time. For the stations on their own see Example 2, and for one station Example 1.