joshspeagle / dynesty

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

Get (metadata of) nearby live points? #420

Closed mvsoom closed 1 year ago

mvsoom commented 1 year ago

Just a quick question: is it possible to get nearby live points during the nested sampling run?

And is it possible to attach some metadata to the likelihood return value?

Context: I am thinking to use a preconditioning matrix in a linear solve problem inside the likelihood evaluation. The preconditioner would just be the already-solved-for inverse design matrix of the nearest point (which could be the current point at the last likelihood evaluation). That inverse matrix would then be stored as the point's metadata together with its log likelihood value, to be read by other live points in later iterations.

segasai commented 1 year ago

For the second question: https://dynesty.readthedocs.io/en/stable/quickstart.html#saving-auxiliary-information-from-log-likelihood-function

For the first one I think you would need to manually access the parent sampler and the live-points stored there. (there is no formal API for this). https://github.com/joshspeagle/dynesty/blob/201c0cd8245acddf2f8b05feb06913697c5ef7ea/py/dynesty/sampler.py#L92

mvsoom commented 1 year ago

Thanks for the answer! Sweet that you can attach a blob. Before I close this, do you have any recommendations as how to access the parent sampler from within the log likelihood functions? And, is there perhaps an easier way to just access the previous point in an MCMC walk scenario? (Since that one will typically be close to the current position.)

segasai commented 1 year ago

regarding the accessing of previous pts, if things are not threaded this would do

import numpy as np

import dynesty

# Classic eggbox problem
tmax = 5. * np.pi

class cache:
    sampler = None

def loglike(x):
    if cache.sampler is not None:
        print(cache.sampler.live_u)
    return -0.5 * np.sum(x**2)

def prior(x):
    return 20 * x - 10

sampler = dynesty.NestedSampler(loglike,
                                prior,
                                2,
                                nlive=200,
                                bound='multi',
                                sample="unif")
cache.sampler = sampler
sampler.run_nested(print_progress=True)

in the threaded case, I don't know. you need to think yourself.

mvsoom commented 1 year ago

OK, thanks!