ns_interfaces#

Wrappers for external nested samplers.

gpry.ns_interfaces.ensure_scalar(x)#
exception gpry.ns_interfaces.NestedSamplerNotInstalledError[source]#

Bases: Exception

Exception to be raised at initialization of any of the interfaces if the NS failed to be imported.

class gpry.ns_interfaces.NSInterface(bounds, verbosity=None)[source]#

Bases: ABC

Meta-class for Nested Sampler interfaces.

abstract set_verbosity(verbose)[source]#

Sets the verbosity of the sampler at run time.

abstract set_prior(bounds)[source]#

Sets the prior used by the nested sampler.

abstract set_precision(**kwargs)[source]#

Sets precision parameters for the nested sampler.

abstract run(logp_func, param_names=None, out_dir=None, seed=None)[source]#

Runs the nested sampler.

param_names (optional, otherwise x_[i] will be used) should be a list of sampled parameter names, or a list of (name, label) tuples. Labels are interpreted as LaTeX but should not include ‘$’ signs.

Must return a tuple (X_MC, y_MC, w_MC, logZ, logZstd).

abstract delete_output(out_dir=None)[source]#

Deletes the last sampler output.

If out_dir specified, deletes the one stored there instead.

static process_out_dir(out_dir, default_prefix='ns_samples', random_if_undefined=True)[source]#

Given an output root out_dir as folder/ or folder/prefix, returns separately the folder path and the file name prefix.

If random_if_undefined is True (default), it returns a random temp folder. Otherwise it raises ValueError.

class gpry.ns_interfaces.InterfacePolyChord(bounds, verbosity=3)[source]#

Bases: NSInterface

Interface for the PolyChord nested sampler, by W. Handley, M. Hobson & A. Lasenby.

See PolyChord/PolyChordLite

set_verbosity(verbose)[source]#

Sets the verbosity of the sampler at run time.

set_prior(bounds)[source]#

Sets the prior used by the nested sampler.

set_precision(**kwargs)[source]#

Sets precision parameters for the nested sampler.

run(logp_func, param_names=None, out_dir=None, seed=None)[source]#

Runs the nested sampler.

param_names (optional, otherwise x_[i] will be used) should be a list of sampled parameter names, or a list of (name, label) tuples. Labels are interpreted as LaTeX but should not include ‘$’ signs.

delete_output(out_dir=None)[source]#

Deletes the last PolyChord output.

If out_dir specified, deletes the one stored there instead.

class gpry.ns_interfaces.InterfaceNessai(bounds, verbosity=3)[source]#

Bases: NSInterface

Interface for the nessai nested sampler, by M.J. Williams, J. Veitch and C. Messenger.

See https://nessai.readthedocs.io

set_verbosity(verbose)[source]#

Sets the verbosity of the sampler at run time.

set_prior(bounds)[source]#

Sets the prior used by the nested sampler.

set_precision(nlive=None, precision_criterion=None, **kwargs)[source]#

Sets precision parameters for the nested sampler.

run(logp_func, param_names=None, out_dir=None, seed=None)[source]#

Runs the nested sampler.

param_names (optional, otherwise x_[i] will be used) should be a list of sampled parameter names, or a list of (name, label) tuples. Labels are interpreted as LaTeX but should not include ‘$’ signs.

delete_output(out_dir=None)[source]#

Deletes last the nessai output.

If out_dir specified, deletes the one stored there instead.

class gpry.ns_interfaces.InterfaceUltraNest(bounds, verbosity=3)[source]#

Bases: NSInterface

Interface for the ultranest nested sampler, by J. Buchner.

See https://johannesbuchner.github.io/UltraNest

set_verbosity(verbose)[source]#

Sets the verbosity of the sampler at run time.

set_prior(bounds)[source]#

Sets the prior used by the nested sampler.

set_precision(nlive=None, precision_criterion=None, num_repeats=None, max_ncalls=None, **kwargs)[source]#

Sets precision parameters for the nested sampler.

run(logp_func, param_names=None, out_dir=None, seed=None)[source]#

Runs the nested sampler.

param_names (optional, otherwise x_[i] will be used) should be a list of sampled parameter names, or a list of (name, label) tuples. Labels are interpreted as LaTeX but should not include ‘$’ signs.

delete_output(out_dir=None)[source]#

Deletes the last ultranest output.

If out_dir specified, deletes the one stored there instead.

class gpry.ns_interfaces.InterfaceBlackJAX(bounds, verbosity=3)[source]#

Bases: NSInterface

Interface for the BlackJAX nested sampler (Handley-lab fork with NS support).

Uses Nested Slice Sampling (NSS) with Hit-and-Run Slice Sampling as the inner kernel. Fully JAX-native, JIT-compilable, and GPU-compatible.

See handley-lab/blackjax

set_verbosity(verbose)[source]#

Sets the verbosity of the sampler at run time.

set_prior(bounds)[source]#

Sets the prior used by the nested sampler.

set_precision(nlive=None, num_repeats=None, max_ncalls=None, precision_criterion=None, nprior=None, num_inner_steps=None, max_steps=None, num_delete=None, **kwargs)[source]#

Sets precision parameters for the nested sampler.

run(logp_func, param_names=None, out_dir=None, seed=None)[source]#

Runs the BlackJAX nested sampler.

Parameters:
  • logp_func (callable) – Log-likelihood function. Takes array X of shape (d,) or (n, d) and returns scalar or array.

  • param_names (list, optional) – Parameter names.

  • out_dir (str, optional) – Not used (BlackJAX is in-memory), but kept for interface compatibility.

  • seed (int, optional) – Random seed.

Returns:

(X_MC, y_MC, w_MC)

Return type:

arrays of samples, log-likelihoods, and normalized weights.

delete_output(out_dir=None)[source]#

BlackJAX is in-memory, nothing to delete.