Skip to content

Conditioning on submodel variables #857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
penelopeysm opened this issue Mar 22, 2025 · 2 comments · Fixed by #892
Closed

Conditioning on submodel variables #857

penelopeysm opened this issue Mar 22, 2025 · 2 comments · Fixed by #892
Assignees
Labels
bug Something isn't working

Comments

@penelopeysm
Copy link
Member

penelopeysm commented Mar 22, 2025

This behaviour with submodels is all very reasonable:

using DynamicPPL, Distributions

@model function f()
    x ~ Normal()
    y ~ Normal()
end
@model function g()
    a ~ to_submodel(f())
end
keys(VarInfo(g()))
# 2-element Vector{VarName{sym, typeof(identity)} where sym}:
#  a.x
#  a.y

Now, let's say we wanted to condition x in the inner model. We can do that from the very outermost layer, by conditioning the model g(). When looked at from the outside, the x in the inner model is actually var"a.x", so that's what we need to use in the conditioning values. We see that this works perfectly:

cg = g() | (@varname(a.x => 1)
keys(VarInfo(cg))
# 1-element Vector{VarName{Symbol("a.y"), typeof(identity)}}:
#  a.y

Now if we instead wanted to condition the submodel itself (rather than the outermost model), one should expect that we can do that without prefixing. However, it doesn't work:

@model function h()
    a ~ to_submodel(f() | (x = 1,))
end
keys(VarInfo(h()))
# 2-element Vector{VarName{sym, typeof(identity)} where sym}:
#  a.x
#  a.y

To condition on the inner model, you still have to include the prefix:

@model function h2()
    a ~ to_submodel(f() | (@varname(a.x => 1)
end
keys(VarInfo(h2()))
# 1-element Vector{VarName{Symbol("a.y"), typeof(identity)}}:
#  a.y

This is quite counterintuitive and opens up things like this:

cf = f() | (x = 1,)
keys(VarInfo(cf)) # [y]

@model function h3()
    a ~ to_submodel(cf)
end
keys(VarInfo(h3())) # expected [a.y]; but this is [a.x, a.y]

(Note that old @submodel had the same issue.)

I didn't test fix; it might have the same problem.

@penelopeysm penelopeysm added the bug Something isn't working label Mar 22, 2025
@penelopeysm
Copy link
Member Author

penelopeysm commented Mar 22, 2025

this is illustrative

using DynamicPPL, Distributions
@model function f()
    @show "inner, $(__context__)"
    x ~ Normal()
    y ~ Normal()
    return (x, y)
end
@model function g()
    @show "outer, $(__context__)"
    a ~ to_submodel(f() | (@varname(x) => 1))
end
model = g()

julia> model()
"outer, $(__context__)" = "outer, SamplingContext{SampleFromPrior, DefaultContext, Random.TaskLocalRNG}(Random.TaskLocalRNG(), SampleFromPrior(), DefaultContext())"
"inner, $(__context__)" = "inner, PrefixContext{:a, SamplingContext{SampleFromPrior, ConditionContext{Dict{VarName{:x, typeof(identity)}, Int64}, DefaultContext}, Random.TaskLocalRNG}}(SamplingContext{SampleFromPrior, ConditionContext{Dict{VarName{:x, typeof(identity)}, Int64}, DefaultContext}, Random.TaskLocalRNG}(Random.TaskLocalRNG(), SampleFromPrior(), ConditionContext(Dict(x => 1), DefaultContext())))"
(-0.6695328944144235, 0.1774662210135376)

The simplest solution might be to make to_submodel prefix all conditioned/fixed values with its prefix too (?)

return if is_rhs_model(right)
# Prefix the variables using the `vn`.
rand_like!!(
right,
should_auto_prefix(right) ? PrefixContext{Symbol(vn)}(context) : context,
vi,
)
else

basically here we need to dig into right, get its model, get its context, and modify the values inside any ConditionContext/FixedContext as well (i don't think there's code to do that)

This was referenced Apr 16, 2025
@penelopeysm penelopeysm self-assigned this Apr 16, 2025
penelopeysm referenced this issue Apr 24, 2025
* Release 0.36

* AbstractPPL 0.11 + change prefixing behaviour (#830)

* AbstractPPL 0.11; change prefixing behaviour

* Use DynamicPPL.prefix rather than overloading

* Remove VarInfo(VarInfo, params) (#870)

* Unify `{untyped,typed}_{vector_,}varinfo` constructor functions (#879)

* Unify {Untyped,Typed}{Vector,}VarInfo constructors

* Update invocations

* NTVarInfo

* Fix tests

* More fixes

* Fixes

* Fixes

* Fixes

* Use lowercase functions, don't deprecate VarInfo

* Rewrite VarInfo docstring

* Fix methods

* Fix methods (really)

* Link varinfo by default in AD testing utilities; make test suite run on linked varinfos (#890)

* Link VarInfo by default

* Tweak interface

* Fix tests

* Fix interface so that callers can inspect results

* Document

* Fix tests

* Fix changelog

* Test linked varinfos

Closes #891

* Fix docstring + use AbstractFloat

* Fix `condition` and `fix` in submodels (#892)

* Fix conditioning in submodels

* Simplify contextual_isassumption

* Add documentation

* Fix some tests

* Add tests; fix a bunch of nested submodel issues

* Fix fix as well

* Fix doctests

* Add unit tests for new functions

* Add changelog entry

* Update changelog

Co-authored-by: Hong Ge <[email protected]>

* Finish docs

* Add a test for conditioning submodel via arguments

* Clean new tests up a bit

* Fix for VarNames with non-identity lenses

* Apply suggestions from code review

Co-authored-by: Markus Hauru <[email protected]>

* Apply suggestions from code review

* Make PrefixContext contain a varname rather than symbol (#896)

---------

Co-authored-by: Hong Ge <[email protected]>
Co-authored-by: Markus Hauru <[email protected]>

---------

Co-authored-by: Markus Hauru <[email protected]>
Co-authored-by: Hong Ge <[email protected]>
Co-authored-by: Markus Hauru <[email protected]>
@penelopeysm
Copy link
Member Author

Closed by #892, released in 0.36 #829

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant