joshspeagle / dynesty

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

Usefulness of reflective/periodic dimensions #410

Open segasai opened 1 year ago

segasai commented 1 year ago

In all the tests that I have done I don't think I have seen any benefits from reflective or periodic conditions which makes me wonder whether they are useful at all or not.

Here is the example where in theory periodic conditions should make sense, but here for rwalk sampler the number of function calls is actually smaller when not using periodic option and for the rslice it's exactly the same. Only for unif sampler the usage of periodic leads to 20% smaller number of ncalls

import numpy as np
import dynesty

nlive = 100
printing = True
ndim = 2

def loglike(x):
    return -0.5 * x[1]**2 - 10 * np.cos(np.pi * x[0])

def prior_transform(x):
    return (2 * x - 1)

def doit():
    dynamic = True
    sampler = 'rwalk'
    # hard test of dynamic sampler with high dlogz_init and small number
    # of live points
    per = [0]
    per = None
    rstate = np.random.default_rng(1)
    if dynamic:
        dns = dynesty.DynamicNestedSampler(loglike,
                                           prior_transform,
                                           ndim,
                                           nlive=nlive,
                                           sample=sampler,
                                           periodic=per,
                                           rstate=rstate)
    else:
        dns = dynesty.NestedSampler(loglike,
                                    prior_transform,
                                    ndim,
                                    nlive=nlive,
                                    sample=sampler,
                                    periodic=per,
                                    rstate=rstate)
    dns.run_nested(print_progress=printing)
    return dns.results

if __name__ == '__main__':
    res = doit()

Also it is clear that we need an example that should serve as test-case for periodic doing anything useful (like the one given here; but maybe there is a better one).

Thoughts ?

ColmTalbot commented 1 year ago

The number of likelihood evaluations is essentially a deterministic function of the MCMC length + number of live points + likelihood for rwalk, so I would expect the number of calls per run to not be strongly affected.

What is strongly affected is the quality of the convergence. Did you look at the posteriors in this case? In my experience, the periodic boundaries (for periodic parameters) significantly decrease the required MCMC length to get unbiased posteriors.

segasai commented 1 year ago

That is a fair point.

The question is what kind of statistic we should look into here to have an objective characterisation of whether these parameters improve things.