Skip to content

Nested PrefixContext behaviour is not fully defined #786

@penelopeysm

Description

@penelopeysm
# Setup
using DynamicPPL
p1 = PrefixContext{:a}(DefaultContext())
p2 = PrefixContext{:a}(PrefixContext{:b}(DefaultContext()))
p3 = PrefixContext{:a}(SamplingContext(PrefixContext{:b}(DefaultContext())))
p4 = SamplingContext(PrefixContext{:a}(DefaultContext()))

julia> prefix(p1, @varname(x))
a.x

julia> prefix(p2, @varname(x))
b.a.x

julia> prefix(p3, @varname(x)) 
a.x

julia> prefix(p4, @varname(x))
ERROR: MethodError: no method matching prefix(::SamplingContext{SampleFromPrior, PrefixContext{…}, Random.TaskLocalRNG}, ::VarName{:x, typeof(identity)})
The function `prefix` exists, but no method is defined for this combination of argument types.

Several points:

  1. prefix(p1, @varname(x)) is all good.

  2. It seems to me that it would be more natural for prefix(p2, @varname(x)) to return a.b.x as right now this means that nested submodels have the prefixing occurring in the less intuitive way:

using DynamicPPL, Distributions

@model function subsubmodel()
  x ~ Normal(0,1)
end

@model function submodel()
  x ~ to_submodel(prefix(subsubmodel(), :c), false)
  return x
end

@model function parentmodel()
  x1 ~ to_submodel(prefix(submodel(), :a), false)
  x2 ~ to_submodel(prefix(submodel(), :b), false)
end

keys(VarInfo(parentmodel()))
# 2-element Vector{VarName{sym, typeof(identity)} where sym}:
#  c.a.x
#  c.b.x
  1. The intuitive behaviour for prefix(p3, @varname(x)) would be to prefix with both a and b

  2. prefix(p4, @varname(x)) should traverse the context tree accordingly. This is related to to_submodel does not correctly handle manually specified prefixes when sampling #785, where the underlying problem is that the model is being evaluated with a DebugContext(SamplingContext(PrefixContext...))) but the varnames aren't prefixed.

Metadata

Metadata

Assignees

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