Skip to content

fix(Aggregate): right-align a delay nested inside an aggregation - #307

Merged
seabbs merged 3 commits into
mainfrom
fix/aggregate-delay-right-align
Aug 20, 2026
Merged

seabbs merged 3 commits into
mainfrom
fix/aggregate-delay-right-align

Conversation

@seabbs-bot

@seabbs-bot seabbs-bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #292.

A LatentDelay nested inside an Aggregate shortened the window series, so
the inner model returned fewer predictions than there were present windows and
the scatter back to full length raised DimensionMismatch.

What changed

_return_aggregate now places the inner predictions on the last
length(pred_obs) reporting windows, dropping the leading ones. That is the
issue's first option and it matches the right-alignment every other observation
chain in the package already uses.

Aggregate also right-aligns a Y_t that is shorter than y_t, so the other
nesting composes too (see below).

The docstring gains two sentences naming both orders and the units each gives.
No warning, no unit conversion.

Confirming the "partial window" reading

The issue's first option was described as resting on "a partially covered
window is dropped". That reading does not hold against the code. A window
reaching back before the start of the series is clipped, not dropped —
sum(Y_t[max(1, i - aggregation[i] + 1):i]) sums whatever days exist.

The right-alignment is still correct, but for a different reason: LatentDelay
itself right-aligns and drops its own leading entries, and the scatter simply
follows the alignment its inner model already chose. No new concept is needed,
and the existing clipping behaviour at the start of a series is unchanged.

A second bug, found while writing the docstring

The docstring is required to name both orders as valid. The other order did not
work:

LatentDelay(Aggregate(PoissonError(), [0, 0, 0, 0, 0, 0, 7]), [0.0, 0.0, 1.0])
# BoundsError: attempt to access 26-element Vector{Float64} at index [22:28]

Aggregate sized its windows off y_t while indexing Y_t, so a delay applied
outside the aggregation ran off the end. Fixed with the same right-alignment
statement: a shorter Y_t covers the last length(Y_t) time points, and window
indices shift by the offset. A window reaching back before the start of Y_t is
clipped, exactly as one reaching before time 1 already was.

This is beyond the letter of #292 but not beyond its subject, and documenting an
order that raises BoundsError was not an option.

Units, pinned by test

With weekly windows over 28 days, a spike on the last day of each window, and a
point-mass delay of lag two:

nesting delay units expected at days 7, 14, 21, 28
Aggregate(LatentDelay(...), agg) windows (14 days) 0, 0, 1, 2
LatentDelay(Aggregate(...), pmf) time points (2 days) 0, 1, 2, 3

Also tested: a delay exactly as long as the window series leaves one window, and
a delay longer than the window series throws.

Tests

Run in this worktree, on this branch:

  • Pkg.test(test_args = ["skip_quality"]) — 1255 pass, 0 fail (21m44s).
  • Pkg.test(test_args = ["quality_only"]) — passed (Aqua, JET, formatting,
    doctests).
  • julia --project=test/ad test/ad/runtests.jl forwarddiff — 1008 pass,
    0 fail. The DirectInfections+Aggregate AD scenario is in that set.
  • Runic clean on src and test.

This was opened by a bot. Please ping @seabbs for any questions.

An observation modifier nested inside an `Aggregate` shortens the window
series, so the inner model returns fewer predictions than there are present
windows and the scatter back to full length failed to build. Scatter onto
the windows that survive, dropping the leading ones as every other
observation chain does.

Right-align a shorter expected series to the observed series too, so a
delay applied outside the aggregation composes as well. Record in the
docstring that the nesting decides whether the delay is in windows or in
time points.
@github-actions

Copy link
Copy Markdown
Contributor

Try this Pull Request!

Option 1: Julia Package Manager

Open Julia and type:

import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposableTuringIDModels.jl", rev="fix/aggregate-delay-right-align")
using ComposableTuringIDModels

Option 2: Local Checkout

If you have the repo locally:

git checkout fix/aggregate-delay-right-align
julia --project=. -e "using Pkg; Pkg.instantiate()"

@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unit 93.23% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/observation_models/modifiers/Aggregate.jl 92.30% <100.00%> (+1.39%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

seabbs
seabbs previously approved these changes Aug 20, 2026

@seabbs seabbs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A delay nested inside an Aggregate cannot be built

2 participants