Estimating the reproduction number from overdispersed counts
Introduction
Conjugate gamma–Poisson estimation estimates
This page is for readers whose counts are overdispersed. It keeps the model, the windows and the two sums of the first page, and changes only the distribution of each count. It simulates one overdispersed outbreak and shows how far the gamma posterior falls short. It then gives two intervals that allow for the extra variation. One is an exact posterior when the dispersion is known. The other is a confidence interval that estimates the dispersion from the data []. The last section shows how often each interval contains the true value as the dispersion grows.
It assumes you have read Conjugate gamma–Poisson estimation.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Transform, pressure, window_sums
using Distributions: BetaPrime, Gamma, NegativeBinomial, Normal, cdf,
mean, quantile, rate, shape
using Random, Statistics
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
CairoMakie.activate!(type = "png", px_per_unit = 2)Functions from the first page
These are the window, posterior and plotting functions of the first page, repeated so this page runs on its own.
# Windows of `width` days that follow one another over `days`.
function consecutive(width, days)
starts = first(days):width:(last(days) - width + 1)
return (starts = starts, stops = starts .+ (width - 1))
end
# The sums of the counts `y` and of the multipliers `E` over each window.
function window_totals(y, E, w)
return (
cases = window_sums(y, w.starts, w.stops),
pressure = window_sums(E, w.starts, w.stops),
)
end
# The gamma posterior of each window, from a gamma prior and the totals.
function gamma_posterior(prior, totals)
return Gamma.(
shape(prior) .+ totals.cases,
1 ./ (rate(prior) .+ totals.pressure)
)
end
# The mean and central 95% interval of each posterior, one for each day.
function summarise(posterior, days; series = "Posterior")
return DataFrame(
day = collect(days), mean = mean.(posterior),
lower = quantile.(posterior, 0.025),
upper = quantile.(posterior, 0.975), series = series
)
end
# A line and a band by day for each series, with `truth` dashed in black.
function plot_posterior(df, truth)
order = sorter(unique(df.series))
by = mapping(color = :series => order => "", col = :series => order)
estimate = data(df) * by * (
mapping(:day, :lower, :upper) * visual(Band, alpha = 0.3) +
mapping(:day, :mean) * visual(Lines, linewidth = 2)
)
reference = data(truth) * mapping(:day, :R) *
visual(Lines, color = :black, linestyle = :dash)
axis = (; xlabel = "Day", ylabel = "Reproduction number")
return draw(
estimate + reference; axis, legend = (show = false,),
figure = (size = (640, 300),)
)
endAn overdispersed outbreak
Suppose that each unit of pressure causes a Poisson number of infections, whose rate is gamma distributed with mean
with mean
As on the first page, each count is the quantile at a uniform draw Transform modifier reads both
# A count with mean `v` and size `κ v / R`, drawn at the uniform `θ.u`.
function nb_count(κ)
return function (v, θ)
v > 0 || return 0.0
count = NegativeBinomial(κ * v / θ.R, κ / (κ + θ.R))
return float(quantile(count, θ.u))
end
end
per_day(x::AbstractVector) = TimeVarying(x)
per_day(x::AbstractMatrix) = TimeVarying(PerStratum(x))
# A modifier that draws an overdispersed count from each value.
function nb_counts(κ, u, R)
return Transform(nb_count(κ), (u = per_day(u), R = per_day(R)))
endThe generation interval,
gi = [0.05, 0.2, 0.3, 0.25, 0.12, 0.08]
L = length(gi)
T = 70
R = [t <= 28 ? 1.6 : t <= 49 ? 0.8 : 1.2 for t in 1:T]
seed = fill(10.0, L)
κ = 0.5
renewal = Recurrence(
gi; modifiers = (nb_counts(κ, rand(Xoshiro(1), T), R),)
)
incidence = renewal(R; history = seed)
truth = DataFrame(day = 1:T, R = R)
@chain DataFrame(day = 1:T, incidence = incidence) begin
data(_) * mapping(:day, :incidence) * visual(Scatter, color = :black)
draw(
_; axis = (xlabel = "Day", ylabel = "Incidence"),
figure = (size = (640, 300),)
)
endThe counts follow the three levels of
A posterior for known dispersion
With
where
Why the posterior is beta prime
The probability
the kernel of a beta density in
# The posterior of each window for counts whose size is `κ` times the
# pressure, from the totals and the parameters of the limiting gamma prior.
function beta_prime_posterior(prior, totals, κ)
return κ .* BetaPrime.(
shape(prior) .+ totals.cases,
κ .* (rate(prior) .+ totals.pressure)
)
endThe windows are the ten weeks of the run, with the prior of the first page. The figure sets the gamma posterior, which treats the counts as Poisson, against the beta prime posterior.
prior = Gamma(1, 5)
weeks = consecutive(7, 1:T)
week_of_day = repeat(1:length(weeks.starts); inner = 7)
Λ = pressure(renewal, incidence; history = seed)
totals = window_totals(incidence, Λ, weeks)
as_poisson = gamma_posterior(prior, totals)
known_κ = beta_prime_posterior(prior, totals, κ)
fits = vcat(
summarise(as_poisson[week_of_day], 1:T; series = "Gamma (Poisson)"),
summarise(known_κ[week_of_day], 1:T; series = "Beta prime")
)
plot_posterior(fits, truth)The line is the posterior mean, the band the central 95% interval and the dashed line the simulating
An interval for unknown dispersion
The dispersion is rarely known. With
The quasi-Poisson model keeps the mean
where
# The quasi-Poisson estimate of each window and its 95% interval.
function quasi_poisson(y, E, w)
totals = window_totals(y, E, w)
estimate = totals.cases ./ totals.pressure
ratio = window_sums(
ifelse.(E .> 0, y .^ 2 ./ E, 0.0), w.starts, w.stops
)
n = (w.stops .- w.starts .+ 1)'
ϕ = (ratio ./ estimate .- totals.cases) ./ (n .- 1)
se = estimate .* sqrt.(ϕ ./ totals.cases)
return (;
estimate, lower = estimate .- 1.96 .* se,
upper = estimate .+ 1.96 .* se,
)
endThe figure sets the quasi-Poisson interval of each week against the beta prime posterior.
quasi = quasi_poisson(incidence, Λ, weeks)
quasi_fit = DataFrame(
day = 1:T, mean = vec(quasi.estimate)[week_of_day],
lower = vec(quasi.lower)[week_of_day],
upper = vec(quasi.upper)[week_of_day], series = "Quasi-Poisson"
)
beta_prime_fit = summarise(known_κ[week_of_day], 1:T; series = "Beta prime")
plot_posterior(vcat(quasi_fit, beta_prime_fit), truth)The quasi-Poisson interval has about the width of the beta prime interval, without knowing
How often each interval contains the true value
One outbreak shows the widths, not how often each interval is right. The calibration check of the first page answers this by simulation. Here it is repeated for several values of
[] show that a Poisson interval understates the standard error by the factor
where
# The share of intervals from `lower` to `upper` that contain `value`.
coverage(lower, upper, value) = mean(lower .<= value .<= upper)
# The coverage of each interval for 2000 outbreaks with shape `κ`.
function coverage_at(κ; R = 1.5, n = 2000, days = 21)
u = rand(Xoshiro(round(Int, 100κ)), n, days)
count = nb_counts(κ, u, fill(R, n, days))
outbreaks = Recurrence(gi; modifiers = (count,))
seeds = fill(20.0, n, L)
y = outbreaks(fill(R, n, days); history = seeds)
Λ = pressure(outbreaks, y; history = seeds)
window = (starts = [8], stops = [days])
totals = window_totals(y, Λ, window)
gamma = gamma_posterior(prior, totals)
beta_prime = beta_prime_posterior(prior, totals, κ)
quasi = quasi_poisson(y, Λ, window)
interval(d, p) = quantile.(d, p)
return DataFrame(
κ = κ,
interval = ["Gamma (Poisson)", "Beta prime", "Quasi-Poisson"],
coverage = [
coverage(interval(gamma, 0.025), interval(gamma, 0.975), R),
coverage(
interval(beta_prime, 0.025), interval(beta_prime, 0.975), R
),
coverage(quasi.lower, quasi.upper, R),
]
)
end
shapes = [0.25, 0.5, 1, 2, 5, 20]
coverages = reduce(vcat, coverage_at.(shapes))
predicted = DataFrame(κ = exp.(range(log(0.2), log(25); length = 100)))
ϕ = 1 .+ 1.5 ./ predicted.κ
predicted.coverage = 1 .- 2 .* cdf.(Normal(), -1.96 ./ sqrt.(ϕ))
draw(
data(coverages) *
mapping(:κ, :coverage, color = :interval => "") *
visual(ScatterLines, markersize = 10) +
data(predicted) * mapping(:κ, :coverage) *
visual(Lines, color = :grey, linestyle = :dot) +
mapping([0.95]) * visual(HLines, color = :black, linestyle = :dash);
axis = (
xlabel = "Shape κ (smaller is more overdispersed)",
ylabel = "Coverage of the 95% interval", xscale = log10,
xticks = (shapes, ["0.25", "0.5", "1", "2", "5", "20"]),
),
figure = (size = (640, 320),)
)The dashed line is the nominal 0.95 and the dotted line the coverage predicted for the Poisson interval. The gamma posterior falls well short as the counts become more overdispersed, close to the prediction. The beta prime posterior, which knows
Other count models
Other negative binomial models, with the size proportional to the mean or constant, have no beta prime posterior. [] fit them by maximum likelihood and compare their intervals with the quasi-Poisson one. Models that need no closed form, such as a negative binomial count with unknown dispersion, can be fitted with particle filtering or MCMC.
References
B. Němcová, I. H. Goldstein, J. Sebastian, V. M. Minin and J. Bracher. Unjustified Poisson assumptions lead to overconfident estimates of the effective reproductive number. Epidemiology and Infection 154, e79 (2026).
K. D. Johnson, M. Beiglböck, M. Eder, A. Grass, J. Hermisson, G. Pammer, J. Polechová, D. Toneian and B. Wölfl. Disease momentum: Estimating the reproduction number in the presence of superspreading. Infectious Disease Modelling 6, 706–728 (2021).
P. McCullagh and J. A. Nelder. Generalized Linear Models. 2 Edition (Springer US, 1989).