Skip to content

Renewal model with negative-binomial reporting ​

The renewal equation is the workhorse of real-time epidemic estimation: it expresses new infections as a function of past infections weighted by the generation interval, scaled by a time-varying reproduction number [ DocumenterCitations.CitationSiteNode("cori2013new-cite-2")

]. DocumenterCitations.CitationSiteNode("mishra2020derivation-cite-2")

showed that this renewal construction follows from an age-dependent branching process and pairs naturally with a negative-binomial observation model to give a Bayesian hierarchical model for reported case counts.

This tutorial builds that model from two composed parts — a Renewal infection process that carries an autoregressive latent process for , and a NegativeBinomialError observation model — and fits it to the test-confirmed COVID-19 cases from South Korea that DocumenterCitations.CitationSiteNode("mishra2020derivation-cite-3")

analysed. The latent process is folded into the renewal model rather than supplied as a separate top-level component: the reproduction number is the renewal model's own parameter process.

The model ​

is the discretised generation interval, the autoregressive damping, the innovation standard deviation, and the observation overdispersion.

Components ​

The latent process is a second-order autoregressive model on with a HierarchicalNormal innovation term, matching DocumenterCitations.CitationSiteNode("mishra2020derivation-cite-4")

. Strong autocorrelation in the reproduction number is encoded by a first damping prior concentrated near one (  on ) with a weaker second lag. This process is the renewal model's reproduction-number process — it is folded into the infection model below rather than composed separately.

julia
using ComposableTuringIDModels, Distributions, Random, Turing, Mooncake
using ADTypes: AutoMooncake
Random.seed!(1234)

latent = AR(
    damp = [truncated(Normal(0.8, 0.05), 0, 1),
        truncated(Normal(0.1, 0.05), 0, 1)],
    init = [Normal(0.0, 0.2), Normal(0.0, 0.2)],
    ϵ_t = HierarchicalNormal(std = HalfNormal(0.1)))
AR
└─ ϵ_t: HierarchicalNormal

The infection process needs a discrete generation interval. Renewal takes a continuous distribution and discretises it with double interval censoring [ DocumenterCitations.CitationSiteNode("charniga2024best-cite-1")

], using CensoredDistributions.jl. Following DocumenterCitations.CitationSiteNode("mishra2020derivation-cite-5")

we use a serial interval as a proxy for the generation interval. Renewal is the only infection model that carries a generation interval, because it is the only one that uses one; it couples that interval to the latent process (its rt slot) and a prior for the initial infections.

julia
renewal = Renewal(; generation_time = Gamma(6.5, 0.62),
    rt = latent, initialisation = Normal(log(1.0), 0.1))
renewal.gen_int
8-element Vector{Float64}:
 0.026663134095601098
 0.14059778064943768
 0.2502660305615845
 0.24789569560506872
 0.1731751163417783
 0.09635404000022221
 0.045734375752163825
 0.01931382699414364

The stored gen_int is a probability vector — the continuous serial interval binned into daily weights that sum to one. Double interval censoring is not the same as evaluating the continuous density at integer days: it accounts for both the primary and secondary events falling anywhere within their days, which shifts and spreads the mass relative to the underlying [ DocumenterCitations.CitationSiteNode("charniga2024best-cite-2")

].

julia
sum(renewal.gen_int), length(renewal.gen_int)
(0.9999999999999999, 8)

The infection process in isolation ​

Because the renewal model is a model in its own right, it can be exercised on its own — without an observation model — and we can isolate the contribution of the renewal equation by pinning its reproduction-number process to a known trajectory. With the latent folded in, the way to do that is to build a renewal model whose rt slot is a deterministic FixedIntercept latent, giving a constant , and to fix the initial-infections parameter. The same as_turing_model call that composes into the full model then runs the infection process standalone, returning its infections I_t and the internal latent draw Z_t.

julia
fixed_logR = log(1.4)
renewal_fixed = Renewal(; generation_time = renewal.gen_int,
    rt = FixedIntercept(fixed_logR), initialisation = Normal())
demo = fix(as_turing_model(renewal_fixed, 60), (init_incidence = 0.0,))()
(constant_Rt = round(exp(first(demo.Z_t)), digits = 2),
    grows = demo.I_t[end] > demo.I_t[1])
(constant_Rt = 1.4, grows = true)

A constant   grows incidence; a path that declined through zero would instead produce the textbook turn-over (incidence growing, decelerating as  , and falling once  ). Driving the renewal model with a richer fixed path is just a matter of swapping the FixedIntercept latent for a deterministic latent of the desired shape. Nothing here is conditioned on data; the component is inspected in isolation before it is assembled into the full model with its sampled process.

Reported cases are overdispersed counts of the latent infections. The prior is placed on the cluster factor , which is roughly the coefficient of variation of the observation noise and so easier to reason about a priori.

julia
obs = NegativeBinomialError(cluster_factor = HalfNormal(0.1))

IDModel assembles the two parts — the renewal infection process (which already carries the latent process) and the observation model — into one composed model.

julia
model = IDModel(renewal, obs)
IDModel
├─ infection: Renewal
│  └─ rt: AR
│     └─ ϵ_t: HierarchicalNormal
└─ observation: NegativeBinomialError

Before fitting, the composed model is also a prior simulator: passing missing observations makes as_turing_model return generated quantities — the reported cases generated_y_t, the latent infections I_t, and the latent process Z_t — instead of conditioning on data. That is the mechanism used for the prior checks above; here we go straight to real data.

The data ​

DocumenterCitations.CitationSiteNode("mishra2020derivation-cite-6")

fit this model to daily test-confirmed COVID-19 cases in South Korea over the first wave of 2020. The series is stored with the docs and read with CSV/DataFrames.

julia
using CSV, DataFrames
datapath = joinpath(pkgdir(ComposableTuringIDModels),
    "docs", "src", "tutorials", "data", "south_korea_data.csv")
south_korea = CSV.read(datapath, DataFrame)
first(south_korea, 5)
5×4 DataFrame
RowColumn1datecases_newdeaths_new
Int64DateInt64Int64
112019-12-3100
222020-01-0100
332020-01-0200
442020-01-0300
552020-01-0400

We fit the growth-and-decline window of the first wave, matching the span used by DocumenterCitations.CitationSiteNode("mishra2020derivation-cite-7")

, and take the reported cases over it as the observed series.

julia
tspan = (45, 80)
y_obs = south_korea.cases_new[first(tspan):last(tspan)]
n = length(y_obs)
(n = n, total_cases = sum(y_obs),
    from = south_korea.date[first(tspan)], to = south_korea.date[last(tspan)])
(n = 36, total_cases = 8537, from = Dates.Date("2020-02-13"), to = Dates.Date("2020-03-19"))

Fit ​

Conditioning on the observed counts and sampling with NUTS recovers the posterior. We draw four chains in parallel with MCMCThreads() so the posterior is well resolved and the cross-chain diagnostic is available; the slightly raised target acceptance rate keeps the sampler stable on the hierarchical innovation scale. We differentiate with Mooncake, the recommended backend for this package (see Automatic differentiation backend).

julia
posterior = as_turing_model(model, y_obs, n)
chain = sample(
    posterior, NUTS(0.95; adtype = AutoMooncake(; config = nothing)),
    MCMCThreads(), 250, 4; progress = false)
┌ Info: Found initial step size
└   ϵ = 0.00625
┌ Info: Found initial step size
└   ϵ = 0.00625
┌ Info: Found initial step size
└   ϵ = 0.003125
┌ Info: Found initial step size
└   ϵ = 0.0015625
┌ Warning: There were 8 divergent transitions. Consider reparameterising your model or using a smaller step size. For adaptive samplers such as NUTS and HMCDA, consider increasing `target_accept`.
└ @ Turing.Inference ~/.julia/packages/Turing/4sXd9/src/mcmc/hmc.jl:483
┌ Warning: There were 5 divergent transitions. Consider reparameterising your model or using a smaller step size. For adaptive samplers such as NUTS and HMCDA, consider increasing `target_accept`.
└ @ Turing.Inference ~/.julia/packages/Turing/4sXd9/src/mcmc/hmc.jl:483
┌ Warning: There were 1 divergent transitions. Consider reparameterising your model or using a smaller step size. For adaptive samplers such as NUTS and HMCDA, consider increasing `target_accept`.
└ @ Turing.Inference ~/.julia/packages/Turing/4sXd9/src/mcmc/hmc.jl:483
┌ Warning: There were 14 divergent transitions. Consider reparameterising your model or using a smaller step size. For adaptive samplers such as NUTS and HMCDA, consider increasing `target_accept`.
└ @ Turing.Inference ~/.julia/packages/Turing/4sXd9/src/mcmc/hmc.jl:483

