joshspeagle / dynesty

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

Access index of original samples in resampled chain #418

Closed dilpath closed 1 year ago

dilpath commented 1 year ago

Firstly, thanks for the nice package! I've been implementing an interface in the pyPESTO [1] package, and so far it's been very useful! For it to work "well" there, I would need a couple of adjustments, hence this and the next issue.

Dynesty version pip install dynesty==2.0.3

Describe the bug dynesty provides the ability to get an MCMC-like chain with sampler.results.samples_equal() [2]. However, this doesn't provide the corresponding sampler.results.logl values too. It would be great if there was some index_resampled=sampler.results.samples_equal_index() method that provides the index of samples returned by sampler.results.samples_equal(). I could then get corresponding samples and log-likelihood with sampler.results.samples[index_resampled] and sampler.results.logl[index_resampled].

The alternative is to construct index_resampled by searching sampler.results.samples for the samples returned by sampler.results.samples_equal, get their index, then use that as index_resampled. However, my attempt at this was quite slow.

[1] https://github.com/ICB-DCM/pyPESTO [2] https://github.com/joshspeagle/dynesty/blob/a24b5c38e553dd47d4d5efe9e80228879266c08d/py/dynesty/utils.py#L726-L734

segasai commented 1 year ago

I agree that's a bit of oversight. I'll think of a proper solution. But there is a way of getting equally sampled indices through this

In [1]: xs=np.random.normal(size=(1000,3))

In [2]: wts = np.random.uniform(size=1000);wts=wts/wts.sum()

In [3]: import dynesty.utils

In [4]: indx=dynesty.utils.resample_equal(np.arange(1000),wts)

Now indx will be indices that you can use to index the samples, log-likelihoods etc

dilpath commented 1 year ago

Works well! Thanks for the quick response.