Skip to content

sample with LogDensityFunction: part 2 - ess.jl + mh.jl #2590

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

Draft
wants to merge 10 commits into
base: py/ldf-hmc
Choose a base branch
from

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Jun 16, 2025

See #2555

Pending #2588

@penelopeysm penelopeysm changed the base branch from main to py/ldf-hmc June 16, 2025 15:07
Copy link
Contributor

Turing.jl documentation for PR #2590 is available at:
https://TuringLang.github.io/Turing.jl/previews/PR2590/

Copy link

codecov bot commented Jun 16, 2025

Codecov Report

Attention: Patch coverage is 38.09524% with 26 lines in your changes missing coverage. Please review.

Project coverage is 36.19%. Comparing base (e08f548) to head (ec885a4).
Report is 1 commits behind head on py/ldf-hmc.

Files with missing lines Patch % Lines
src/mcmc/ess.jl 5.55% 17 Missing ⚠️
src/mcmc/mh.jl 65.00% 7 Missing ⚠️
src/mcmc/abstractmcmc.jl 50.00% 2 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           py/ldf-hmc    #2590      +/-   ##
==============================================
- Coverage       42.32%   36.19%   -6.13%     
==============================================
  Files              22       22              
  Lines            1498     1478      -20     
==============================================
- Hits              634      535      -99     
- Misses            864      943      +79     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@penelopeysm penelopeysm force-pushed the py/ldf-ess branch 2 times, most recently from 84e8b7e to 393234b Compare June 16, 2025 20:57
@penelopeysm penelopeysm force-pushed the py/ldf-ess branch 2 times, most recently from fa0f068 to 22c2e9a Compare June 17, 2025 12:37
@penelopeysm penelopeysm changed the title sample with LogDensityFunction: part 2 - ess.jl sample with LogDensityFunction: part 2 - ess.jl + mh.jl Jun 17, 2025
Comment on lines 79 to 83
ctx = if ldf.context isa SamplingContext
ldf.context
else
SamplingContext(rng, spl)
SamplingContext(rng, spl, ldf.context)
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise the existing context won't be obeyed

Comment on lines -142 to -153
# Some of the proposals require working in unconstrained space.
transform_maybe(proposal::AMH.Proposal) = proposal
function transform_maybe(proposal::AMH.RandomWalkProposal)
return AMH.RandomWalkProposal(Bijectors.transformed(proposal.proposal))
end

function MH(model::Model; proposal_type=AMH.StaticProposal)
priors = DynamicPPL.extract_priors(model)
props = Tuple([proposal_type(prop) for prop in values(priors)])
vars = Tuple(map(Symbol, collect(keys(priors))))
priors = map(transform_maybe, NamedTuple{vars}(props))
return AMH.MetropolisHastings(priors)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code was broken for a long time and nobody bothered to update it. the idea would be that this would return an AMH.MetropolisHastings which then needed to be wrapped in an ExternalSampler. however the external sampler would break (see tests below) I just removed it because it wasn't documented and wasn't working.

Comment on lines -42 to -48
# s6 = externalsampler(MH(gdemo_default, proposal_type=AdvancedMH.RandomWalkProposal))
# c6 = sample(gdemo_default, s6, N)

# NOTE: Broken because MH doesn't really follow the `logdensity` interface, but calls
# it with `NamedTuple` instead of `AbstractVector`.
# s7 = externalsampler(MH(gdemo_default, proposal_type=AdvancedMH.StaticProposal))
# c7 = sample(gdemo_default, s7, N)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these were the corresponding tests

Comment on lines -228 to -229
@testset "MH link/invlink" begin
vi_base = DynamicPPL.VarInfo(gdemo_default)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests covered by requires_unconstrained_space

Comment on lines -283 to +284
check_numerical(chain, [:s, :m], [mean(InverseGamma(2, 3)), 0]; atol=0.3)
@test mean(chain[:a]) ≈ 0.0 atol = 0.1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was annoyingly difficult to get right. with the gdemo model i had to bump this up to 50000 samples to get it to work reasonably often. I replaced it with a simpler model for which it's obvious when it's being sampled from the prior (a = 0) rather than the posterior (a = 2.5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant