Extension for MarginalLogDensities.jl
This PR adds an extension for MarginalLogDensities.jl, allowing one to call marginalize on a Model, resulting in a MarginalLogDensity from MarginalLogDensities.jl.
Example:
julia> using Turing, MarginalLogDensities
julia> @model function demo()
x ~ Normal(0, 1)
y ~ Normal(x, 1)
end
demo (generic function with 2 methods)
julia> model = demo();
julia> # Marginalize out `x`.
marginalized = marginalize(model, [@varname(x)]);
julia> # Compute the marginal log-density of `y = 0.0`.
abs(marginalized([0.0]) - logpdf(Normal(0, √2), 0.0)) < 1e-1
true
Ref: #2398
A bit uncertain if we should export this marginalize or hide it for now.
Note that this will fail until https://github.com/ElOceanografo/MarginalLogDensities.jl/pull/36 has made its way to the public (i.e. v0.3.6). Not entirely certain why this hasn't happened yet (it is in the registry :confused: )
EDIT: https://github.com/ElOceanografo/MarginalLogDensities.jl/pull/36#issuecomment-2519654423
We need https://github.com/TuringLang/DynamicPPL.jl/pull/738 to go through before this will work :+1:
I'll have a look at this now; should be good now that the above PR has been merged 👍
Found this after reading the recent Stan release and losing myself in a rabbit hole. This would be super handy for some modular fitting stuff as I think said elsewhere. What is the status? Anything aside from cheerleading to do?
@ElOceanografo Hello! Are you still interested in this? I'm happy to try to pick it up (although I will probably need a bit of time to read the previous discussions + wrap my head around the stats).
Yes, I'm still interested in this! I have not tried running this code myself yet, but based on a quick skim it looks like it's doing what I'd expect. Let me know if I can help out in any way.
Just finished digging into this and made a PR to this branch that should get thing working again: https://github.com/TuringLang/Turing.jl/pull/2662
If it's simpler, I could also just open it as a new PR.
Opened a new version of this at #2664 based on the up-to-date main branch, so this PR can be closed in favor of that one.