Turing.jl icon indicating copy to clipboard operation
Turing.jl copied to clipboard

Extension for MarginalLogDensities.jl

Open torfjelde opened this issue 1 year ago • 5 comments

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

torfjelde avatar Dec 05 '24 08:12 torfjelde

A bit uncertain if we should export this marginalize or hide it for now.

torfjelde avatar Dec 05 '24 08:12 torfjelde

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

torfjelde avatar Dec 05 '24 08:12 torfjelde

We need https://github.com/TuringLang/DynamicPPL.jl/pull/738 to go through before this will work :+1:

torfjelde avatar Dec 05 '24 18:12 torfjelde

I'll have a look at this now; should be good now that the above PR has been merged 👍

torfjelde avatar Jan 13 '25 13:01 torfjelde

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?

seabbs avatar Jun 05 '25 19:06 seabbs

@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).

penelopeysm avatar Aug 12 '25 14:08 penelopeysm

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.

ElOceanografo avatar Aug 18 '25 23:08 ElOceanografo

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.

ElOceanografo avatar Aug 22 '25 04:08 ElOceanografo

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.

ElOceanografo avatar Aug 25 '25 08:08 ElOceanografo