Skip to content
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

Deepcopy adaptor before starting sampling #382

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Nov 5, 2024

The implementation of sample() in AdvancedHMC mutates the adaptor passed in, which is both unintuitive (it's sample not sample!) and also undocumented (see #379).

This PR:

  • renames sample() to sample_mutating_adaptor() (other suggestions for names welcome – I didn't want to use sample! as that has another meaning)
  • makes a new version of sample() which performs a deepcopy of the adaptor before handing over to sample_mutating_adaptor()
  • adds tests
  • also updates Random.GLOBAL_RNG to Random.default_rng(), which I think is the proper modern method of accessing the global PRNG state.

Draft for now to check that CI passes + also to wait for #381 to be merged. (or rejected 😱 )

@penelopeysm
Copy link
Member Author

penelopeysm commented Nov 5, 2024

I have this terrible fear that somebody out there might have a workflow which relies on the adaptor being mutated in a predictable manner (https://xkcd.com/1172/)

.... Of course, it's in the tests 😄

adaptor = AdvancedHMC.make_adaptor(nuts, metric, integrator)
samples, stats = sample(h, κ, θ_init, n_samples, adaptor, n_adapts; verbose = false)
return (samples = samples, stats = stats, adaptor = adaptor)

@penelopeysm penelopeysm force-pushed the py/random branch 2 times, most recently from 39a4c24 to 9998e10 Compare November 5, 2024 02:07
This avoids the unintuitive behaviour seen in #379
@sefffal
Copy link

sefffal commented Nov 5, 2024

Thanks @penelopeysm !

I have this terrible fear that somebody out there might have a workflow which relies on the adaptor being mutated in a predictable manner (https://xkcd.com/1172/)

Despite opening #379 (comment) I'm actually longtimeuser4 in this meme haha...

I inspect the adaptor sometimes after sampling in order to see the mass matrix it adapts.
But I admit that the adaptor getting mutated is perhaps unexpected.

Copy link
Member

@torfjelde torfjelde left a comment

Choose a reason for hiding this comment

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

IMO we should probably just drop this sample impl in favour of AbstractMCMC.jl's sample 🤷

It's more of a relic from before we had that implementation, and so we might now be at a point where it's not worht keeping it around anymore.

Comment on lines +111 to +113
Base.isnan(v::DualValue) = any(isnan, v.value) || any(isnan, v.gradient)
Base.isnan(v::AbstractVecOrMat) = any(isnan, v)
Base.isnan(z::PhasePoint) = isnan(z.ℓπ) || isnan(z.ℓκ)
Copy link
Member

Choose a reason for hiding this comment

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

Seems unrelated?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, that's a hangover from #381 because I was a bit lazy to fix the commit history

Copy link
Member Author

Choose a reason for hiding this comment

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

Realised this one has a double bump in Project.toml too. - let's decide on that PR first then I'll modify accordingly

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.

3 participants