Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions src/compose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ function concrete_observations(y::AbstractVector)
T = nonmissingtype(eltype(y))
# A `Vector{Missing}` has no value type to put in the carrier, and nothing
# can be written into it either, so leave it.
# A series with nothing observed in it is the same request as `missing`, a
# predictive draw at that length, so it takes that form whatever its
# element type.
# Any other element type without a `zero` is copied instead, so the caller's
# array is still out of reach.
T === Union{} && return y
all(ismissing, y) && return Vector{Missing}(missing, length(y))
Comment thread
seabbs-bot marked this conversation as resolved.
Resolved
(isconcretetype(T) && T <: Number) || return copy(y)
present = .!ismissing.(y)
value = identity.(coalesce.(y, zero(T)))
Expand Down
15 changes: 9 additions & 6 deletions src/observation_models/ObservationErrorModels/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,17 @@ end
_restore_missing(y) = y
_restore_missing(y::MissingObservations) = map((v, p) -> p ? v : missing, y.value, y.present)

# A blank in a series is marginalised, not drawn, whichever route the series
# arrives by.
# Narrowing it with `concrete_observations` turns a gap into a
# [`MissingObservations`](@ref) carrier that is scored by reading only, and a
# series with nothing observed in it into the predictive form.
# A series reached through a `NamedTuple` field is not a model argument, so
Comment thread
seabbs-bot marked this conversation as resolved.
Resolved
# DynamicPPL neither copies nor promotes it and the `y_t[i] ~ …` sugar would
# write a blank's draw straight back into the caller's array.
# Narrowing it with `concrete_observations` turns any blank into a
# [`MissingObservations`](@ref) carrier that is scored by reading only.
# A series passed as the model's own `y_t` argument already has DynamicPPL's
# copy, so it passes through untouched.
_scored_series(obs_model, y_t, Y_t) = define_y_t(obs_model, y_t, Y_t)
# otherwise write a blank's draw straight back into the caller's array.
function _scored_series(obs_model, y_t, Y_t)
return concrete_observations(define_y_t(obs_model, y_t, Y_t))
end
_scored_series(obs_model, y_t::MissingObservations, Y_t) = y_t
function _scored_series(obs_model, y_t::NamedTuple, Y_t)
y_t.y isa MissingObservations && return y_t.y
Expand Down
13 changes: 7 additions & 6 deletions test/unit/compose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ end
)
@test all(v -> v isa MissingObservations, concrete_observations(nt_gap))

# A fully missing vector is split too: `present` is all `false`, and the
# value vector is a placeholder. Nothing a draw could be written into
# survives, so the caller's array cannot be mutated by scoring it.
# A series with nothing observed in it is the same request as `missing`,
# so it takes the predictive form at its own length rather than a carrier
# that would marginalise the whole series. The fresh array also keeps the
# caller's array out of reach of a draw.
y_allgap = Vector{Union{Missing, Int}}(missing, 3)
allgap = concrete_observations(y_allgap)
@test allgap isa MissingObservations
@test !any(allgap.present)
@test !(eltype(allgap.value) >: Missing)
@test allgap isa Vector{Missing}
@test length(allgap) == 3
@test allgap !== y_allgap

# A `Vector{Missing}` carries no value type to build a carrier from, and
# nothing can be written into it either (the tilde sugar widens it into a
Expand Down
36 changes: 32 additions & 4 deletions test/unit/observation_models/observations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ end
@test isequal(yp, yp_before)
@test !has_y_t(mp)

# A fully blank series in a NamedTuple field is marginalised whole. The
# predictive route is `y = missing`, which sizes the draw off `Y_t`.
# A fully blank series in a NamedTuple field is the predictive route, the
# same as `y = missing`, and the caller's array is still not written into.
ym = Vector{Union{Missing, Int}}(missing, 3)
mm = as_turing_model(BinomialError(), (y = ym, N = trials), fill(0.02, 3))
@test all(ismissing, mm().y_t)
@test all(x -> 0 <= x <= 1000, mm().y_t)
@test all(ismissing, ym)
@test !has_y_t(mm)
@test has_y_t(mm)
sim = as_turing_model(
BinomialError(), (y = missing, N = trials), fill(0.02, 3)
)
Expand Down Expand Up @@ -231,6 +231,34 @@ end
@test !has_y_t(mc)
end

@testitem "a gap in a plain vector is marginalised on every route" begin
using ComposableTuringIDModels, Distributions
using DynamicPPL: VarInfo, logjoint
# The bare error model takes the same contract as a `NamedTuple` field and
# an `IDModel`: a blank leaves the likelihood rather than becoming a latent,
# and the caller's array is not written into.
y = Vector{Union{Missing, Int}}([5, missing, 7])
y_before = copy(y)
Y_t = fill(6.0, 3)
bare = as_turing_model(PoissonError(), y, Y_t)
named = as_turing_model(PoissonError(), (y = y,), Y_t)
@test isempty(keys(VarInfo(bare)))
@test isempty(keys(VarInfo(named)))
ref = logpdf(Poisson(6.0), 5) + logpdf(Poisson(6.0), 7)
@test logjoint(bare, VarInfo(bare)) ≈ ref
@test logjoint(named, VarInfo(named)) ≈ ref
@test isequal(bare().y_t, y_before)
@test isequal(y, y_before)

# A series with nothing observed in it is the predictive route on the bare
# path as well, at its own length.
blank = as_turing_model(
PoissonError(), Vector{Union{Missing, Int}}(missing, 3), Y_t
)
@test length(keys(VarInfo(blank))) == 3
@test all(x -> x isa Integer, blank().y_t)
end

@testitem "define_y_t unpacks counts for vector or NamedTuple data" begin
using ComposableTuringIDModels
Y_t = fill(10.0, 5)
Expand Down
Loading