Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
628dcce
Select strata of a state, a coupling per batch and a kernel shared by…
claude Dec 1, 2026
fa57c33
State the chunked-run contract, test it over the scenario registry an…
claude Dec 1, 2026
93c9625
Load a resumed history a stratum at a time and test the stepwise piec…
claude Dec 1, 2026
c61d2d9
Keep the new docs within the wording, sentence and code length standards
claude Dec 1, 2026
e13229c
Round the tutorial's summary
claude Dec 1, 2026
349f96e
Word the history-load comment in the present
claude Dec 1, 2026
adcd789
Merge main into the stepwise branch
claude Dec 1, 2026
b12de6e
Rewrite the particle filter page from generic functions and share sel…
claude Dec 1, 2026
e1809cc
Merge main into the stepwise branch
claude Dec 2, 2026
95ec588
Run reversed scenarios unchunked in the chunked-run test
claude Dec 2, 2026
2b32b51
Zero only the buffer rows a history leaves unloaded, and load a histo…
claude Dec 2, 2026
8550755
Keep a state's outputs in the buffer of the run that wrote them
claude Dec 2, 2026
7a91254
A state as one vector: vec(state), State(like, x; t) and length(state)
claude Dec 2, 2026
d78b0fa
history = PerStratum(x): one value before the start for each stratum
claude Dec 2, 2026
9eaa7b5
Benchmark a run a step at a time: the resumed_steps case and stepwise…
claude Dec 2, 2026
73365af
Proposal: advance!(r, state, gain) steps a state in its own buffer
claude Dec 2, 2026
7ceba22
Read and return a state on a buffer held strata × time without a gene…
claude Dec 2, 2026
ac39cbd
Particle filtering page: LowLevelParticleFilters.jl does the filterin…
claude Dec 2, 2026
22ec9a7
Test the bootstrap filter with particles as strata on the host and on…
claude Dec 2, 2026
5813fe6
Merge main into the cheap resumed step branch
claude Dec 2, 2026
b2424e1
Batch axis before time, a shared kernel's cotangent on the device, sh…
claude Dec 2, 2026
4cc23ea
advance!: the run it returns as a formula
claude Dec 2, 2026
920359d
vec(state) and State(like, x) behind their own functions
claude Dec 2, 2026
37fc903
Merge main into the cheap resumed step branch
claude Dec 3, 2026
80dc12e
Name the owned field in the State docstring
claude Dec 3, 2026
c739061
The rule returns the state the plain run returns, and takes its buffe…
claude Dec 3, 2026
618e5b7
Fold the particle filtering page of #1016 into this branch
claude Dec 3, 2026
f5a14f9
Merge main into the cheap resumed step branch
seabbs-bot Dec 3, 2026
465efd8
Say what reusing a state after advance! does, and test the strata × t…
seabbs-bot Dec 3, 2026
2d28edb
A cached run's state on floats has the plain run's type
seabbs-bot Dec 4, 2026
11e9b2f
Restore the Gordon1993 and Andrieu2010 entries the page fold dropped
seabbs-bot Dec 4, 2026
aa69aca
Leave the PerStratum history scenarios out where a release lacks them
seabbs-bot Dec 4, 2026
dcd406b
Traced buffer and zeros methods on a state's buffer rows
seabbs-bot Dec 4, 2026
1c6f490
Merge main into the cheap resumed step branch
seabbs-bot Dec 4, 2026
e2de184
One traced zeros method per host type, so neither is ambiguous
seabbs-bot Dec 4, 2026
52fbf9a
Name vec in the particle filtering page without a cross-reference
seabbs-bot Dec 4, 2026
3db88e7
Pass every run keyword through the Reactant extension's traced run
seabbs-bot Dec 4, 2026
b7b8701
Reactant rows for the PerStratum history scenarios
seabbs-bot Dec 4, 2026
2ef35b4
The PerStratum history scenarios' rows in the Reactant results page
seabbs-bot Dec 4, 2026
f482c6d
Merge main into the cheap resumed step branch
seabbs-bot Dec 4, 2026
5f7742a
State(like, x) keeps the strata of a history with no outputs
seabbs-bot Dec 4, 2026
76ef073
Merge main into the cheap resumed step branch
seabbs-bot Dec 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/docs_config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const HEAVY_TUTORIALS = [
"imported-cases.jl",
"clinical-outcomes.jl",
"forecasting.jl",
"particle-filter.jl",
"reproduction-number.jl",
"latent-processes.jl",
"spatial-strata.jl",
Expand All @@ -44,6 +45,8 @@ const TUTORIAL_STUBS = [
"clinical-outcomes.md" =>
"# [Clinical outcomes](@id tutorial-clinical-outcomes)",
"forecasting.md" => "# [Forecasting](@id tutorial-forecasting)",
"particle-filter.md" =>
"# [A particle filter and SMC² for a renewal model](@id tutorial-particle-filter)",
"reproduction-number.md" =>
"# [The reproduction number from a run](@id tutorial-reproduction-number)",
"latent-processes.md" =>
Expand Down Expand Up @@ -252,6 +255,8 @@ const PACKAGE_TUTORIALS = [
"Clinical outcomes" =>
"getting-started/tutorials/clinical-outcomes.md",
"Forecasting" => "getting-started/tutorials/forecasting.md",
"A particle filter and SMC² for a renewal model" =>
"getting-started/tutorials/particle-filter.md",
"Reproduction number from a run" =>
"getting-started/tutorials/reproduction-number.md",
"Latent processes driving the reproduction number" =>
Expand Down
3 changes: 2 additions & 1 deletion docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pages = [
"Imported cases" => "getting-started/tutorials/imported-cases.md",
"Clinical outcomes" => "getting-started/tutorials/clinical-outcomes.md",
"Forecasting" => "getting-started/tutorials/forecasting.md",
"A particle filter and SMC² for a renewal model" => "getting-started/tutorials/particle-filter.md",
"Reproduction number from a run" => "getting-started/tutorials/reproduction-number.md",
"Latent processes driving the reproduction number" => "getting-started/tutorials/latent-processes.md",
"Spatial models" => "getting-started/tutorials/spatial-strata.md",
Expand All @@ -50,6 +51,7 @@ pages = [
"Internal API" => "lib/internals.md",
],
"Extensions" => [
"ADTypes + DifferentiationInterface" => "extensions/differentiation-interface.md",
"ADTypes + Enzyme + EnzymeTestUtils" => "extensions/enzyme-test-utils.md",
"ADTypes + Mooncake + Random" => "extensions/mooncake.md",
"Adapt + KernelAbstractions" => "extensions/kernel-abstractions.md",
Expand All @@ -58,7 +60,6 @@ pages = [
"Distributions" => "extensions/distributions.md",
"Enzyme" => "extensions/enzyme.md",
"FFTW" => "extensions/fftw.md",
"ForwardDiff" => "extensions/forward-diff.md",
"Functors" => "extensions/functors.md",
"GPUArrays + KernelAbstractions" => "extensions/gpuarrays.md",
"LogExpFunctions" => "extensions/log-exp-functions.md",
Expand Down
1 change: 1 addition & 0 deletions docs/src/explanation/api-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Both operators compute one step at a time; [A step](@ref explanation-step) gives
## [Shapes](@id overview-shapes)

[Slots and shapes](@ref explanation-slots) says which wrapper each coefficient takes, and [Data shapes](@ref explanation-data-shapes) gives the shapes of the gain and `add`.
[Parameters shared across strata](@ref explanation-shared-parameters) stores a parameter once for the strata that read it, and [Batches of strata](@ref explanation-batches) runs copies of a model as strata.

## [Delay distributions](@id overview-delays)

Expand Down
61 changes: 61 additions & 0 deletions docs/src/explanation/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ A resumed call, a later `start` and a forecast past the data all read the same c
The `history` holds the values before `start`, oldest first.
The recursion reads only the last ``L`` of them, and zeros stand for any that are missing.
For a single stratum a number is one value before `start`, so `history = 1.0` is `history = [1.0]`.
A vector is the values of one series, so one value before `start` for each of ``S`` strata is `history = PerStratum(x)`, which is `history = reshape(x, S, 1)`.
A modifier's `Init` reads the whole history, so a modifier can set its starting state from more than ``L`` values.

A [`State`](@ref ComposableRecurrences.State) holds everything the next step reads: the last ``L`` outputs, the state of each modifier and the next time.
Expand All @@ -176,6 +177,66 @@ whole ≈ vcat(fit, forecast)
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`](@ref ComposableRecurrences.Recent), deepens the window past ``L``, and the `State` keeps the deeper window.

A run in any number of chunks is the single run, for the outputs, the pressure, the traces and their gradients, on host and device arrays: [`Resume`](@ref ComposableRecurrences.Resume) states the contract.
Every chunk reads its inputs at absolute times, so each takes the inputs of the single run and its own `stop`.
A stepwise method, such as a particle filter or an online forecast, is a loop of such chunks.

Between chunks, [`select(state, strata)`](@ref ComposableRecurrences.select) returns the state whose stratum ``i`` is stratum `strata[i]` of the one given:

```math
y'_{t,i} = y_{t,r_i}, \qquad s'_{i} = s_{r_i},
```

where ``r_i`` = `strata[i]`, ``y_t`` are the last outputs the state holds and ``s`` each modifier's state.
It drops strata that have finished, repeats those a resampling step keeps and reorders them.

```@example operators
using ComposableRecurrences: select
pool = Depletion(PerStratum([50.0, 80.0, 60.0]))
r = Recurrence(k; modifiers = (pool,))
R = fill(1.6, 3, 20)
first_part = r(R; history = ones(3, 3), stop = 12, record = Resume())
kept = [3, 1]
r_kept = Recurrence(k; modifiers = (Depletion(PerStratum([60.0, 50.0])),))
rest = r_kept(R[kept, :]; state = select(first_part.state, kept))
rest ≈ r(R; state = first_part.state)[kept, :]
```

A method that keeps its own state vector, such as a filter from another package, reads a `State` as one vector and builds one back.
The vector [`vec(state)`](@ref Base.vec(::ComposableRecurrences.State)) holds each stratum's outputs, oldest first, then each modifier's state.
The call [`State(like, x; t)`](@ref ComposableRecurrences.State(::ComposableRecurrences.State, ::AbstractVector)) returns the state of that shape whose next step is at time `t`.
Time is absolute in every call and the vector holds none, so the method passes it.

```@example operators
state = first_part.state
x = vec(state)
again = ComposableRecurrences.State(state, x; t = state.t)
length(state), vec(again) == x
```

A resumed call leaves the state it is given as it was: it copies the state's outputs into a new buffer, runs there and returns a new state.
The call [`advance!(r, state, gain)`](@ref ComposableRecurrences.advance!) runs the same steps in the buffer the state already has and returns the same outputs and state.
A loop of single steps then copies no history.
The state passed is used up, and the loop carries on from the one returned.

```@example operators
using ComposableRecurrences: advance!
function stepwise(r, R, state, n)
y = zeros(size(R, 1), n)
for t in 1:n
out = advance!(r, state, R)
y[:, t] = out.y
state = out.state
end
return y, state
end
start = r(R; history = ones(3, 3), stop = 0, record = Resume()).state
y, last_state = stepwise(r, R, start, 20)
y ≈ r(R; history = ones(3, 3))
```

[A particle filter and SMC² for a renewal model](@ref tutorial-particle-filter) builds a particle filter from resumed calls and `select`.

## [Seeding](@id explanation-seeding)

A recurrence needs values before its first step, in one of two places.
Expand Down
73 changes: 70 additions & 3 deletions docs/src/explanation/shapes-and-couplings.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Each slot accepts its own set of wrappers.

| Slot | Bare | [`PerStratum`](@ref) | [`TimeVarying`](@ref) | [`Pairwise`](@ref) | `Pairwise` routes | [`Derived`](@ref ComposableRecurrences.Derived) | [`Recent`](@ref ComposableRecurrences.Recent) | [`Gather`](@ref ComposableRecurrences.Gather) |
|---|---|---|---|---|---|---|---|---|
| `Recurrence` kernel | lags | yes | yes | yes | yes | in `TimeVarying(d, Primary())` | no | no |
| `Recurrence` kernel | lags | yes | yes | yes | yes | in `TimeVarying(d, Primary())` | no | of a `PerStratum` kernel, fixed or `TimeVarying` |
| `Convolution` kernel | lags | yes | yes | no | no | in `TimeVarying(d, Primary())` | no | no |
| coupling | `S × S` or `λI`; `S × S × T` read as `TimeVarying` | no | yes | no | no | no | no | no |
| modifier parameter | one value; `S × T` read as `TimeVarying(PerStratum(x))` | yes | yes | no | no | yes | yes | yes |
Expand All @@ -64,7 +64,7 @@ Each slot accepts its own set of wrappers.
- [`Pairwise(A)`](@ref Pairwise): one kernel per pair of strata, receiving stratum first; `Pairwise(K₁ => k₁, K₂ => k₂)` sums routes, each a coupling and a kernel.
- [`TimeVarying(x)`](@ref TimeVarying): a kernel, coupling or parameter that changes over time, time last.
- [`Derived(f, args...)`](@ref ComposableRecurrences.Derived): a parameter computed by `f`, or a cohort kernel computed inside the operator.
- [`Gather(p, rows)`](@ref ComposableRecurrences.Gather): a parameter read by stratum `k` at row `rows[k]` of `p`, such as a value given per group read by each stratum of the group.
- [`Gather(p, rows)`](@ref ComposableRecurrences.Gather): a parameter or kernel read by stratum `k` at row `rows[k]` of `p`, so strata [share the rows](@ref explanation-shared-parameters) they read.
- [`Survival(h)`](@ref ComposableRecurrences.Survival): each cohort's survival against a hazard `h`, for a computed kernel.
- [`Increments(x)`](@ref ComposableRecurrences.Increments): a computed kernel's differences along its lags, such as a delay's mass from its distribution, `Increments(1 - Survival(h))`.
- [`Normalised(x)`](@ref ComposableRecurrences.Normalised): a computed kernel's weights over each cohort's total across its lags; it fills on the host only.
Expand Down Expand Up @@ -123,6 +123,73 @@ r(PerStratum([1.0, 2.0]); history = ones(2, 2), stop = 4)

For a convolution it is `repeat(ρ, 1, T)` as the gain, or `ρ .* c(x)` without `add`.

## [Parameters shared across strata](@id explanation-shared-parameters)

Strata often cross two factors, such as age groups and locations, or the particles of a filter and the strata of the model.
A parameter then differs over one factor only, and a `PerStratum` of it would repeat each value.
[`Gather(p, rows)`](@ref ComposableRecurrences.Gather) stores the parameter once per level and reads it by an index:

```math
u_{t,k} = p_{t, r_k},
```

where ``u_{t,k}`` is the value stratum ``k`` reads at time ``t``, ``p`` the stored parameter with a row per level and ``r_k`` = `rows[k]` the level of stratum ``k``.
Here four strata are two ages in each of two places, with a susceptibility per age and a population per place:

```@example shapes
using ComposableRecurrences: Depletion, Gather, select
age = [1, 2, 1, 2]
place = [1, 1, 2, 2]
susceptibility = Gather(PerStratum([1.0, 0.6]), age)
population = Gather(PerStratum([500.0, 200.0]), place)
r = Recurrence([0.5, 0.3]; modifiers = (Depletion(population),))
r(susceptibility; history = ones(4, 2), stop = 3)
```

The `Gather` is the outer wrapper, so a level's value over time is `Gather(TimeVarying(PerStratum(P)), rows)`.
It is a modifier parameter, a `Derived` argument, a gain or add, a `Depletion`'s population or a `Recurrence` kernel.
In a gradient the strata that share a row add their cotangents into it, so an AD backend differentiates the stored array.

[`select(parameter, strata)`](@ref ComposableRecurrences.select) reads a parameter at chosen strata.
It changes the index and leaves the stored values where they are:

```@example shapes
kept = select(population, [4, 1])
kept.rows, kept.p === population.p
```

## [Batches of strata](@id explanation-batches)

A batch is ``B`` independent copies of an ``S``-stratum model: particles, scenarios, posterior draws or replicate simulations.
The copies run as ``B S`` strata, with stratum ``(b - 1) S + i`` being stratum ``i`` of batch ``b``, so there is no batch axis inside the operators.
A coupling and a shared parameter make the copies one model.

- [`Batched(K)`](@ref ComposableRecurrences.Batched) is the coupling that mixes the strata of each batch by the same ``S \times S`` matrix,

```math
q_{(b-1)S + i} = \sum_{j=1}^{S} K_{ij} \, p_{(b-1)S + j}, \qquad b = 1, \dots, B,
```

where ``p`` holds each stratum's kernel-weighted past and ``q`` the pressure on it.
It is one matrix product on the strata reshaped to ``S \times B``.
- A `Gather` stores a parameter once whether it differs by stratum, by batch or by neither.

A call whose `history` is ``S \times h \times B`` runs the batches as strata and returns ``S \times T \times B``:

```@example shapes
using ComposableRecurrences: Batched
K = [0.9 0.1; 0.2 0.8]
population = Gather(PerStratum([500.0, 200.0]), repeat(1:2, 3))
batched = Recurrence(
[0.5, 0.3]; coupling = Batched(K), modifiers = (Depletion(population),)
)
h = ones(2, 2, 3) .* reshape([1.0, 2.0, 4.0], 1, 1, 3)
size(batched(1.4; history = h, stop = 5))
```

A [resumed call](@ref explanation-history-state) works on the ``B S`` strata, and `select` of whole batches resamples them.
A modifier whose groups hold more than one value lays its strata out group by group, so its batches are groups and not consecutive strata.

## [Nesting](@id explanation-nesting)

Wrappers nest from the outside in: time, then strata, then the slot's own axes.
Expand Down Expand Up @@ -249,7 +316,7 @@ by_gain == by_rows
true
```

The coupling can be `I`, `λ * I`, a dense, `Diagonal` or sparse matrix, a [`TimeVarying`](@ref) array with time last, or a [type you write](@ref explanation-own-coupling).
The coupling can be `I`, `λ * I`, a dense, `Diagonal` or sparse matrix, a [`TimeVarying`](@ref) array with time last, a [`Batched`](@ref explanation-batches) matrix or a [type you write](@ref explanation-own-coupling).

## [Coupling or pairwise kernel](@id explanation-pairwise)

Expand Down
Loading