Skip to content

ComposableRecurrences ​

Reactant

Fast, composable and differentiable recurrences and convolutions in Julia.

Why ComposableRecurrences? ​

  • A recurrence computes each value from its own past values, such as infections from earlier infections. A convolution weights the past values of another series, such as reported cases from earlier infections.

  • Both are usually hand-written loops, rewritten for each model. Recurrence and Convolution express them as two operators, for one series or for many strata, such as towns, run side by side.

  • Mixing between strata, finite pools and caps are added by passing couplings and modifiers to an operator.

  • Operators run on CPU threads and on GPU arrays through KernelAbstractions.jl. On Julia 1.12 and later, a forward run compiles into a trimmed executable with juliac.

  • The aim is to be as fast as hand-written code with hand-written gradients. Against a hand-written loop of each documented model, the package's Mooncake and Enzyme gradients are a median 4.1× and 2.4× faster than the same backend on the loop, comparing minimum times. The forward run and ForwardDiff gradient run at a median 1.2× and 1.1× the loop's speed, and are slower on several models. These figures are from revision 48f2396; see the benchmarks for each model and the slower cases.

Getting started ​

Three towns share an outbreak. Infections in each town come from earlier infections in that town and, through travel, in the other towns. Each town has a finite number of people who can be infected. A share of infections is reported after a delay. An intervention from day 50 lowers the reproduction number in every town.

where is the number of infections in town on day , the number of reports and the reproduction number. The value is the number of new infections before depletion, and the susceptible pool starts at the population . The generation interval has weight on lag over lags, the reporting delay has weight over lags, and a share   of infections is reported. The gravity coupling comes from the populations and the distances between towns, with  . The term is 1 when   and 0 otherwise. Town A has 10 infections on each of the six days before day 1, and the other towns none. The renewal and the delay both read these as their history, so the reports from day 1 include them. In the code, PerStratum(pop) gives each town its own population, and the run covers   days.

julia
using ComposableRecurrences
using ComposableRecurrences: Depletion

gi = [0.05, 0.2, 0.3, 0.25, 0.12, 0.08]
T = 75
towns = ["A", "B", "C"]
pop = [60_000.0, 25_000.0, 10_000.0]
dist = [0.0 20.0 45.0; 20.0 0.0 30.0; 45.0 30.0 0.0]
gravity = [a == b ? 0.0 : pop[b] / dist[a, b]^2 for a in 1:3, b in 1:3]
K = 0.998 * [a == b for a in 1:3, b in 1:3] + 0.002 * gravity ./ sum(gravity; dims = 2)
seed = [fill(10.0, 1, 6); zeros(2, 6)]
R = [1.8 - clamp((t - 50) / 6, 0, 1) for _ in towns, t in 1:T]

renewal = Recurrence(gi; coupling = K, modifiers = (Depletion(PerStratum(pop)),))
delay = Convolution([0.0, 0.1, 0.25, 0.3, 0.2, 0.1, 0.05])
infections = renewal(R; history = seed)
reports = delay(infections; gain = 0.4, history = seed)

Infections, reports and the remaining susceptible share in each town, with the intervention from day 50 to 56 shaded

Getting started guide

Where to learn more ​

Part of the EpiAware ecosystem ​

ComposableRecurrences is part of EpiAware, a set of composable tools for infectious disease modelling. See the other packages in the ecosystem.

Contributing ​

We welcome contributions and new contributors! Please open an issue or pull request on GitHub. This package follows ColPrac and is formatted with Runic.

How to cite ​

If you use ComposableRecurrences in your work, please cite it. Citation metadata lives in CITATION.cff, which GitHub renders as a "Cite this repository" button on the repository page.

Code of conduct ​

Please note that the ComposableRecurrences project is released with a Contributor Code of Conduct. By contributing, you agree to abide by its terms.