Skip to content

API overview ​

This page defines each concept of the package once. Every other page uses these terms as defined here and links back to this page. Each entry names the types of its concept and links the page that covers it.

using ComposableRecurrences exports Recurrence, Convolution, TimeVarying, PerStratum and Pairwise. Every other public name is loaded by name, as in using ComposableRecurrences: Depletion, Floor, Recent, and is written here without the package prefix.

The concepts in one model ​

The model is that of Three towns, step by step, which states its equations. gi is a generation interval, pop the population of each town, K a gravity coupling, seed the infections before day 1 and R the reproduction number.

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

renewal and delay are operators, and the three towns are strata. gi and the vector of delay weights are kernels, K is a coupling and Depletion(PerStratum(pop)) is a modifier. PerStratum is a shape, R and 0.4 are gains and seed is a history. The two lines that apply an operator are calls, and each returns a matrix of 3 strata by 75 steps.

How kernels, couplings, modifiers, shapes, operators, calls and executors fit together

Operator ​

An operator is an object built once from its coefficients and then called on data. A Recurrence computes each output from its own past outputs, as a renewal process does. A Convolution computes each output from the past values of an input series, as a reporting delay does. Building an operator lists the keywords of each constructor.

Step ​

A step is one unit of model time, an integer counted from 1. Both operators compute one output per stratum at each step. A step gives the equations of both operators, and Time units covers data and delays in units other than the step.

Stratum ​

A stratum is one of series that an operator computes side by side, such as a town or an age group. A series with strata is an   matrix with a row for each stratum and a column for each step, and a single series is a vector. Data shapes gives the shape each input takes.

Kernel ​

A kernel is a vector of weights on lags, where lag is the value steps before the current one. A generation interval is the kernel of a renewal process, and a reporting delay the kernel of a convolution. The kernel of a recurrence starts at lag 1 and that of a convolution at lag 0, as A recurrence is a convolution of its own outputs explains.

Delay distribution ​

A delay distribution is a probability distribution of a delay, passed in place of the kernel weights with the number of lags. The operator takes the probability of each whole lag as the kernel. Delay distributions as kernels gives the calculation and the packages to load.

History ​

The history is the series before the first step of a call, oldest first. A recurrence reads its own earlier outputs from the history, and a convolution its earlier inputs. History and state covers what a history must hold, and Seeding how to build one that starts an outbreak.

Gain and add ​

The gain multiplies the weighted sum of a step, as the reproduction number does in a renewal process. The add input is added after the gain, as imported infections are. Both are data passed to a call, not coefficients held by the operator.

Coupling ​

A coupling is an   matrix that mixes the strata within a step of a recurrence, after the kernel and before the gain. Entry is the weight of the sending stratum in the receiving stratum , and the default I applies no mixing. Batched(K) applies one such matrix within each batch of strata. Where the coupling acts lists the coupling types and states which stratum a per-stratum coefficient belongs to.

Modifier ​

A modifier is a map applied to the value of each step of a recurrence, after the gain and add. A modifier can hold state that it carries from one step to the next. The built-in modifiers are:

  • Depletion, which draws each value from a finite pool such as the susceptible population;

  • Redistribute, which moves a share of the value of each stratum to other strata;

  • Allocate, which rescales groups of strata to given totals;

  • Transform, which applies a function such as an addition or a cap.

Modifiers act in the order given, and Order gives a case where two orders differ. Built-in modifiers describes each one.

Pool and depletion form ​

A pool is a quantity that a Depletion carries from step to step, such as the number of susceptible people. A depletion form is the rule by which a pool gives up the value that a step asks of it. The forms are Hazard, the default, Floor and Truncate. Depletion forms gives the equation of each.

Shape ​

A coefficient is a number held by an operator: a kernel weight, a coupling weight or a parameter of a modifier. The shape of a coefficient is how it varies across strata and steps. A plain array is shared by every stratum and step. PerStratum gives each stratum its own value, Pairwise each pair of strata and TimeVarying each step. Gather reads one value for each group of strata, as Parameters shared across strata describes. A parameter of your own is a subtype of AbstractParameter, as A parameter of your own shows. Slots and shapes states which wrapper each coefficient takes.

Call ​

A call applies an operator from step start to step stop and returns its outputs. The record keyword adds to what a call returns:

  • Resume() adds the state from which a later call continues the run;

  • Pressure() adds the pressure, the coupled weighted sum of each step before the gain;

  • Trace() adds what each modifier recorded.

select(state, strata) reorders, repeats or drops the strata of a state before the next call. Calls lists every call form with what it returns.

Time-series process ​

A random walk and an autoregression are recurrences with innovations as the add input, and a moving average is a convolution of innovations. Time-series processes writes each as an operator.

Executor ​

An executor sets where the loops of a call run. Serial is the default, Threaded splits loops across CPU threads and Parallel runs them as kernels on a device such as a GPU. Executors states when each applies.

Rule ​

A rule is a reverse pass written for an operator, which an automatic differentiation backend runs in place of differentiating the loop of the operator. Every call is differentiable with or without its rule. Rules and plain AD states when a call takes its rule.

New types ​

A new coupling, modifier or depletion form is a type with one or two methods. Writing new types gives a worked example of each.