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

Improve (hopefully) how HybridGibbs uses samplers state and history #503

Merged
merged 18 commits into from
Sep 10, 2024

Conversation

nabriis
Copy link
Collaborator

@nabriis nabriis commented Sep 2, 2024

Closes #483.

This PR makes it such that HybridGibbs will extract each of its samplers state and history, then reinitialize the sampler with a new target, then set the sampler state and history from earlier.

This reason we have to do this is because changing .target will not always correctly set all internal variables of the samplers that depend on the target.

Also thanks to Silja, ensured that acc rate is stored correctly in HybridGibbs.

Suggestion for review if method works

Use CWMH and compare with HybridGibbs using MH. Are the results similar? Test both warmup and sampling.

Silja reviewed and tested. Result OK.

@siljachr
Copy link
Collaborator

siljachr commented Sep 4, 2024

The MH sampler did not append to self._acc, and therefore self._acc stayed at the initial value 1, which meant that the tune method increased the step length to the maximum (i.e. 1). I have pushed a small change that fixes this.

In line 220 of cuqi/experimental/mcmc/_gibbs, the skip_len of the tune method is hardcoded to 1, meaning that the tuning of the scale parameter is always based on only the latest self._acc, i.e. either 0 or 1. This is not sensible for tuning. I suggest either using skip_len = max(int(0.1 * Nb), 1) which is the default in the warmup method in cuqi/experimental/mcmc/_sampler. Or letting it be user defined, again similar to the implementation in the warmup method in cuqi/experimental/mcmc/_sampler.

@siljachr
Copy link
Collaborator

siljachr commented Sep 4, 2024

The MH sampler did not append to self._acc, and therefore self._acc stayed at the initial value 1, which meant that the tune method increased the step length to the maximum (i.e. 1). I have pushed a small change that fixes this.

In line 220 of cuqi/experimental/mcmc/_gibbs, the skip_len of the tune method is hardcoded to 1, meaning that the tuning of the scale parameter is always based on only the latest self._acc, i.e. either 0 or 1. This is not sensible for tuning. I suggest either using skip_len = max(int(0.1 * Nb), 1) which is the default in the warmup method in cuqi/experimental/mcmc/_sampler. Or letting it be user defined, again similar to the implementation in the warmup method in cuqi/experimental/mcmc/_sampler.

I fixed an issue concerning how I first implemented appendence to self._acc. Now implemented in HybridGibbs instead of MH so it is also compatible with regular sampling (non-Gibbs), which already has appendence to self._acc implemented.

I have added the option of setting the tune_freq for HybridGibbs sampling, so it is no longer hard-coded. The implementation is equivalent to tune_freq in cuqi/experimental/mcmc/_sampler.

I have no further comments or concerns related to storing and using the acceptance rate in HybridGibbs :)

Copy link
Contributor

@amal-ghamdi amal-ghamdi left a comment

Choose a reason for hiding this comment

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

Super!, many thanks @nabriis and @siljachr. Looks great.

I have a few comments. Feel free to address as you see fit.

cuqi/experimental/mcmc/_gibbs.py Outdated Show resolved Hide resolved
cuqi/experimental/mcmc/_gibbs.py Outdated Show resolved Hide resolved
cuqi/experimental/mcmc/_gibbs.py Outdated Show resolved Hide resolved
cuqi/experimental/mcmc/_gibbs.py Show resolved Hide resolved
cuqi/experimental/mcmc/_gibbs.py Outdated Show resolved Hide resolved
tests/zexperimental/test_mcmc.py Outdated Show resolved Hide resolved
Copy link
Contributor

@chaozg chaozg left a comment

Choose a reason for hiding this comment

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

Thanks @nabriis and @siljachr for this nice PR. I just have a tiny reminder that the example in the docstring of HybridGibbs nees a bit update as it still follows the old way of sampling. I mean samples = sampler.sample(Ns=1000, Nb=200) here.

cuqi/experimental/mcmc/_gibbs.py Outdated Show resolved Hide resolved
tests/zexperimental/test_mcmc.py Outdated Show resolved Hide resolved
Copy link
Contributor

@chaozg chaozg left a comment

Choose a reason for hiding this comment

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

LGTM. Regarding the issue of the example at the docstring of HybridGibbs, which is of course out the scope of this PR, I took the liberty of creating an issue to track it: #511

Copy link
Contributor

@amal-ghamdi amal-ghamdi left a comment

Choose a reason for hiding this comment

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

Thank you @nabriis, I just added one comment regarding printing a warning for NUTS.

Copy link
Contributor

@amal-ghamdi amal-ghamdi left a comment

Choose a reason for hiding this comment

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

LGTM!

@nabriis nabriis merged commit 528481d into main Sep 10, 2024
6 checks passed
@nabriis nabriis deleted the fix_483_acc_rate_HybridGibbs branch September 10, 2024 20:51
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.

Acc rate not stored in HybridGibbs sampler
4 participants