Time units
A model has one time unit, its step.
using ComposableRecurrences, Distributions, CensoredDistributions
using ComposableRecurrences: Transform
gi = Gamma(2.0, 2.5) # a generation interval in days, mean 5 daysGamma distribution
Parameters:
α = 2.0
θ = 2.5The step is the unit
A Recurrence steps once per index:
where gain and add all have one value per step. A daily model reads each of them by day.
A delay gives its masses on whole steps of its own unit:
where step keyword of delay_masses, and Recurrence and Convolution pass it on. It is 1 by default, so a delay in days gives masses on days.
Recurrence(gi; lags = 15).kernel ≈ ComposableRecurrences.delay_masses(gi, 15)[2:end]trueA discrete distribution sums the masses of the whole units in each step, as in the formula above. A continuous one also takes the delay's start as uniform within its step and censors its end to whole steps, so four quarter-day masses do not sum to the daily mass.
Delays in sequence
A delay made of two delays in sequence, such as incubation then onset to report, has two forms. Each is a different model.
When each delay rounds to whole steps before the next one starts, the chain's masses are the full linear convolution of the two delays' masses
A tuple of delays gives this, through the delay_masses method for a tuple. It also composes delays that are already masses, such as fitted PMFs.
When the delays add in continuous time, the sum
Compose the distributions as distributions, for example with ConvolvedDistributions, and pass the sum as one delay, whose masses come from the CensoredDistributions extension.
A tuple of two continuous delays is the first form: each delay is censored on its own, then the masses are convolved. The two forms give different masses, since each censored delay carries its own rounding.
Two gamma delays with the same scale sum to a gamma delay, so the two forms can be compared directly:
chained = Convolution((Gamma(5.0, 1.0), Gamma(2.0, 1.0)); lags = 20).kernel
summed = Convolution(Gamma(7.0, 1.0); lags = 20).kernel
round(maximum(abs, chained .- summed); digits = 3)0.002The difference is the rounding that each separately censored delay carries.
Parameters coarser than the step
A weekly reproduction number in a daily model holds each week's value over the days of that week:
The gain is data, one value per step, so it is the weekly values repeated:
Rw = [1.4, 1.1, 0.8]
R = repeat(Rw; inner = 7)
R[6:9]4-element Vector{Float64}:
1.4
1.4
1.1
1.1A kernel, coupling or modifier parameter that changes at a few times is given by its change points, [t => value] in TimeVarying. Each value holds from its start until the next. Here imports fall each week:
imports = Transform(+, TimeVarying([1 => 2.0, 8 => 0.5, 15 => 0.0]))
renewal = Recurrence(gi; lags = 15, modifiers = (imports,))
y = renewal(R; history = ones(14))Data coarser than the step, observed
Weekly counts of a daily model are the sums of its days:
Keep the model daily and sum its outputs in the likelihood:
weekly = vec(sum(reshape(y, 7, :); dims = 1))3-element Vector{Float64}:
34.62726717720772
41.38696186821917
28.99897813333848Data coarser than the step, driving
A weekly series that drives a daily model, such as a seed or an input
There are three choices.
Spread it uniformly,
. This costs nothing, but puts jumps at the week boundaries that the data do not show.Infer the daily series as a latent process, with the weekly sums as its likelihood. This is the most faithful choice, and costs a latent series to fit.
Step at the data's cadence, with the delays censored on weeks through
step. This needs no daily series, but loses the timing within a week.
The first is one line:
w = [70.0, 140.0, 210.0]
x = repeat(w ./ 7; inner = 7)
x[6:9]4-element Vector{Float64}:
10.0
10.0
20.0
20.0The third steps by week, with the delays censored on weeks:
weekly_renewal = Recurrence(gi; lags = 4, step = 7)
k = weekly_renewal.kernel
round.(k; digits = 3), round(sum(k); digits = 2)([0.516, 0.085, 0.008], 0.61)A Recurrence kernel starts at lag 1, so the mass within one step,
Steps finer than the data's unit
A model on quarter days with delays in days has lags count steps, not days, so 10 days of a delay are 40 lags:
delay = Convolution(LogNormal(1.5, 0.75); lags = 40, step = 0.25)
length(delay.kernel), round(sum(delay.kernel); digits = 2)(40, 0.85)The history, gain and add are then given per quarter day too.