Skip to content

feat(latent): add broadcast_window for an effect confined to a window - #345

Merged
seabbs merged 4 commits into
mainfrom
166-windowed
Sep 3, 2026
Merged

seabbs merged 4 commits into
mainfrom
166-windowed

Conversation

@seabbs-bot

@seabbs-bot seabbs-bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #166.

The investigation on #166 found a windowed effect was already reachable by composing existing modifiers, and this PR started as documentation of that. Sam asked for a helper as well, so it now ships one.

broadcast_window

broadcast_window(model, window) builds a latent model that equals model on window and zero everywhere else. Summing it onto a base path with CombineLatentModels confines the effect to that window.

base = RandomWalk(init = Normal(0, 0.1), ϵ_t = IID(Normal(0, 0.05)))
windowed = CombineLatentModels(
    [base, broadcast_window(Normal(1, 0.2), 20:30)], ["", "Window"]
)
keys(rand(as_turing_model(windowed, 40)))
# VarName[init, ϵ_t, Window.intercept]

It is a BroadcastLatentModel under a new InWindow rule, the same shape broadcast_dayofweek has under RepeatEach. No new component type, and it stacks by ordinary composition.

One helper covers every effect form because the inner slot is a PATH slot. FixedIntercept(2.0) gives a known constant, a bare Distribution gives one estimated level held across the window, and a process gives an effect that varies within it. The rule asks for a series as long as the window, so a process member is generated over the window rather than over the whole series and then masked.

A window touching either end is an ordinary case. This is the one place the obvious alternative fails: partitioning the series with ConcatLatentModels needs a zero-length segment for 1:10 and throws AssertionError: Non-positive dimensions are not allowed. There is a test for each edge.

The window is visible in the model tree

A BroadcastLatentModel does what its rule says, not what its own type says, so the tree label now carries the rule.

CombineLatentModels
├─ models[1]: RandomWalk
│  └─ ϵ_t: IID
└─ models[2]: PrefixLatentModel
   └─ model: BroadcastLatentModel(InWindow(20:30))
      └─ model: Intercept

Before this a day-of-week effect and a windowed effect printed identically as bare BroadcastLatentModel. The label is built from the type name rather than from show, because show module-qualifies the rule name outside the package's own scope.

src/base/prettyprinting.jl is in this diff because that label needed a seam: _node_label(model) = string(nameof(typeof(model))) is the previous expression verbatim, so every component that does not override it prints exactly as before, and BroadcastLatentModel is the single override.

Docs

The design.md section leads with the helper and keeps the from-scratch composition underneath, so the mechanism is still on the page. A test asserts the two routes agree, including that they draw the same prefixed parameter names, so a model can move to the helper without renaming anything.

Tests

test/unit/latent_models/windowed.jl: the rule's placement and its assertion failures, the effect landing exactly on the window, zero contribution outside, the prefixed parameter names, both edge-touching windows and the whole-series window, a process member generated over the window, the tree label, and the equivalence with the from-scratch composition.

Verification

  • Full suite for this worktree excluding the :ad items: 2128 pass, 5 pre-existing broken, no failures (18m21s).
  • Quality suite (Aqua, exports, doctests) is included in that and passes.
  • Runic --check src test docs benchmark ext: exit 0.
  • Every @example design block in the new section runs in order in a clean module.

Not verified: the full Documenter build. Its CI job was cancelled rather than failed on both attempts of the previous revision, and a local build did not finish. Treat the docs build as unconfirmed until CI reports green on this revision.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ

@github-actions

github-actions Bot commented Sep 2, 2026

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="166-windowed")
using ComposableTuringIDModels

Option 2: Local Checkout

If you have the repo locally:

git checkout 166-windowed
julia --project=. -e "using Pkg; Pkg.instantiate()"

