joshspeagle / dynesty

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

Bug with maxiter/maxcall and dynamic sampling #392

Closed segasai closed 2 years ago

segasai commented 2 years ago

While running the notebooks I stumbled up on this bug:

  File "/tmp/qq.py", line 28, in <module>
    dsampler2.run_nested(maxiter=50000, use_stop=False)
  File "/home/skoposov/pyenv38/lib/python3.8/site-packages/dynesty/dynamicsampler.py", line 1810, in run_nested
    passback = self.add_batch(
  File "/home/skoposov/pyenv38/lib/python3.8/site-packages/dynesty/dynamicsampler.py", line 1973, in add_batch
    for cur_results in self.sample_batch(nlive_new=nlive,
  File "/home/skoposov/pyenv38/lib/python3.8/site-packages/dynesty/dynamicsampler.py", line 1392, in sample_batch
    for it, results in enumerate(batch_sampler.add_live_points()):
  File "/home/skoposov/pyenv38/lib/python3.8/site-packages/dynesty/sampler.py", line 407, in add_live_points
    logvols = self.saved_run.D['logvol'][-1]
IndexError: list index out of range

This can be reproduced with this:

mport dynesty
import numpy as np
from numpy import linalg

rstate = np.random.default_rng(513)
ndim = 3
C2 = np.identity(ndim)
Cinv2 = linalg.inv(C2)
lnorm2 = -0.5 * (np.log(2 * np.pi) * ndim + np.log(linalg.det(C2)))

def loglikelihood2(x):
    """Multivariate normal log-likelihood."""
    return -0.5 * np.dot(x, np.dot(Cinv2, x)) + lnorm2

# prior transform
def prior_transform(u):
    return 10. * (2. * u - 1.)

dsampler2 = dynesty.DynamicNestedSampler(loglikelihood2,
                                         prior_transform,
                                         ndim=3,
                                         bound='single',
                                         sample='unif',
                                         rstate=rstate)
dsampler2.run_nested(maxiter=50000, use_stop=False)
dres2 = dsampler2.results

This can be explained by

I this patch I fix the issue by fudging the logvol/logl arrays in the batch_sampler. This is not a pretty solution, but the best I could come up with.

coveralls commented 2 years ago

Pull Request Test Coverage Report for Build 3060227232


Totals Coverage Status
Change from base Build 3054862301: 0.02%
Covered Lines: 3843
Relevant Lines: 4242

💛 - Coveralls