dynesty

raw JSON →
3.0.0 verified Sat May 09 auth: no python

A dynamic nested sampling package for computing Bayesian posteriors and evidences. Current version 3.0.0 (released 2024-06-10). Requires Python >=3.8. Active development with major refactoring in v3.0.0 for extensibility and speed.

pip install dynesty
error AttributeError: module 'dynesty' has no attribute 'NestedSampler'
cause Import path incorrect or old dynesty version (<1.0).
fix
Use correct import: from dynesty import NestedSampler
error ValueError: The prior transform must return an array of shape (ndim,) or (npoints, ndim)
cause Prior transform returns wrong shape (e.g., scalar or higher dimension).
fix
Ensure prior_transform returns a 1D array of length ndim.
error TypeError: run_nested() missing 1 required positional argument: 'dlogz'
cause In dynesty v2+, dlogz must be passed as keyword argument (not positional).
fix
Use sampler.run_nested(dlogz=0.5) or other kwargs.
error dynesty.pool.Pool is deprecated
cause The dynesty.pool module was removed in v3.0.0.
fix
Use multiprocessing.Pool directly: pool = multiprocessing.Pool(); sampler = NestedSampler(..., pool=pool)
breaking v3.0.0 refactored the sampler API: custom samplers now need to be provided as class instances (e.g., NestedSampler(sample=RWalkSampler(walks=44))). The previous string-based specification (sample='rwalk') is deprecated.
fix Use sampler classes: from dynesty.sampling import RWalkSampler; NestedSampler(..., sample=RWalkSampler(walks=44))
deprecated The 'npdim' option has been removed since v2.1.4. Prior transforms must return vectors of same dimension as input.
fix Remove npdim parameter; ensure prior_transform returns array of length ndim.
gotcha Resuming a run from a checkpoint created with dynesty <2.1.3 is not possible after v2.1.4. The checkpoint format changed.
fix If you need to resume, finish the run with an older version or restart from scratch.
gotcha When using blob=True, live_points must be a tuple of 4 elements (u, v, logl, blobs). Omitting blobs will cause an error.
fix Provide all four entries when specifying live_points with blobs.

Basic nested sampling run with Gaussian likelihood and uniform prior.

import numpy as np
from dynesty import NestedSampler, utils

# Define a simple prior transform (uniform in [0,1]^2)
def prior_transform(u):
    return 10 * u - 5  # shift to [-5, 5]

# Define a simple Gaussian log-likelihood
def loglikelihood(x):
    return -0.5 * np.sum(x**2)

# Run nested sampling
sampler = NestedSampler(loglikelihood, prior_transform, ndim=2)
sampler.run_nested(dlogz=0.5)
results = sampler.results

# Compute evidence and posterior samples
log_z = results.logz[-1]
weights = np.exp(results.logwt - results.logz[-1])
samples = utils.resample_equal(results.samples, weights)
print(f"log Z = {log_z:.2f}")