Sampling returns a chain whose parameter names are flat: composition does not namespace by default, so a component's own parameter keeps its plain name however deeply it is nested — damp and std below belong to the AR process folded into the renewal model. A few components deliberately prefix their children, and two components that would otherwise use the same name are separated by wrapping one in PrefixLatentModel or PrefixObservationModel. sample returns a FlexiChains chain, which summarystats summarises directly — no conversion step — giving point estimates and their uncertainty alongside the effective sample size and convergence diagnostic. The autoregressive damping (damp[1]), the innovation scale (std), and the observation cluster factor (cluster_factor) are all identified from the observed South Korean series:

julia
using MCMCChains
summarystats(chain)
╭─FlexiSummary (9 statistics) ─────────────────────────────────────────────────╮
│   iter    collapsed                                                          │
│   chain   collapsed                                                          │
│ ↓ stat  = [mean, std, mcse, ess_bulk, ess_tail, rhat, q5, q50, q95]          │
│                                                                              │
│ Parameters (77) ── AbstractPPL.VarName                                       │
│  Float64  init[1], init[2], damp[1], damp[2], std, ϵ_t[1], ϵ_t[2], ϵ_t[3],   │
│           ϵ_t[4], ϵ_t[5], ϵ_t[6], ϵ_t[7], ϵ_t[8], ϵ_t[9], ϵ_t[10], ϵ_t[11],  │
│           ϵ_t[12], ϵ_t[13], ϵ_t[14], ϵ_t[15], ϵ_t[16], ϵ_t[17], ϵ_t[18],     │
│           ϵ_t[19], ϵ_t[20], ϵ_t[21], ϵ_t[22], ϵ_t[23], ϵ_t[24], ϵ_t[25],     │
│           ϵ_t[26], ϵ_t[27], ϵ_t[28], ϵ_t[29], ϵ_t[30], ϵ_t[31], ϵ_t[32],     │
│           ϵ_t[33], ϵ_t[34], 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],                │
│           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]    0.0327   0.1953  0.0054  1318.9336  638.2407  0.9994  …    │
│        init[2]   -0.0049   0.1893  0.0053  1284.0950  774.7784  1.0010  …    │
│        damp[1]    0.8164   0.0394  0.0016   597.1746  720.1504  1.0012  …    │
│        damp[2]    0.0824   0.0397  0.0030   151.0305   99.9100  1.0016  …    │
│            std    0.4217   0.0475  0.0024   382.3383  599.6918  1.0007  …    │
│         ϵ_t[1]    0.4704   0.9101  0.0769   140.5419  353.7978  1.0140  …    │
│         ϵ_t[2]    0.9153   0.8472  0.0537   250.3780  217.1795  1.0097  …    │
│         ϵ_t[3]    1.1694   0.9107  0.0581   247.1843  393.1262  0.9998  …    │
│         ϵ_t[4]    1.2683   0.8346  0.0528   249.6007  317.0613  1.0036  …    │
│         ϵ_t[5]    2.4202   0.7835  0.0548   206.9908  463.7515  1.0129  …    │
│         ϵ_t[6]    1.6080   0.6483  0.0316   423.4535  625.1361  1.0141  …    │
│         ϵ_t[7]    0.8896   0.5915  0.0281   442.9272  609.3305  1.0007  …    │
│         ϵ_t[8]    0.7256   0.5191  0.0249   436.0263  550.2958  0.9995  …    │
│         ϵ_t[9]   -0.6358   0.4703  0.0207   508.7369  542.5845  0.9998  …    │
│        ϵ_t[10]   -2.2949   0.5117  0.0251   404.3383  470.0993  0.9997  …    │
│        ϵ_t[11]   -1.6968   0.4721  0.0250   391.7451  533.1358  1.0009  …    │
│        ϵ_t[12]    0.5580   0.4164  0.0215   453.3964  275.1040  1.0000  …    │
│        ϵ_t[13]    1.1456   0.3648  0.0139   692.0879  558.1744  1.0085  …    │
│        ϵ_t[14]    0.0085   0.3372  0.0103   973.7937  495.1693  0.9999  …    │
│        ϵ_t[15]    1.2608   0.3895  0.0197   449.7572  380.6159  1.0016  …    │
│        ϵ_t[16]   -1.1290   0.3811  0.0167   532.4324  572.8702  0.9999  …    │
│        ϵ_t[17]   -0.4196   0.3125  0.0131   591.8984  563.1053  0.9994  …    │
│        ϵ_t[18]   -0.7640   0.3236  0.0121   742.7174  592.2178  0.9998  …    │
│        ϵ_t[19]   -0.6902   0.3295  0.0091  1329.6351  688.4688  1.0049  …    │
│        ϵ_t[20]   -0.5694   0.3375  0.0102  1069.8147  464.2191  1.0010  …    │
│        ϵ_t[21]    0.2563   0.3306  0.0115   856.7927  417.3830  1.0009  …    │
│        ϵ_t[22]   -0.0440   0.3115  0.0093  1043.2612  535.6221  1.0024  …    │
│        ϵ_t[23]   -0.5169   0.3115  0.0094  1307.2210  488.6313  1.0010  …    │
│        ϵ_t[24]   -0.8989   0.3458  0.0106  1053.3641  650.0363  0.9992  …    │
│        ϵ_t[25]   -1.3730   0.3946  0.0155   780.7331  308.9912  1.0052  …    │
│        ϵ_t[26]    1.0402   0.4118  0.0238   321.8809  379.6524  1.0005  …    │
│        ϵ_t[27]   -1.1111   0.4133  0.0217   401.1254  315.1895  1.0017  …    │
│        ϵ_t[28]    0.0112   0.4040  0.0140   896.4543  422.4665  1.0037  …    │
│        ϵ_t[29]    0.1911   0.4056  0.0147   706.6920  667.2825  0.9996  …    │
│        ϵ_t[30]   -0.3428   0.4368  0.0161   716.6177  547.7746  1.0024  …    │
│        ϵ_t[31]    0.1766   0.4169  0.0166   631.0187  588.1796  1.0037  …    │
│        ϵ_t[32]    0.6200   0.4218  0.0175   576.6737  655.2172  0.9997  …    │
│        ϵ_t[33]    0.6263   0.4039  0.0124  1079.7545  700.5190  0.9994  …    │
│        ϵ_t[34]    1.2891   0.3887  0.0177   536.2629  388.4792  1.0003  …    │
│   init_incide…   -0.0337   0.0969  0.0031   969.3816  770.2435  1.0039  …    │
│     exp_I_t[1]    0.9815   0.1083  0.0033  1116.4578  871.1598  1.0019  …    │
│     exp_I_t[2]    0.9717   0.2189  0.0071   926.7697  704.8690  1.0018  …    │
│     exp_I_t[3]    1.2444   0.4711  0.0368   159.9402  466.6843  1.0093  …    │
│     exp_I_t[4]    1.7604   0.5812  0.0397   217.1432  396.8190  0.9992  …    │
│     exp_I_t[5]    2.7694   0.8913  0.0505   323.3858  498.1685  0.9995  …    │
│     exp_I_t[6]    4.7421   1.3789  0.0856   256.7200  486.2945  1.0054  …    │
│     exp_I_t[7]   14.3297   3.0333  0.1316   525.6397  757.7559  1.0061  …    │
│     exp_I_t[8]   33.3458   5.4702  0.2267   568.0459  743.4487  1.0004  …    │
│     exp_I_t[9]   74.5769   9.7179  0.3142   984.3190  564.3061  0.9991  …    │
│    exp_I_t[10]  177.6928  20.6528  0.9169   540.1628  509.2955  0.9999  …    │
│    exp_I_t[11]  241.7004  26.3142  1.2002   506.7630  444.0883  1.0009  …    │
│    exp_I_t[12]  169.3317  21.0334  0.7526   962.6017  539.7947  1.0001  …    │
│    exp_I_t[13]  146.9356  24.6581  1.5849   363.2578  304.1865  1.0003  …    │
│    exp_I_t[14]  261.0028  28.4083  0.9390  1027.4587  508.4089  1.0022  …    │
│    exp_I_t[15]  444.1218  43.2128  1.2755  1100.2670  724.8120  1.0024  …    │
│    exp_I_t[16]  439.9598  49.4893  1.5248  1164.0226  539.8307  1.0006  …    │
│    exp_I_t[17]  867.1103  85.6103  4.8314   378.5267  343.1943  0.9995  …    │
│    exp_I_t[18]  608.2889  60.0856  2.2968   824.7354  593.5072  0.9994  …    │
│    exp_I_t[19]  683.0606  64.2500  1.9230  1019.4653  586.8943  1.0007  …    │
│    exp_I_t[20]  606.9473  59.3885  1.7438  1398.6797  656.4993  1.0005  …    │
│    exp_I_t[21]  524.7966  55.2325  1.5617  1480.2891  672.4439  1.0005  …    │
│    exp_I_t[22]  448.2208  47.5068  1.6347   986.0980  409.0317  1.0017  …    │
│    exp_I_t[23]  514.8131  52.6011  1.5654  1095.6651  594.2782  1.0038  …    │
│    exp_I_t[24]  476.0046  47.4940  1.4318  1059.6695  415.6256  1.0030  …    │
│    exp_I_t[25]  364.2985  35.4937  1.1121  1117.5489  591.3477  0.9999  …    │
│    exp_I_t[26]  248.7649  27.6962  0.7776  1349.8069  753.7969  1.0016  …    │
│    exp_I_t[27]  144.2362  20.9399  1.1489   482.3513  288.0221  1.0036  …    │
│    exp_I_t[28]  229.6799  24.6351  1.1876   468.4226  377.1180  1.0016  …    │
│    exp_I_t[29]  119.8976  16.1560  0.7497   536.1246  601.9125  1.0048  …    │
│    exp_I_t[30]  112.0230  15.0774  0.4360  1206.8928  593.1025  0.9995  …    │
│    exp_I_t[31]  106.4992  13.7447  0.4015  1185.5218  826.4152  0.9998  …    │
│    exp_I_t[32]   79.4214  11.2117  0.4354   670.7275  582.5554  1.0015  …    │
│    exp_I_t[33]   76.2271  10.5902  0.3394   897.1041  847.1726  1.0009  …    │
│    exp_I_t[34]   86.2739  11.1679  0.4180   827.3069  462.1464  0.9992  …    │
│    exp_I_t[35]   96.0834  12.5996  0.4977   849.3457  499.5549  0.9997  …    │
│    exp_I_t[36]  145.2462  17.5090  0.6007   880.0039  379.4282  1.0035  …    │
│   cluster_fac…    0.0832   0.0587  0.0057    59.3400  161.2966  1.0109  …    │
╰──────────────────────────────────────────────────────────────────────────────╯

