Reporting delays and day-of-week effects
Real surveillance data is rarely a clean count of infections on the day they occur. Cases are reported after a delay — an incubation period followed by a reporting lag — and the number reported depends on the day of the week. Tools for real-time estimation such as those of DocumenterCitations.CitationSiteNode("abbott2020estimating-cite-3")
build these features into the observation model so that the latent infection signal is estimated free of reporting artefacts.
This tutorial keeps the renewal infection core of the previous example but replaces the simple observation model with a layered one: infections are convolved through two delay distributions and then modulated by a day-of-week reporting pattern. It also shows the latent process as an ARIMA-style differenced process broadcast to a weekly timescale, and assembles everything with IDProblem. The model follows the configuration of the EpiNow2 package [ DocumenterCitations.CitationSiteNode("abbott2020estimating-cite-4")
] and is fit to daily confirmed COVID-19 cases from Italy's first wave in 2020.
The model
where
A weekly latent process
The latent process is an ARIMA(2,1,1): an AR/MA combination (arma) wrapped in a DiffLatentModel to difference it once. Differencing makes the level a random walk rather than mean-reverting, which suits a reproduction number that can drift.
using ComposableTuringIDModels, Distributions, Random, Turing, Mooncake
using ADTypes: AutoMooncake
Random.seed!(20240601)
arma21 = arma(
init = [Normal(0, 0.2), Normal(0, 0.2)],
damp = [truncated(Normal(0.1, 0.2), 0, 1), truncated(Normal(0.1, 0.05), 0, 1)],
θ = [truncated(Normal(0.0, 0.2), -1, 1)],
ϵ_t = HierarchicalNormal(std = HalfNormal(0.1)))
arima211 = DiffLatentModel(; model = arma21, init = [Normal(0.3, 0.3)])broadcast_weekly makes the process piecewise-constant by week: a new value is drawn each week and held for seven days. This models
weekly_latent = broadcast_weekly(arima211)The infection process
As before, a Renewal process driven by a discretised generation interval. Here we use a rt slot.
renewal = Renewal(; generation_time = Gamma(1.4, 1 / 0.38),
rt = weekly_latent, initialisation = Normal(log(1.0), 1.0))A layered observation model
We start from the NegativeBinomialError link and build outward. ascertainment_dayofweek wraps it with a partially pooled day-of-week multiplier, so reporting can be systematically higher or lower on particular weekdays.
negbin = NegativeBinomialError(cluster_factor = HalfNormal(0.1))
dayofweek_negbin = ascertainment_dayofweek(
negbin; latent_model = HierarchicalNormal(std = HalfNormal(1.0)))LatentDelay convolves the expected observations with a delay distribution (discretised by double interval censoring). Two layers compose sequentially: a fixed incubation period from infection to symptom onset, then a reporting delay from onset to report whose parameters are inferred. The reporting delay is an UncertainDelay: its LogNormal log-scale mean and standard deviation carry priors, so the delay is rediscretised each draw and estimated jointly with the reproduction number rather than fixed from external data.
incubation = LogNormal(1.6, 0.42) # infection -> symptom onset (fixed)
reporting = UncertainDelay( # symptom onset -> report (inferred)
LogNormal, [Normal(0.58, 0.3), truncated(Normal(0.47, 0.2), 0, Inf)];
D = 8.0)
observation = LatentDelay(LatentDelay(dayofweek_negbin, incubation), reporting)That single observation object now carries, from the inside out: a negative binomial link, a day-of-week ascertainment modifier, a fixed incubation-delay convolution, and an inferred reporting-delay convolution — assembled entirely by composition. The reporting-delay parameters flow through the same priors seam as every other parameter, so inferring the delay needs no change to the rest of the model.
The data
We fit the model to the daily confirmed COVID-19 cases from Italy's first wave (the example series shipped with the EpiNow2 package), stored with the docs.
using CSV, DataFrames
datapath = joinpath(pkgdir(ComposableTuringIDModels),
"docs", "src", "tutorials", "data", "italy_data.csv")
italy = CSV.read(datapath, DataFrame)
n = 42
y_obs = italy.confirm[1:n]
(n = n, total_cases = sum(y_obs), from = italy.date[1], to = italy.date[n])(n = 42, total_cases = 115239, from = Dates.Date("2020-02-22"), to = Dates.Date("2020-04-03"))Assemble and fit
IDProblem ties the latent, infection, and observation models to a time span. Its as_turing_model method takes data as a named tuple with a y_t field (passing missing values would instead simulate from the prior).
problem = IDProblem(
infection = renewal,
observation_model = observation,
tspan = (1, n))Fitting conditions on the observed reports, differentiating with the recommended Mooncake backend (see Automatic differentiation backend). We draw four chains in parallel with MCMCThreads(), which gives a cross-chain
posterior = as_turing_model(problem, (y_t = y_obs,))
chain = sample(
posterior, NUTS(0.95; adtype = AutoMooncake(; config = nothing)),
MCMCThreads(), 250, 4; progress = false)┌ Info: Found initial step size
└ ϵ = 0.003125
┌ Info: Found initial step size
└ ϵ = 0.003125
┌ Info: Found initial step size
└ ϵ = 0.0015625
┌ Info: Found initial step size
└ ϵ = 0.003125sample returns a FlexiChains chain, which summarystats summarises directly — no conversion step. The day-of-week scale (DayofWeek.std), the negative-binomial overdispersion (cluster_factor) and the inferred reporting-delay parameters (delay.θ, the LogNormal log-mean and log-sd) appear alongside the latent-process parameters:
using MCMCChains
summarystats(chain)╭─FlexiSummary (9 statistics) ─────────────────────────────────────────────────╮
│ iter collapsed │
│ chain collapsed │
│ ↓ stat = [mean, std, mcse, ess_bulk, ess_tail, rhat, q5, q50, q95] │
│ │
│ Parameters (64) ── AbstractPPL.VarName │
│ Float64 init[1], diff.init[1], diff.init[2], diff.damp[1], diff.damp[2], │
│ diff.θ, diff.std, diff.ϵ_t[1], diff.ϵ_t[2], diff.ϵ_t[3], │
│ init_incidence, exp_I_t[1], exp_I_t[2], exp_I_t[3], exp_I_t[4], │
│ exp_I_t[5], exp_I_t[6], exp_I_t[7], exp_I_t[8], exp_I_t[9], │
│ exp_I_t[10], exp_I_t[11], exp_I_t[12], exp_I_t[13], exp_I_t[14], │
│ exp_I_t[15], exp_I_t[16], exp_I_t[17], exp_I_t[18], exp_I_t[19], │
│ exp_I_t[20], exp_I_t[21], exp_I_t[22], exp_I_t[23], exp_I_t[24], │
│ exp_I_t[25], exp_I_t[26], exp_I_t[27], exp_I_t[28], exp_I_t[29], │
│ exp_I_t[30], exp_I_t[31], exp_I_t[32], exp_I_t[33], exp_I_t[34], │
│ exp_I_t[35], exp_I_t[36], exp_I_t[37], exp_I_t[38], exp_I_t[39], │
│ exp_I_t[40], exp_I_t[41], exp_I_t[42], delay.θ[1], delay.θ[2], │
│ DayofWeek.std, DayofWeek.ϵ_t[1], DayofWeek.ϵ_t[2], │
│ DayofWeek.ϵ_t[3], DayofWeek.ϵ_t[4], DayofWeek.ϵ_t[5], │
│ DayofWeek.ϵ_t[6], DayofWeek.ϵ_t[7], cluster_factor │
│ │
│ Extras (14) │
│ Float64 n_steps, is_accept, acceptance_rate, log_density, │
│ hamiltonian_energy, hamiltonian_energy_error, │
│ max_hamiltonian_energy_error, tree_depth, numerical_error, │
│ step_size, nom_step_size, logprior, loglikelihood, logjoint │
│ │
│ Summary │
│ param mean std mcse ess_bulk ess_tail rhat … │
│ init[1] 1.0125 0.1836 0.0109 284.6344 366.1649 0.9990 … │
│ diff.init[1] -0.0450 0.1895 0.0041 2242.4900 743.7114 1.0000 … │
│ diff.init[2] -0.5196 0.1451 0.0084 303.0623 340.9166 1.0012 … │
│ diff.damp[1] 0.3336 0.1684 0.0205 55.2681 28.5210 1.0302 … │
│ diff.damp[2] 0.1133 0.0486 0.0024 388.4540 174.0953 1.0070 … │
│ diff.θ 0.0131 0.1918 0.0064 896.3039 766.3044 1.0040 … │
│ diff.std 0.1259 0.0706 0.0054 150.7713 169.1856 1.0041 … │
│ diff.ϵ_t[1] -0.8729 0.8307 0.0784 118.8089 263.2514 1.0397 … │
│ diff.ϵ_t[2] -0.7535 0.7866 0.0526 228.2208 243.1559 1.0031 … │
│ diff.ϵ_t[3] -0.0017 0.9655 0.0951 101.6424 379.1894 1.0116 … │
│ init_incide… 3.2378 0.7946 0.0565 194.0618 227.1301 1.0004 … │
│ exp_I_t[1] 46.9200 35.7389 2.8578 194.8746 216.9642 1.0000 … │
│ exp_I_t[2] 64.2084 44.3833 3.5289 197.9441 217.4215 1.0004 … │
│ exp_I_t[3] 88.3971 55.5088 4.3519 204.3449 208.0416 1.0021 … │
│ exp_I_t[4] 122.4236 70.1480 5.3548 217.2657 239.5567 1.0024 … │
│ exp_I_t[5] 170.5723 90.0446 6.5651 238.9204 266.1501 1.0024 … │
│ exp_I_t[6] 239.1201 118.2891 8.0109 273.9305 292.4772 1.0004 … │
│ exp_I_t[7] 337.3188 160.4866 9.7296 352.0098 442.9000 0.9994 … │
│ exp_I_t[8] 437.7706 152.6248 10.1662 266.7581 294.9352 1.0005 … │
│ exp_I_t[9] 600.2783 175.2492 11.1841 281.0957 425.4771 0.9995 … │
│ exp_I_t[10] 824.5622 194.3799 11.7286 304.5209 473.5601 0.9992 … │
│ exp_I_t[11] 1136.6248 208.6732 11.3731 351.5270 762.5431 0.9991 … │
│ exp_I_t[12] 1572.8390 220.5553 10.2843 473.1976 809.9436 0.9998 … │
│ exp_I_t[13] 2185.1413 253.6075 9.2278 778.8751 758.4812 1.0011 … │
│ exp_I_t[14] 3048.1456 377.3203 14.9665 641.7410 846.7010 0.9993 … │
│ exp_I_t[15] 2516.0533 209.4055 8.2370 670.9002 737.2509 1.0007 … │
│ exp_I_t[16] 2935.6942 217.3488 8.3813 711.0111 790.7648 1.0013 … │
│ exp_I_t[17] 3373.1517 228.8580 8.9959 674.4491 761.8518 1.0019 … │
│ exp_I_t[18] 3862.2436 252.0243 10.2896 618.6617 716.6458 1.0031 … │
│ exp_I_t[19] 4417.8542 294.9035 12.5584 562.1594 695.1569 1.0040 … │
│ exp_I_t[20] 5052.2079 364.5094 16.0113 520.3548 741.3928 1.0062 … │
│ exp_I_t[21] 5777.7893 466.3374 20.7758 498.1567 741.7460 1.0062 … │
│ exp_I_t[22] 4906.4223 204.1385 6.4282 996.9794 867.7391 1.0003 … │
│ exp_I_t[23] 5195.3932 207.6025 6.4713 1004.8201 842.7605 1.0003 … │
│ exp_I_t[24] 5451.7872 218.3946 7.1436 914.0732 859.6714 0.9996 … │
│ exp_I_t[25] 5705.5917 243.8619 8.7486 749.1415 908.2284 1.0000 … │
│ exp_I_t[26] 5964.3652 285.0400 11.4567 579.9293 806.2388 1.0003 … │
│ exp_I_t[27] 6230.5298 340.4535 15.2676 486.9394 548.3218 1.0014 … │
│ exp_I_t[28] 6504.5274 408.2994 18.9853 445.8947 503.9935 1.0022 … │
│ exp_I_t[29] 5231.3240 179.0701 8.1424 471.0909 564.9285 1.0055 … │
│ exp_I_t[30] 5169.3772 199.6612 9.3723 444.3913 633.8809 1.0060 … │
│ exp_I_t[31] 5067.8487 225.8126 10.7915 435.7812 557.5041 1.0060 … │
│ exp_I_t[32] 4954.6048 256.5702 12.3091 437.0135 588.5393 1.0056 … │
│ exp_I_t[33] 4837.1457 289.8790 13.8117 444.6767 658.8212 1.0051 … │
│ exp_I_t[34] 4718.0940 324.1779 15.3212 451.7191 836.9802 1.0046 … │
│ exp_I_t[35] 4598.3493 358.4651 16.8012 460.7985 690.9332 1.0041 … │
│ exp_I_t[36] 4031.6816 534.3261 33.2663 276.8722 464.0460 1.0090 … │
│ exp_I_t[37] 3863.7993 600.4873 39.5907 250.9835 362.1073 1.0108 … │
│ exp_I_t[38] 3693.8223 674.3951 46.8112 232.2265 369.4101 1.0124 … │
│ exp_I_t[39] 3529.2530 749.1294 54.0535 220.0358 348.8701 1.0136 … │
│ exp_I_t[40] 3371.9135 822.8008 61.0422 211.7837 284.7997 1.0128 … │
│ exp_I_t[41] 3222.1952 894.9456 67.7186 203.8601 284.7943 1.0129 … │
│ exp_I_t[42] 3079.9927 965.6836 74.2762 198.7947 293.7932 1.0125 … │
│ delay.θ[1] 0.4750 0.2751 0.0078 1259.8636 657.7526 0.9995 … │
│ delay.θ[2] 0.4662 0.1997 0.0079 548.7279 296.6431 1.0008 … │
│ DayofWeek.s… 0.1218 0.0744 0.0108 32.2867 20.5401 1.0342 … │
│ DayofWeek.ϵ… -0.0163 0.6234 0.0479 170.1095 318.0049 1.0212 … │
│ DayofWeek.ϵ… 0.5810 0.6395 0.0476 179.5326 393.4691 1.0007 … │
│ DayofWeek.ϵ… 0.4107 0.6221 0.0381 265.3618 355.9900 1.0011 … │
│ DayofWeek.ϵ… 0.7868 0.8127 0.0911 101.8162 61.0810 1.0046 … │
│ DayofWeek.ϵ… -0.2858 0.6837 0.0724 98.0081 68.5554 1.0065 … │
│ DayofWeek.ϵ… -0.6418 0.7714 0.0987 88.0128 52.2707 1.0071 … │
│ DayofWeek.ϵ… -0.9338 0.7368 0.0647 131.8098 107.5054 1.0070 … │
│ cluster_fac… 0.1108 0.0250 0.0029 87.9073 169.7117 1.0139 … │
╰──────────────────────────────────────────────────────────────────────────────╯DayofWeek.std is the scale of the partially pooled weekday multipliers (its own block, namespaced because the ascertainment modifier introduces a named sub-process); cluster_factor is the negative-binomial overdispersion; delay.θ are the inferred reporting-delay parameters. The day-of-week effect, the reporting delay, and the weekly reproduction number were all estimated jointly — and any of them can be swapped, fixed, or removed by editing one line of the composition above.
Prior versus posterior
Sampling the same model with Prior gives a prior draw over the same parameters. Overlaying it on the posterior with PairPlots.jl — the FlexiChains extension turns each chain, subset to a few keys, into a PairPlots.Series — shows which parameters the six weeks of Italian data moved.
using CairoMakie, PairPlots
prior_chain = sample(posterior, Prior(), 1000; progress = false)
pp_keys = [@varname(diff.damp), @varname(diff.θ),
@varname(diff.std), @varname(cluster_factor)]
pairplot(
PairPlots.Series(chain[pp_keys]; label = "posterior"),
PairPlots.Series(prior_chain[pp_keys]; label = "prior"))
The innovation scale diff.std) and the negative-binomial overdispersion (cluster_factor) tighten under the data, while the autoregressive damping (diff.damp) and moving-average (diff.θ) coefficients of the ARIMA process stay close to their weakly informative priors.
Posterior trajectories
generated_observables; the reports predict on the model with the observations set to missing. Two small helpers reduce the per-draw trajectories to credible bands.
gens = vec(generated_observables(posterior, (y_t = y_obs,), chain).generated)
Rt = credible_bands(reduce(hcat, (exp.(g.Z_t) for g in gens)))
pred = predict(as_turing_model(problem, (y_t = fill(missing, n),)), chain)
yt = predictive_bands(pred, n)
fig = Figure(; size = (760, 620))
ax1 = Axis(fig[1, 1]; ylabel = "Reproduction number Rₜ")
ci_ribbon!(ax1, 1:size(Rt, 1), Rt; color = :purple, label = "posterior median")
hlines!(ax1, [1.0]; color = :grey, linestyle = :dash)
axislegend(ax1; position = :rt)
ax2 = Axis(fig[2, 1]; xlabel = "Day", ylabel = "Confirmed cases")
ci_ribbon!(ax2, 1:size(yt, 1), yt; color = :teal,
label = "posterior predictive")
scatter!(ax2, 1:n, y_obs; color = :black, markersize = 7, label = "observed")
axislegend(ax2; position = :lt)
fig
The weekly
A time-varying reporting pattern
The day-of-week multiplier above is static: one weekly profile held fixed across the series. Reporting behaviour can itself drift — testing capacity changes, weekend effects strengthen or weaken — and the same composition expresses that. Because the ascertainment modifier takes any latent model, replacing the pooled HierarchicalNormal weekday effect with a BroadcastLatentModel over a process that evolves week to week turns the fixed profile into a time-varying one, at the cost of more latent parameters. The structural change is again local to the observation model; the infection and latent
The reporting delay can drift in the same way, through the same seam. An UncertainDelay parameter is a prior slot like any other, so replacing its constant log-mean prior with a process — a RandomWalk — makes the delay distribution itself time-varying: it is rediscretised at each time point and applied with a per-time convolution, while the log-scale spread keeps a constant prior.
drifting = UncertainDelay(
LogNormal, [RandomWalk(), truncated(Normal(0.47, 0.2), 0, Inf)]; D = 8.0)
tv_observation = LatentDelay(
LatentDelay(dayofweek_negbin, incubation), drifting)Nothing else changes: the infection process, the
References
- S. Abbott, J. Hellewell, R. N. Thompson, K. Sherratt, H. P. Gibbs, N. I. Bosse, J. D. Munday, S. Meakin, E. L. Doughty, J. Y. Chun and others. Estimating the time-varying reproduction number of SARS-CoV-2 using national and subnational case counts. Wellcome Open Research 5, 112 (2020).