@seabbs-review-bot seabbs-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a docs-only PR (plus one new @testitem) that shows a windowed effect built from existing TransformLatentModel and CombineLatentModels components, with no new source code. The composition claims check out against the actual implementations (index-based gating, prefix naming, AR damp as a process), and the test exercises real behaviour rather than restating the implementation. Sound change; one minor gap worth a look.

Automated first pass by seabbs-review-bot (Claude sonnet), triggered by: first pass. Not a human review. Comment @seabbs-review-bot to ask for another pass: @seabbs any time, the author's agent once it has pushed changes. Add the no-review label to opt this PR out. Ping @seabbs with any questions.

Comment thread docs/src/design.md Outdated
keys(rand(as_turing_model(shifted, 40)))
```

[`CombineLatentModels`](@ref) sums, so a multiplicative window effect on a

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion This closing sentence asserts a new technique (multiplicative windowing via log-space composition wrapped in exp) but unlike every other claim in this section it has no code block backing it. Either cut the sentence or add a short @example design block demonstrating it, so the page doesn't end on an untested assertion.

@seabbs-review-bot seabbs-review-bot Bot added the llm-reviewed Reviewed by seabbs-review-bot label Sep 2, 2026
@codecov

codecov Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/latent_models/manipulators/broadcast/rules.jl 0.00% 13 Missing ⚠️
src/base/prettyprinting.jl 0.00% 2 Missing ⚠️
...atent_models/manipulators/broadcast/LatentModel.jl 0.00% 2 Missing ⚠️
...rc/latent_models/manipulators/broadcast/helpers.jl 0.00% 2 Missing ⚠️
Flag Coverage Δ
ad-forwarddiff 63.82% <0.00%> (-0.75%) ⬇️
ad-reversediff-compiled 64.03% <0.00%> (-0.75%) ⬇️

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

Files with missing lines Coverage Δ
src/base/prettyprinting.jl 94.44% <0.00%> (-5.56%) ⬇️
...atent_models/manipulators/broadcast/LatentModel.jl 83.33% <0.00%> (-16.67%) ⬇️
...rc/latent_models/manipulators/broadcast/helpers.jl 66.66% <0.00%> (-33.34%) ⬇️
src/latent_models/manipulators/broadcast/rules.jl 48.00% <0.00%> (-52.00%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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.

rather than just docing shall we make a helper for this common pattern?

seabbs-bot and others added 2 commits September 2, 2026 17:02
An effect confined to an index window needs no new latent modifier.
`TransformLatentModel` reads the index and `CombineLatentModels` sums
paths, so a window is the two together.

Add a design section covering the fixed effect, the estimated effect
size, stacking two windows, and the same gated process used as a
per-step parameter. Add a regression test so the composition and its
prefixed parameter names cannot drift.

Investigated for #166.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
`broadcast_window(model, window)` builds a latent model that equals
`model` on `window` and zero everywhere else, so summing it onto a base
path with `CombineLatentModels` confines the effect to that window.

It is a `BroadcastLatentModel` under a new `InWindow` rule, the same
shape as `broadcast_dayofweek` under `RepeatEach`, so it needs no new
component type and stacks by ordinary composition. The rule asks for a
series as long as the window, so a process member is generated over the
window rather than over the whole series. The inner slot is a PATH
slot, which lets one helper serve a known effect, an estimated level
and a process. A window touching either end of the series is an
ordinary case.

Carry the broadcast rule into the model tree label, so a day-of-week
effect and a windowed effect no longer print identically and the window
is visible without reading the code. The label is built from the type
name rather than from `show`, which module-qualifies outside the
package's own scope.

Rewrite the design section to lead with the helper and keep the
from-scratch composition underneath as the explanation.

Closes #166.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
@seabbs-bot seabbs-bot changed the title docs(latent): show a windowed effect built from existing modifiers feat(latent): add broadcast_window for an effect confined to a window Sep 2, 2026
@seabbs-bot

Copy link
Copy Markdown
Collaborator Author

Built the helper as asked rather than only documenting.

broadcast_window(model, window) now ships, as a function beside broadcast_dayofweek and broadcast_weekly, backed by a new InWindow broadcast rule. Building it did not change my mind on the name or the shape.

One helper turned out to cover both the fixed and the estimated effect, because the inner slot is a PATH slot: FixedIntercept(2.0) for a known constant, a bare Distribution for an estimated level, a process for an effect that varies within the window. So there is no second helper.

Two things worth flagging:

Making the window visible in the tree needed more than following broadcast_dayofweek, because that sibling does not show its own period or rule either. I added a label seam so a BroadcastLatentModel carries its rule into the tree. That fixes the invisibility for the day-of-week and weekly helpers at the same time, and it is why src/base/prettyprinting.jl is in the diff.

The docs section now leads with the helper and keeps the from-scratch composition underneath, with a test asserting the two agree and draw the same prefixed names.

Rebased onto current main, so #346, #347 and #351 are in.

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

@seabbs-bot

Copy link
Copy Markdown
Collaborator Author

@seabbs-review-bot

@seabbs-review-bot seabbs-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

broadcast_window adds a well-tested InWindow broadcast rule that places an inner latent process on a declared window and zeroes the rest, wired through the existing PATH-slot/BroadcastLatentModel machinery with no new abstraction. Behaviour is correct (window bounds, zero-fill, AD-safe zero via eltype(latent), tree-label override) and tests cover fixed/estimated/process cases, edge windows and the manual-composition equivalence. The main nit is documentation length: the same PATH-slot and edge-window explanation is repeated near-verbatim across three places.

Automated first pass by seabbs-review-bot (Claude sonnet), triggered by: re-review requested by seabbs-bot. Not a human review. Comment @seabbs-review-bot to ask for another pass: @seabbs any time, the author's agent once it has pushed changes. Add the no-review label to opt this PR out. Ping @seabbs with any questions.


The result is a latent model that equals `model` on `window` and zero everywhere else, so summing it onto a base path with [`CombineLatentModels`](@ref) gives an effect that is active only over the window.

The `model` slot is a PATH slot, which is what lets one helper serve both a fixed and an estimated effect.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion This paragraph repeats content already stated in three places: BroadcastLatentModel's own docstring (LatentModel.jl:67-70) covers the PATH-slot/auto-wrap behaviour, InWindow's docstring (rules.jl:44-58) covers the window semantics, and design.md's new 'How it is built' section restates both again. Trim broadcast_window's docstring to what is specific to the helper (that it composes BroadcastLatentModel with InWindow) and link to BroadcastLatentModel/InWindow for the shared behaviour, rather than re-explaining it a third time.

Review feedback. The closing sentence claimed a multiplicative window
effect works in log space under an `exp` transform, and was the only
claim in the section with no runnable block behind it.

Add the block, and a test asserting the ratio to the unwindowed path is
the effect inside the window and one outside.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
@seabbs-bot

Copy link
Copy Markdown
Collaborator Author

Acted on the review.

Accepted the one finding. The closing sentence on the multiplicative window was the only claim in the section with no runnable block behind it. It now has one, and a test asserting the ratio to the unwindowed path is the effect inside the window and exactly one outside. I checked the claim was true before writing the example rather than the other way round.

I also reframed it while adding the code. The old wording said a multiplicative effect on a positive path is the composition in log space wrapped in exp, which put the transform in the wrong place. The example builds the effect additively in log space and exponentiates the result, which is the same thing and is what the code actually does.

Verification on this revision: full suite for this worktree excluding the :ad items, 2128 pass, 5 pre-existing broken, no failures. Runic exit 0.

Documenter is still unverified, and per #353 that is repo-wide: the shared gh-pages-deploy concurrency group cancels the queued run when a third branch arrives, so docs runs here are usually cancelled about twenty seconds in, before Julia starts. gh pr checks renders that as a failure. Every @example design block in the section runs in order in a clean module, which is the most I can confirm locally.

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

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Benchmark comparison vs base

Minimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:

Group 🟢 <50% 🟢 50–75% 🟢 75–95% ⚪ 95–105% 🔴 105–125% 🔴 125–150% 🔴 >150%
Evaluation · · · 9 · · ·
ForwardDiff · · · 2 · · ·
ReverseDiff (tape) · · 1 1 · · ·
Mooncake reverse · · · 2 · · ·
Enzyme reverse · · · 1 · · ·
Evaluation — 9 benchmarks (by time change)
Benchmark base PR time memory
Sampling / NUTS (DirectInfections+Poisson, 50 draws) 111.23 ms 107.88 ms ⚪ 0.97× ⚪ 1.0×
Model evaluation / RandomWalk latent / rand 431.0 ns 421.0 ns ⚪ 0.98× ⚪ 1.0×
Model evaluation / Renewal+NegativeBinomial / rand 2.23 μs 2.18 μs ⚪ 0.98× ⚪ 1.0×
Model evaluation / AR latent / rand 611.0 ns 600.0 ns ⚪ 0.98× ⚪ 1.0×
Model evaluation / Renewal+NegativeBinomial / forward 4.64 μs 4.72 μs ⚪ 1.02× ⚪ 1.0×
Model evaluation / DirectInfections+Poisson / forward 1.1 μs 1.11 μs ⚪ 1.01× ⚪ 1.0×
Model evaluation / AR latent / forward 540.0 ns 540.0 ns ⚪ 1.0× ⚪ 1.0×
Model evaluation / DirectInfections+Poisson / rand 651.0 ns 651.0 ns ⚪ 1.0× ⚪ 1.0×
Model evaluation / RandomWalk latent / forward 420.0 ns 420.0 ns ⚪ 1.0× ⚪ 1.0×
AD gradients — 7 benchmarks (by time change)
Benchmark base PR time memory
AD gradients / AR latent logjoint / ReverseDiff (tape) 84.85 μs 68.28 μs 🟢 0.8× ⚪ 1.0×
AD gradients / DirectInfections+Poisson posterior / Enzyme reverse 3.64 μs 3.79 μs ⚪ 1.04× ⚪ 1.0×
AD gradients / DirectInfections+Poisson posterior / Mooncake reverse 8.77 μs 8.97 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / AR latent logjoint / ForwardDiff 8.81 μs 8.99 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / DirectInfections+Poisson posterior / ReverseDiff (tape) 100.34 μs 98.98 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / AR latent logjoint / Mooncake reverse 5.47 μs 5.52 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / DirectInfections+Poisson posterior / ForwardDiff 14.92 μs 14.9 μs ⚪ 1.0× ⚪ 1.0×

@seabbs
seabbs merged commit 234dc44 into main Sep 3, 2026
9 of 25 checks passed
@seabbs
seabbs deleted the 166-windowed branch September 3, 2026 16:55
seabbs-bot added a commit that referenced this pull request Sep 3, 2026
Reflows every authored documentation page so prose is one sentence per line,
splits sentences joined by a colon, semicolon or dash, and cuts restatement
and filler.

Also, on top of the earlier pass:

- a sentence immediately after a code block that re-asserted what the block
  showed is gone,
- a `##` comment in the literate tutorial that carried explanation moved into
  the surrounding markdown,
- trailing clauses making a wry or self-satisfied point are cut, keeping the
  fact.

`design.md`, `delays-dayofweek.md`, `renewal-negbin.md`,
`time-varying-damping.md` and `renewal-modifiers.jl` are treated afresh on top
of #276, #338 and #345.

No fenced code block content changes.

Co-Authored-By: Sam Abbott <contact@samabbott.co.uk>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-reviewed Reviewed by seabbs-review-bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate: is a windowed effect already reachable, or does the latent-model family need a windowed modifier?

2 participants