Skip to content

Operators ​

A recurrence reads its own past outputs and a convolution an input series; the API overview shows where they fit.

julia
using ComposableRecurrences
using ComposableRecurrences: exponential_history, Depletion

A step ​

Recurrence and Convolution compute their outputs one step at a time, with time the integer step counted from 1 in the model's time unit. Each series run side by side, such as one per town, is a stratum. A convolution has no coupling and no modifiers.

One recurrence step and one convolution output

A call runs from step start to step stop. For each step of that range, a recurrence computes

and a convolution computes

where is the output, one entry per stratum, the input of a convolution and the weighted sum of past outputs. The kernel has weight on lag over lags, is the coupling, the gain and the add input. The value before the modifiers , which act in order, is , and multiplies element by element. For a recurrence the history holds the outputs before start, and for a convolution the inputs before time 1. Where the coupling acts writes the step stratum by stratum, and Order writes the modifiers' part.

Building an operator ​

Each operator is built once from a kernel and then called like a function.

julia
delay = Convolution([0.0, 0.1, 0.25, 0.3, 0.2, 0.1, 0.05])
delay([10.0, 20.0, 30.0])
3-element Vector{Float64}:
 0.0
 1.0
 4.5

The constructors are Recurrence(kernel; lags, step, coupling, modifiers) and Convolution(kernel; lags, step, method). lags and step apply when the kernel is a delay distribution. The keyword method sets how a convolution sums its lags: Direct() sums them one by one, and FFTMethod() uses a fast Fourier transform. By default a convolution takes the transform for a kernel of at least 300 lags over at least 1000 steps.

An operator, modifier or shape wrapper displays in the REPL as one line with its size and parts, such as Convolution(7 lags) for delay. repr and string keep the constructor form.

Calls ​

A recurrence renewal takes its gain first, and a convolution delay takes its input first and its gain as a keyword.

CallReturns
renewal(gain; history, add, start, stop)the run, a vector or strata × time
delay(x; gain, add, history, start, stop)an array shaped like x
renewal(gain; history, record = (:state, :trace))(; y, state, trace), with the State to resume from
ComposableRecurrences.contributions(delay, x; ...)each lag's term before the sum
delay'(x; tail, start, stop)the transpose, which reads x forward from each time
pressure(renewal, y; history, start) recomputed from y; y over it is the gain as run
ComposableRecurrences.window_sums(y, ends; dims)the sums of y over windows ending at ends
ComposableRecurrences.window_sums(y, starts, stops; dims)the sums of y over windows from starts to stops, which may leave gaps or overlap
ComposableRecurrences.kernel_weights(TimeVarying(w, Primary()), n)the weights a computed kernel w gives cohorts 1 to n

The keyword stop defaults to the common length of the time-indexed inputs, and inputs of different lengths without a stop are an error. An operator can be built inside a model, once for each draw of its parameters: its constructor's checks read only types, so building it adds no measurable cost to a call or a gradient. Without a gain, a recurrence takes a gain of 1. A trace holds one matrix per modifier in trace.modifiers, with a column per step, and the pressure of each step in trace.pressure, shaped like y. With :force in record it also holds trace.force, the value of each step before the modifiers,    for gain and added input , shaped like y; :trace alone leaves it empty. Pass the modifiers as a named tuple, as modifiers = (; pool, imports), and trace.modifiers is keyed by those names, as trace.modifiers.pool. record can also name a part of the trace alone, :pressure, :force or a modifier's name. The parts it does not name are not built and come back empty, so the call's return type stays that of a whole trace.

julia
pool = ComposableRecurrences.Depletion(100.0)
named = Recurrence([0.2, 0.5, 0.3]; modifiers = (; pool))
parts = named(fill(1.3, 10); history = ones(3), record = (:pool, :pressure))
size(parts.trace.modifiers.pool), size(parts.trace.pressure)
((3, 10), (10,))

The transpose sums forward in time from each . For a reporting delay over lags and data up to time , the share of the infections at time reported by is

and delay'(ones(T)) returns . For a delay whose weights sum to 1, it is 1 up to    and falls to at .

A recurrence is a convolution of its own outputs ​

A recurrence reads its own past outputs, so a recurrence with kernel is a convolution of its outputs with shifted by one lag, times the gain :

where is the output at time , the kernel weight on lag , and the convolution kernel vcat(0, k). A recurrence's value cannot depend on itself in the same step, so its first weight is on the previous value. A convolution can weight the input at the current time, as a reporting delay of zero does. Both are written as the quantity is usually written, so a generation interval or a delay goes in unchanged.

julia
k = [0.2, 0.5, 0.3]
gain = fill(1.3, 10)
seed = [1.0, 1.0, 1.0]
y = Recurrence(k)(gain; history = seed)
p = Convolution(vcat(0, k))(y; history = seed)
y ≈ gain .* p
true

The same identity recovers any quantity inside a step from the outputs. Operators return only their outputs, so recompute an intermediate this way rather than asking the operator for it.

Time base ​

