Skip to content

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

Closed

Description

@seabbs-bot

Before building a windowed latent modifier, establish what the existing latent-modifier machinery can already express and confirm that a windowed effect is genuinely out of reach.
There is existing machinery for composing and modifying a latent process, so the investigation is to pin down exactly what a latent modifier can currently do and where applying an effect only within a declared index window is not yet expressible.
A satisfactory answer either shows the effect is already reachable by composing existing modifiers (no new type needed), or identifies the specific gap that a new windowed modifier would fill and confirms it fits the documented type structure.

Rationale

Check what already exists before building.
The design reasoning below is plausible but rests on that check: the role supertypes are documented as the only type structure, and a process owns its parameter path internally, so a component that wrapped a process and reached into its parameters would break both invariants.
A latent modifier looks like the natural home, since modifiers are latent models themselves.

If the check finds a real gap, the feature is a windowed modifier in the latent-model family: it wraps a latent process and applies an effect to its path only within a declared index window.
Because latent modifiers wrap into latent models, several stack by ordinary nesting, their combined effect is the documented composition of the wrapped effects, and they appear as named branches in the model tree printout with no extra machinery.
The effect slot should get the same treatment other modifiers give theirs, so it accepts a constant multiplier, a callable, or a latent process for an uncertain effect size.
A scenario comparison then becomes a change of modifier stack around the same process rather than a rebuild of the latent path.

Value / effort

Value high · Effort medium · Verdict investigation first (confirm the gap before building).

Illustrative example

using ComposableTuringIDModels, Distributions

# Existing latent modifiers wrap an inner latent model and return a latent
# model, so they nest by ordinary composition. TransformLatentModel applies
# an effect to the WHOLE path:
inner = RandomWalk(Normal(0, 0.1), HalfNormal(0.05))
whole = TransformLatentModel(inner, x -> x .+ 0.5)   # shifts every index

# Investigation question: can an effect confined to an index window
# (say indices 10:20) be expressed by composing existing modifiers, or is
# there no modifier that reads the index and gates the effect?

# --- Desired usage if the check finds a real gap ---
# A windowed modifier in the same family, effect applied only within a
# declared window, with the effect slot accepting a constant, a callable,
# or a latent process (mirroring how other modifiers treat their effect):

# windowed = WindowedLatentModel(inner; window = 10:20, effect = 2.0)
#   # effect as constant multiplier inside the window, identity outside
# windowed = WindowedLatentModel(inner; window = 10:20, effect = x -> x .+ 1)
#   # effect as a callable
# windowed = WindowedLatentModel(inner; window = 10:20,
#                                effect = Intercept(Normal(0, 1)))
#   # effect size as an uncertain latent process

# Stacking two windows should nest like any other modifier:
# stacked = WindowedLatentModel(
#     WindowedLatentModel(inner; window = 1:5, effect = 1.5);
#     window = 10:20, effect = 2.0)
# rand(as_turing_model(stacked, 30))

Part of #165.

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

Activity

  1. seabbs-bot commented on Sep 2, 2026

    @seabbs-bot
    CollaboratorAuthor

    Verdict: already reachable. No new type needed.

    A windowed effect is TransformLatentModel reading the index and
    CombineLatentModels summing the result onto the base path. Seven routes were
    tried in the REPL against main (Julia 1.12, DynamicPPL 0.42.9, Turing 0.46.1)
    and all seven work, including the uncertain-effect and stacking cases the issue
    listed as the desired usage.

    Setup

    using ComposableTuringIDModels, Distributions, Random
    inner = RandomWalk(init = Normal(0, 0.1), ϵ_t = IID(Normal(0, 0.05)))
    win = 20:30
    mask(x, w) = in.(eachindex(x), Ref(w))

    1. Fixed effect, additive — a single transform

    The closure indexes the path it is handed, so it never needs the series length.

    A1 = TransformLatentModel(inner, x -> x .+ 2.0 .* mask(x, win))
    Random.seed!(2); base = as_turing_model(inner, 40)()
    Random.seed!(2); got  = as_turing_model(A1, 40)()
    got .- base
    # [0.0 ×19, 2.0 ×11, 0.0 ×10]

    This covers both effect = 2.0 and effect = x -> x .+ 1 from the issue.

    2. Uncertain effect size — a gated process, summed

    effect = TransformLatentModel(Intercept(Normal(1.0, 0.2)),
                                  x -> x .* mask(x, win))
    A2 = CombineLatentModels([inner, effect], ["", "Window"])
    keys(rand(as_turing_model(A2, 40)))
    # VarName[init, ϵ_t, Window.intercept]

    The gated process contributes exactly zero outside the window, so the composed
    path is the base path there. This covers effect = Intercept(Normal(0, 1)).

    3. Stacking two windows

    Windows stack by adding members, not by nesting, and each keeps its own name:

    w1 = TransformLatentModel(Intercept(Normal(1.5, 0.1)), x -> x .* mask(x, 1:5))
    w2 = TransformLatentModel(Intercept(Normal(2.0, 0.1)), x -> x .* mask(x, 20:30))
    stacked = CombineLatentModels([inner, w1, w2], ["", "W1", "W2"])
    keys(rand(as_turing_model(stacked, 40)))
    # VarName[init, ϵ_t, W1.intercept, W2.intercept]

    Deltas against the same seed: [1.553 ×5, 0.0 ×14, 1.954 ×11, 0.0 ×10].

    4. The parameter-slot seam

    A latent process and a parameter prior share one role, so the same gated
    process drops straight into a per-step parameter slot. An AR more strongly
    damped over the window and constant outside it:

    damp_win = CombineLatentModels(
        [FixedIntercept(0.4),
         TransformLatentModel(Intercept(Normal(0.4, 0.01)), x -> x .* mask(x, 20:30))],
        ["", "Shift"])
    ar = AR(damp = damp_win, init = Normal(), ϵ_t = IID(Normal(0, 0.1)))
    keys(rand(as_turing_model(ar, 40)))
    # VarName[init, damp.Shift.intercept, ϵ_t]

    5. Multiplicative uncertain effect

    CombineLatentModels sums, so a multiplicative effect on a positive path is
    the same composition in log space:

    pos  = TransformLatentModel(inner, x -> exp.(x))
    wlog = TransformLatentModel(Intercept(Normal(log(2.0), 0.1)),
                                x -> x .* mask(x, win))
    mult = TransformLatentModel(
        CombineLatentModels([TransformLatentModel(pos, x -> log.(x)), wlog],
                            ["", "Window"]), x -> exp.(x))

    Ratio to the same-seed base path: [1.0, 1.0, 2.183, 2.183, 2.183, 1.0] at
    indices [1, 19, 20, 25, 30, 31].

    For a path that may be negative, Stratify at shape (1, n) with
    combine = * and a Replicated gated effect multiplies two paths directly,
    then vec flattens it. Verified: same ratios, no log space.

    6. ConcatLatentModels — works, but is the worse route

    The user's steer was a concat-style helper. ConcatLatentModels already
    exists and does reach the effect, with a window-aware dimension_adaptor:

    seg(w) = (n, m) -> [first(w) - 1, length(w), n - last(w)]
    A3 = ConcatLatentModels(
        [FixedIntercept(0.0), Intercept(Normal(1.0, 0.2)), FixedIntercept(0.0)],
        seg(win); prefixes = ["", "Window", ""])
    CombineLatentModels([inner, A3], ["", ""])   # works for win = 20:30

    But it breaks whenever the window touches an edge, because that needs a
    zero-length segment:

    ConcatLatentModels([FixedIntercept(0.0), Intercept(Normal(1.0, 0.2)),
                        FixedIntercept(0.0)], seg(1:10); prefixes = ["", "Window", ""])
    as_turing_model(A3e, 40)()
    # AssertionError: Non-positive dimensions are not allowed

    The masking route in 2 handles win = 1:10 with no change at all. So concat is
    not the right primitive here.

    7. Third-party route, if the effect should only exist inside the window

    In routes 2 to 6 the effect process runs over the full length and is then
    masked. To generate it over the window only, a six-line user-defined broadcast
    rule already plugs into the existing extension point:

    struct WindowRule{R <: AbstractUnitRange} <: ComposableTuringIDModels.AbstractBroadcastRule
        window::R
    end
    ComposableTuringIDModels.broadcast_n(r::WindowRule, n, period) = length(r.window)
    function ComposableTuringIDModels.broadcast_rule(r::WindowRule, latent, n, period)
        out = zeros(eltype(latent), n)
        out[r.window] .= latent
        return out
    end
    
    A7 = CombineLatentModels(
        [inner, BroadcastLatentModel(RandomWalk(init = Normal(),
                                               ϵ_t = IID(Normal(0, 0.1))),
                                     1, WindowRule(20:30))],
        ["", "Window"])
    keys(rand(as_turing_model(A7, 40)))
    # VarName[init, ϵ_t, Window.init, Window.ϵ_t]

    Recommendation

    Close as already reachable. No WindowedLatentModel, and no new
    concatenation helper.

    The composition satisfies every property the issue wanted from a modifier: it
    stacks by ordinary composition, its combined effect is the documented
    composition of the parts, the effect slot takes a constant, a callable or a
    latent process, and a scenario comparison is a change of members around the
    same base process. It also stays inside the documented role structure, since
    nothing reaches into another process's parameters.

    Two ergonomic costs are real but do not justify a type. The window lives in an
    anonymous closure, so it is invisible in the model tree:

    CombineLatentModels
    ├─ models[1]: RandomWalk
    │  └─ ϵ_t: IID
    └─ models[2]: PrefixLatentModel
       └─ model: TransformLatentModel
          └─ model: Intercept
    

    And the gating expression is repeated at every call site.

    If either becomes a nuisance, the smallest fix is a helper function beside
    broadcast_dayofweek and broadcast_weekly in
    src/latent_models/manipulators/broadcast/helpers.jl, returning route 7's
    composition. It adds no supertype and no new composition semantics, and it
    generates the effect over the window rather than over the whole series. Names
    worth considering: broadcast_window, in_window, on_window.
    broadcast_window is the better one — it names the family it belongs to and
    sits alphabetically with its siblings, whereas in_window reads like a
    predicate. That is a separate, optional ergonomics change, not this issue.

    A side finding: RandomWalk's positional constructor does not call
    path_prior on ϵ_t, while the keyword constructor does. So
    RandomWalk(Normal(0, 0.1), HalfNormal(0.05)) — the form in this issue's
    example — silently gives a scalar increment and a length-2 path at n = 40.
    Filed as #344.

    Docs and a regression test for the composition are in #345.

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions