joshspeagle / dynesty

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

Resuming runs with Static and Dynamic Sampler #240

Closed Jammy2211 closed 3 years ago

Jammy2211 commented 3 years ago

For the setup I am using, I have the run_nested method in a for loop and call it multiple times for a set number of iterations:

iterations_per_call = 50
total_iterations = 500

while not finished:

    sampler.run_nested(maxcall=iterations_per_call)

    iterations_after_run = np.sum(sampler.results.ncall)

    do_stuff()

    if (total_iterations == iterations_after_run):
        finished = True

The reason I do this is because the do_stuff function above gives me on-the-fly visualization, results, etc and it allows me to cancel the job and resume where I left off (using a pickled Dynesty sampler instance).

The above code seems to work fine for the Static sampler and I have been using it for a long time with no issue.

However, I just had a go at using the Dynamic sampler (which I typically have not used much) and have found that encasing it in the loop above does not seem to be valid. Specifically, after the first run_nested call runs for a total of iterations_per_call, the subsequent calls do not do any new samplings.

I can reduce this problem down to simply running run_nested for the Dynamic sampler twice, back to back, and noting that the second call does not update the sampling. The only way to get the Dynamic sampler to run for a large number of calls is therefore setting iterations_per_call very high and disabling on-the-fly outputs.

My question is, does this behaviour make sense and if so, is there any easy way I can get on the fly behaviour from the Dynamic sampler? Or should I disable this feature for that sampler.

joshspeagle commented 3 years ago

Sorry about the delay in getting back to you. This might be due to the more complicated internal logic related to sample_initial and sample_batch. Let me look into it tomorrow/Friday and see what I can find.

segasai commented 3 years ago

Hi @Jammy2211

If you want to run dynamic nested sampling in a loop you should be doing something like this: rather than do run_nested in a loop (as run_nested indeed does the initial sampling + batches)

import xtest_dyn
import dynesty

sampler = dynesty.DynamicNestedSampler(xtest_dyn.loglike_egg,
                                       xtest_dyn.prior_transform_egg,
                                       2,
                                       nlive=500)
sampler.run_nested(maxcall=1000)
for i in range(100):
    sampler.add_batch(nlive=250, maxcall=1000)

The code shown above requires the most recent version of dynesty (from github main)