Skip to content

Time units ​

A model has one time unit, its step.

julia
using ComposableRecurrences, Distributions, CensoredDistributions
using ComposableRecurrences: Transform
gi = Gamma(2.0, 2.5)   # a generation interval in days, mean 5 days
Gamma distribution
Parameters:
  α = 2.0
  θ = 2.5

The step is the unit ​

A Recurrence steps once per index:

where counts steps and weights the value steps back. The kernel, the history, 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 is the delay in its own unit and is the step in that unit. is the 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.

julia
Recurrence(gi; lags = 15).kernel ≈ ComposableRecurrences.delay_masses(gi, 15)[2:end]
true

A 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 and :

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    is one continuous delay, censored once:

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:

julia
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.002

The 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:

julia
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.1

A 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:

julia
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:

julia
weekly = vec(sum(reshape(y, 7, :); dims = 1))
3-element Vector{Float64}:
 34.62726717720772
 41.38696186821917
 28.99897813333848

Data coarser than the step, driving ​

A weekly series that drives a daily model, such as a seed or an input , needs daily values with

There are three choices.

  1. Spread it uniformly,  . This costs nothing, but puts jumps at the week boundaries that the data do not show.

  2. 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.

  3. 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:

julia
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.0

The third steps by week, with the delays censored on weeks:

julia
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, , is left out. With a generation interval shorter than the step much of the mass lies there. Here the kernel sums to about 0.6, so the weekly model drops the 40% of transmission that happens within the week of infection.

Steps finer than the data's unit ​

A model on quarter days with delays in days has  . The kernel's lags count steps, not days, so 10 days of a delay are 40 lags:

julia
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.