fbesag in time
On a time line, fbesag is a random walk whose step size differs between periods.
A time line as a graph
With neighbours $t-1$ and $t+1$ and one precision, the model is a first-order random walk:
Here $c$ scales the model so that, with $\tau = 1$, the geometric mean of the marginal variances is 1. In fbesag a step inside period $k$ has precision $c\,\tau_k$; across periods, the mean of the two. A period need not be consecutive: below, one is the season of every year.
Example
Four years of simulated weekly counts, 20 expected cases a week: rough in weeks 6 to 26 ($\theta = -1$), very smooth otherwise ($\theta = 4$). Data: fbesag_time_simulated.csv (true $x_t$ and $y_t$).
import numpy as np
import pandas as pd
import scipy.sparse as sp
from pyinla import pyinla
from pyinla.gmrfs import fbesag
d = pd.read_csv("fbesag_time_simulated.csv") # idx, week, season, true x, y, E
n = len(d) # four years of weeks
season = d["season"].to_numpy() # 1: epidemic season, 2: the rest of the year
# the time line: each week is a neighbour of the next one
W = sp.diags([np.ones(n - 1), np.ones(n - 1)], [-1, 1])
m_one = fbesag("idx", W, np.ones(n, int)) # one precision: a random walk
m_season = fbesag("idx", W, season, sd_gamma=1) # one precision for the season, one for the rest
def fit(m):
return pyinla(model={"response": "y", "fixed": ["1"], "random": [m]},
family="poisson", data=d, E=d["E"].to_numpy())
fit_one, fit_season = fit(m_one), fit(m_season)
print(fit_season.summary_hyperpar[["mean", "0.025quant", "0.975quant"]].round(2))
print(fit_season.mlik.iloc[1] - fit_one.mlik.iloc[1]) # log Bayes factor for the two periods
mean 0.025quant 0.975quant
Theta1 for idx -1.05 -1.54 -0.56
Theta2 for idx 3.39 2.21 4.64
30.384214478777494
Both intervals cover the truth.
Precisions

Fit

One precision smooths the same amount everywhere: in the season its intervals are too narrow, and outside it its estimate follows the noise, with intervals twice as wide as those of fbesag.
| period | model | RMSE | 95% interval covers the truth | interval width |
|---|---|---|---|---|
| season | one precision | 0.203 | 0.82 | 0.55 |
| fbesag | 0.172 | 0.95 | 0.66 | |
| rest of the year | one precision | 0.115 | 0.96 | 0.53 |
| fbesag | 0.065 | 0.98 | 0.26 |
RMSE: root mean squared error of the posterior mean of $x_t$. Coverage: share of the weeks whose 95% interval contains the true $x_t$. Width: mean width of the intervals.