joshspeagle / dynesty

Dynamic Nested Sampling package for computing Bayesian posteriors and evidences
https://dynesty.readthedocs.io/
MIT License
347 stars 76 forks source link

refactor slice sampling #271

Closed segasai closed 3 years ago

segasai commented 3 years ago

The current slice samplng code still gave me the problems like this

RuntimeError: Slice sampler has failed to find a valid point. Some useful output quantities:
u: [0.43015122 0.26744562 0.9999996  0.69852869 0.51943458 0.99987344]
u_left: [0.43015122 0.26744562 0.9999996  0.69852869 0.51943458 0.99987344]
u_right: [0.43015122 0.26744562 0.9999996  0.69852869 0.51943458 0.99987344]
u_hat: [-2.77555756e-16 -5.55111512e-17  0.00000000e+00 -1.11022302e-16
 -1.11022302e-16  0.00000000e+00]
u_prop: [0.43015122 0.26744562 0.9999996  0.69852869 0.51943458 0.99987344]
loglstar: 549499.5271041454
logl_prop: 549499.5271040456
axis: [-5.43760729e-05 -1.47307934e-06  9.95810121e-08 -2.81106958e-06
 -9.69705233e-06  8.84660396e-09]
axlen: 5.5325149793425564e-05
s: 0.0.

It turns out that the reason is that during the slice sampling iterations due to floating point errors, it may happen that the original starting point is not more in the slice. The proposed patch fixes that and more:

refactor slice sampling by separating the single slice sampling step in a separate function that allows sharing the code between _slice and _rslice samplers. Also the slice sampling is now more numerically stable due to the fact that we are working in the 'coordinate system' of u0 + x* direction, i.e. we are always strictly guaranteed to have an initial point at 0 and are not affected by the precision loss

coveralls commented 3 years ago

Pull Request Test Coverage Report for Build 883029777


Changes Missing Coverage Covered Lines Changed/Added Lines %
py/dynesty/sampler.py 1 2 50.0%
py/dynesty/sampling.py 69 70 98.57%
py/dynesty/dynesty.py 12 16 75.0%
<!-- Total: 82 88 93.18% -->
Totals Coverage Status
Change from base Build 880284049: -0.3%
Covered Lines: 3343
Relevant Lines: 4665

💛 - Coveralls
segasai commented 3 years ago

This is ready for review/merge. (And with those set of changes I was finally able to get through my entire set of 60k objects without crashes)