Every time-indexed input is read at absolute time , counted from 1. This holds for gain, add, a convolution's x and every TimeVarying kernel, coupling or parameter. start and stop choose which times are computed, not how the inputs are read. So a run over a window of longer time-varying inputs needs no slicing: pass them whole and set start and stop. So a call from start = 4 reads gain[4] at its first step, and the output has stop - start + 1 entries. With the history value at time , that first step is

julia
gain = collect(range(1.5, 0.8; length = 10))
r = Recurrence(k)
y_late = r(gain; history = seed, start = 4)
length(y_late), y_late[1] ≈ gain[4] * sum(k .* reverse(seed))
(7, true)

One time base means a series built in sections lines up with one built whole. A resumed call, a later start and a forecast past the data all read the same column for the same time.

History and state ​

The history holds the values before start, oldest first. The recursion reads only the last of them, and zeros stand for any that are missing. A modifier's Init reads the whole history, so a modifier can set its starting state from more than values.

A State holds everything the next step reads: the last outputs, the state of each modifier and the next time. So a call that resumes from a State gives the values one call over both ranges would. A forecast is then a second call, not a second model.

julia
modifiers = (Depletion(50.0; pools = (S = 50.0 - sum(seed),)),)
r = Recurrence(k; modifiers)
whole = r(fill(1.6, 20); history = seed)
fit, state = r(fill(1.6, 20); history = seed, stop = 12, record = (:state,))
forecast = r(fill(1.6, 20); state)
whole ≈ vcat(fit, forecast)
true

The pool in the State carries the depletion over, so the forecast starts from the pool the fit left. A modifier parameter read from past outputs, Recent, deepens the window past , and the State keeps the deeper window.

Seeding ​

A recurrence needs values before its first step, in one of two places.

  • Before time 1, as history with start = 1. The run starts at time 1, so the gain and other inputs cover the run only.

  • At times 1 to , as history with start = m + 1. The inputs then cover the seed times too, which a Primary kernel needs, since only times from 1 have a column.

exponential_history builds a seed that grows at a fixed rate, as at the start of an epidemic. A seed drawn from a finite pool should leave the pool smaller; the pools keyword of Depletion does this.

julia
seed = exponential_history(2.0, 0.1, length(k))
gain = fill(1.2, 13)
before = Recurrence(k)(gain[4:end]; history = seed)
after = Recurrence(k)(gain; history = seed, start = 4)
before ≈ after
true

The two placements give the same run whenever the inputs over the seed times are not read.

What a call returns ​

A recurrence returns the run alone, from start to stop. With include_history = true it returns the seed followed by the run, which is the whole series a later operator should see. Each trace then has zero columns over the seed, so it shares the output's times. This works with a view as the history on every backend, which vcat does not under Enzyme.

julia
whole = Recurrence(k)(gain; history = seed, include_history = true)
whole == vcat(seed, Recurrence(k)(gain; history = seed))
true

A convolution's history holds inputs before time 1, which it reads but does not return.

So a delay after a recurrence takes the seed as its own history. Delaying the seed and run together and dropping the seed times gives the same reports.

julia
delay = Convolution([0.1, 0.4, 0.3, 0.2])
infections = Recurrence(k)(gain; history = seed)
reports = delay(infections; history = seed)
joined = Recurrence(k)(gain; history = seed, include_history = true)
reports ≈ delay(joined)[(length(seed) + 1):end]
true

Pass the seed as history when the reports should cover the run only, and join it in front when they should cover the seed times too.

Output number type ​

The buffer eltype promotes ComposableRecurrences.param_eltype of every input and field. So Float32 inputs give a Float32 output, and dual numbers pass through any slot. With integer and Bool inputs and fields alone and no modifiers, the output keeps that integer type promoted with Int. So a signed or Bool running total has the eltype cumsum gives. Otherwise it is the float type. A user coupling whose pressure is not an integer needs a float input.

Time-series processes ​

A time-series process, such as a random walk or an autoregression, builds a series from innovations . Each is a recurrence or a convolution: a recurrence takes the innovations as add, and a convolution as its input. As a latent process, one that is not observed, it can drive a gain, such as the reproduction number through its logarithm.

julia
ϵ = [0.5, -0.25, 0.125, 0.25, -0.5]
Recurrence([1.0])(; history = [0.0], add = ϵ)
5-element Vector{Float64}:
 0.5
 0.25
 0.375
 0.625
 0.125
ProcessMathsCode
Random walk  Recurrence([1.0])(; history = [z0], add = ϵ)
Autoregression, AR(p)  Recurrence(φ)(; history, add = ϵ)
Autoregression with a coefficient that changes over time  Recurrence(TimeVarying(reshape(φ, 1, :)))(; history, add = ϵ)
Moving average, MA(q)  Convolution(vcat(1, θ))(ϵ)

These calls have no gain, and the run is as long as ϵ. reshape(φ, 1, :) makes a kernel of one lag with a column per step. A recurrence's own weighted sum is a convolution of its outputs too, as above shows.