ComposableRecurrences
Composable and differentiable recurrences and convolutions in Julia.
Why ComposableRecurrences?
Many models can be expressed as discrete-time recurrences, where each value depends on earlier values, and convolutions, where a delay spreads one series into another.
For example, a renewal process for infectious disease transmission is a recurrence, and the delay from infection to report is a convolution.
Instead of writing these by hand, we implement them as a set of composable operators.
These allow easy extension to age groups, regions, a finite population or a delay that changes over time.
We focus on efficiency, both for forward computation and on all modern automatic differentiation backends, with the aim of being on par with or beating hand-written gradients.
We also aim to enable efficient computation on both CPU and GPU in a range of contexts.
Getting started
The example is a renewal model of an outbreak in three towns. Infections in each town arise from earlier infections in that town and, through travel, in the other two. Each town has a finite population. A share of infections is reported after a delay. The reproduction number falls in every town between days 50 and 56. The model is
where history, so the reports from day 1 include them. In the code, PerStratum(pop) gives each town its own population, and the run covers
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)
Three towns, step by step builds this model one part at a time.
Related packages
ComposableTuringIDModels.jl builds infectious disease models from renewal, delay and latent process components.
ConvolvedDistributions.jl builds convolutions of distributions, such as the total of two independent delays.
CensoredDistributions.jl discretises delay distributions into the probability mass functions these operators take as kernels.
Where to learn more
The tutorials overview lists the tutorials, each of which builds one model.
The API overview defines each concept of the package.
The performance page gives timings against hand-written loops and gradients.
The extensions table lists what each optional package adds.
The developer documentation shows how to write a new modifier or coupling.
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.