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

Fix bug in Slice sampler and speedup test_step.py #5816

Merged
merged 5 commits into from
May 30, 2022

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented May 28, 2022

Closes #5815

I changed the logic when creating the initial interval according to MacKay (2005, page 375) [pdf].

image

Before

ql[i] = q[i] - nr.uniform(0, self.w[i])
qr[i] = q[i] + self.w[i]

After

r = nr.uniform()
ql[i] = q[i] - r * self.w[i]
qr[i] = q[i] + (1 - r) * self.w[i]

I think the previous was causing the proposals to be systematically biased towards higher values (right).

Also made sure that we set the boundaries of previous variables to the accepted values, which probably by mistake was only being done during tuning.

# Set qr and ql to the accepted points (they matter for subsequent iterations)
qr[i] = ql[i] = q[i]

If someone more familiar with these can confirm my changes to Slice make sense that would be great.

The sampler now converges in the multivariate example in #5815, and does an increasingly better job with more samples.


Also did some other minor changes, see individual commits for more details.

@codecov
Copy link

codecov bot commented May 28, 2022

Codecov Report

Merging #5816 (d3cf283) into main (7b239bb) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5816      +/-   ##
==========================================
+ Coverage   89.36%   89.38%   +0.01%     
==========================================
  Files          74       74              
  Lines       13757    13769      +12     
==========================================
+ Hits        12294    12307      +13     
+ Misses       1463     1462       -1     
Impacted Files Coverage Δ
pymc/distributions/logprob.py 97.65% <100.00%> (+0.19%) ⬆️
pymc/step_methods/compound.py 93.33% <100.00%> (+0.15%) ⬆️
pymc/step_methods/slicer.py 95.83% <100.00%> (+0.05%) ⬆️
pymc/step_methods/hmc/base_hmc.py 90.47% <0.00%> (+0.79%) ⬆️

@ricardoV94
Copy link
Member Author

ricardoV94 commented May 28, 2022

It seems like this is how the sampler used to behave until commit b988ba9 changed right_bound = left_bound + w -> right_bound = initial_point + w

@michaelosthege
Copy link
Member

It seems like this is how the sampler used to behave until commit b988ba9 changed right_bound = left_bound + w -> right_bound = initial_point + w

Wow that means the Slice sampler was broken since version 3.2.

@ricardoV94
Copy link
Member Author

Wow that means the Slice sampler was broken since version 3.2.

Should we backport the fix to V3?

@michaelosthege
Copy link
Member

Should we backport the fix to V3?

I'm not sure if it's worth it, but an exception telling users to upgrade to v4 should be easy enough.

@ricardoV94
Copy link
Member Author

It's the same amount of work to add an exception or change the 2 lines

Copy link
Member

@michaelosthege michaelosthege left a comment

Choose a reason for hiding this comment

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

LGTM

@ricardoV94
Copy link
Member Author

I should mention something in the Release Notes

@ricardoV94 ricardoV94 force-pushed the step_tests branch 2 times, most recently from 4430d78 to de94c8c Compare May 30, 2022 13:13
1. The previous interval was of the form `[q0 - uniform(0, self.w), q0 + self.w]` which is not symmetric around q0 in expectation, breaking detailed balance.
2. The left/right bound values used for updating earlier variables were only set to the accepted values during tuning. This might have made bug 1 worse.
Avoid recreation of RaveledArrays in inner loops and repeated indexing of `self.w`
The number of draws was set too high to accommodate the worst / buggy samplers (see pymc-devs#5815)
@ricardoV94
Copy link
Member Author

Added Release note

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

Successfully merging this pull request may close these issues.

Slice sampler does not converge in simple MvNormal prior model
2 participants