Prior versus posterior ​

Before reading the trajectories it is worth asking what the data taught us. Sampling the same model with Prior — ignoring the observations — gives a prior draw over the same parameters, and overlaying it on the posterior shows which parameters moved. We load a Makie backend and PairPlots.jl; the FlexiChains PairPlots extension turns a chain (subset to a few keys with chain[[...]]) into a PairPlots.Series, so prior and posterior overlay on one corner plot.

julia
using CairoMakie, PairPlots

prior_chain = sample(posterior, Prior(), 1000; progress = false)
pp_keys = [@varname(damp), @varname(std),
    @varname(cluster_factor), @varname(init_incidence)]
pairplot(
    PairPlots.Series(chain[pp_keys]; label = "posterior"),
    PairPlots.Series(prior_chain[pp_keys]; label = "prior"))

The innovation scale (std) is sharply updated away from its prior — the data are informative about how much wiggles — while the autoregressive damping (damp), the cluster factor and the initial infections stay closer to their priors on this short window.

Posterior trajectories ​

The reproduction number   is a generated quantity rather than a sampled parameter. generated_observables re-runs the fitted model over the chain to recover the latent and infection trajectories per draw. The reported counts are scored element-wise, so their posterior predictive distribution — fresh counts drawn under each posterior parameter set — comes from predict on the same model with the observations set to missing.

A couple of small helpers reduce the per-draw trajectories to credible bands and draw a median line with 50% and 95% ribbons.

Stack the per-draw into an band, draw the posterior-predictive from the unconditioned model, and plot both against the observed series:

julia
gens = vec(generated_observables(posterior, y_obs, chain).generated)
Rt = credible_bands(reduce(hcat, (exp.(g.Z_t) for g in gens)))

pred = predict(as_turing_model(model, fill(missing, n), 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 = "Reported 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 posterior-predictive band tracks the observed South Korean series closely, and the path recovers the first-wave turn-over: an early rise well above one, a fall through   as the wave peaks, and a decline below one as cases drop.

Forecasting the next weeks ​

The same fitted model forecasts out of sample in one call. Because the latent AR process is non-centred, forecast carries each posterior draw forward — holding the fitted parameters and the in-sample path fixed, and continuing the process over the horizon with fresh prior innovations — then draws the future reported cases:

julia
h = 14
fc = forecast(model, y_obs, chain, h)
size(fc)
(250, 4)

The returned chain carries the predicted over    . We can then plot these forecasts:

julia
# Multi-level CI band quantiles: 90%, 60%, 30% + median
FC_CI_QS = [0.05, 0.2, 0.35, 0.5, 0.65, 0.8, 0.95]

# Draw three nested CI ribbons with the median line
function multi_ci_ribbon!(ax, ts, bands; color, label)
    keep = findall(!ismissing, view(bands, :, 4))
    x, b = ts[keep], Float64.(bands[keep, :])
    # 90% CI (cols 1, 7)
    band!(ax, x, b[:, 1], b[:, 7]; color = (color, 0.1))
    # 60% CI (cols 2, 6)
    band!(ax, x, b[:, 2], b[:, 6]; color = (color, 0.25))
    # 30% CI (cols 3, 5)
    band!(ax, x, b[:, 3], b[:, 5]; color = (color, 0.5))
    # median (col 4)
    lines!(ax, x, b[:, 4]; color = color, linewidth = 2, label = label)
end

# Multi-level credible bands for the forecast
fc_bands = credible_bands(reduce(vcat,
    (permutedims(vec(fc[@varname(y_t[i])])) for i in (n + 1):(n + h)));
    qs = FC_CI_QS)

# Sample 100 random forecast trajectories
n_draws = length(vec(fc[@varname(y_t[n + 1])]))
ntraj = min(100, n_draws)
sample_idx = rand(1:n_draws, ntraj)
trajectories = reduce(hcat,
    [[vec(fc[@varname(y_t[i])])[idx] for i in (n + 1):(n + h)] for idx in sample_idx])

fig_fc = Figure(; size = (760, 360))
axf = Axis(fig_fc[1, 1]; xlabel = "Day", ylabel = "Reported cases",
    yscale = log10)
scatter!(axf, 1:n, y_obs; color = :black, markersize = 7, label = "observed")
# Faint individual forecast trajectories
for idx in 1:ntraj
    lines!(axf, (n + 1):(n + h), max.(trajectories[:, idx], 1);
        color = (:teal, 0.08), linewidth = 0.5)
end
# Multi-level CI bands (offset to keep log10 scale safe at zero)
multi_ci_ribbon!(axf, (n + 1):(n + h), max.(fc_bands, 1); color = :teal,
    label = "forecast")
vlines!(axf, [n + 0.5]; color = :grey, linestyle = :dash)
axislegend(axf; position = :lt)
fig_fc

Swap a component ​

Because the parts share one interface, an alternative observation assumption is a one-line change. Swapping the negative-binomial reporting for a PoissonError leaves the renewal infection process — and its latent process — untouched:

julia
poisson_model = IDModel(renewal, PoissonError())
length(rand(as_turing_model(poisson_model, fill(missing, n), n)))
41

References ​

  1. A. Cori, N. M. Ferguson, C. Fraser and S. Cauchemez. A new framework and software to estimate time-varying reproduction numbers during epidemics. American Journal of Epidemiology 178, 1505–1512 (2013).

  2. S. Mishra, T. Berah, T. A. Mellan, H. J. Unwin, M. A. Vollmer, K. V. Parag, A. Gandy, S. Flaxman and S. Bhatt. On the derivation of the renewal equation from an age-dependent branching process: an epidemic modelling perspective, arXiv preprint arXiv:2006.16487 (2020).

  3. K. Charniga, S. W. Park, A. R. Akhmetzhanov, A. Cori, J. Dushoff, S. Funk and others. Best practices for estimating and reporting epidemiological delay distributions of infectious diseases. PLoS Computational Biology 20, e1012520 (2024).