nessai.samplers.importancesampler

Importance nested sampler.

Module Contents

Classes

OrderedSamples

Samples ordered by log-likelihood.

ImportanceNestedSampler

Parameters:

class nessai.samplers.importancesampler.OrderedSamples(strict_threshold: bool = False, replace_all: bool = False, save_log_q: bool = False)

Samples ordered by log-likelihood.

Parameters:
strict_threshold

If true, when adding new samples, only those above the current log-likelihood threshold will be added to the live points.

replace_all

If true, all samples will be remove when calling remove_samples

save_log_q

If true, log_q will be saved when the instance is pickled. This makes resuming faster but increases the disk usage. If false, the values will not be saved and must be recomputed.

property live_points: numpy.ndarray

The current set of live points

property nested_samples: numpy.ndarray

The current set of discarded points

update_log_likelihood_threshold(threshold: float) None

Update the log-likelihood threshold.

Only relevant if using strict_threshold=True.

Parameters:
thresholdfloat

New log-likelihood threshold

sort_samples(samples: numpy.ndarray, *args) numpy.ndarray

Correctly sort new live points.

Parameters:
x

Array to sort

args

Any extra iterables to sort in the same way as x.

add_samples(samples: numpy.ndarray, log_q: numpy.ndarray) None

Add samples the existing samples

add_to_nested_samples(indices: numpy.ndarray) None

Move a set of samples from the live points to the nested samples

remove_samples() int

Remove samples all samples below the current log-likelihood threshold.

Returns:
nint

The number of samples removed

update_evidence() None

Update the evidence estimate given the current samples

finalise() None

Finalise the samples by consuming all of the live points

compute_importance(importance_ratio: float = 0.5)

Compute the importance

Parameters:
importance_factor

Relative importance of the posterior versus the evidence. 1 is only the posterior and 0 is only the evidence.

Returns:
dict

Dictionary containing the total, posterior and evidence importance as a function of iteration.

compute_evidence_ratio(threshold: float | None = None) float

Compute the evidence ratio given the current log-likelihood threshold.

Parameters:
threshold

Log-likelihood threshold to use instead of the current value.

class nessai.samplers.importancesampler.ImportanceNestedSampler(model: nessai.model.Model, nlive: int = 5000, n_initial: int | None = None, output: str | None = None, seed: int | None = None, checkpointing: bool = True, checkpoint_interval: int = 600, checkpoint_on_iteration: bool = False, checkpoint_callback: Callable | None = None, save_existing_checkpoint: bool = False, save_log_q: bool = False, logging_interval: int = None, log_on_iteration: bool = True, resume_file: str | None = None, plot: bool = True, plotting_frequency: int = 5, min_iteration: int | None = None, max_iteration: int | None = None, min_samples: int = 500, min_remove: int = 1, max_samples: int | None = None, stopping_criterion: str = 'ratio', tolerance: float = 0.0, n_update: int | None = None, plot_pool: bool = False, plot_level_cdf: bool = False, plot_trace: bool = True, plot_likelihood_levels: bool = True, plot_training_data: bool = False, plot_extra_state: bool = False, trace_plot_kwargs: dict | None = None, replace_all: bool = False, threshold_method: Literal[entropy, quantile] = 'entropy', threshold_kwargs: dict | None = None, n_pool: int | None = None, pool: Any | None = None, check_criteria: Literal[any, all] = 'any', weighted_kl: bool = False, draw_constant: bool = True, train_final_flow: bool = False, bootstrap: bool = False, close_pool: bool = False, strict_threshold: bool = False, draw_iid_live: bool = True, **kwargs: Any)

Bases: nessai.samplers.base.BaseNestedSampler

Parameters:
model

User-defined model.

nlive

Number of live points.

tolerance

Tolerance for determining when to stop the sampler.

stopping_criterion

Choice of stopping criterion to use.

check_criteria

If using multiple stopping criteria determines whether any or all criteria must be met.

threshold_method

Method for determining new likelihood threshold.

threshold_kwargs

Keyword arguments for function that determines the likelihood threshold.

draw_constant

If specified the sampler will always add a constant number of samples from each proposal whilst removing a variable amount. If False, the the number will depend on the level method chosen. Note that this will override the choice of live points. The number of points draw is set by the live points.

min_samples

Minimum number of samples that are used for training the next normalising flow.

min_remove

Minimum number of samples that can be removed when creating the next level. If less than one, the sampler will stop if the level method determines no samples should be removed.

max_samples

The maximum number of samples at any given iteration. If not specified, then there is no limit to the number of samples.

plot_likelihood_levels

Enable or disable plotting the likelihood levels.

trace_plot_kwargs

Keyword arguments for the trace plot.

strict_thresholdbool

If true, when drawing new samples, only those with likelihoods above the current threshold will be added to the live points. If false, all new samples are added to the live points.

save_log_qbool

Boolean that determines if the log_q array is saved when checkpointing. If False, this can help reduce the disk usage.

property posterior_effective_sample_size: float

The effective sample size of the posterior distribution.

Returns the value for the posterior samples from the resampling step if they are available, otherwise falls back to the samples from the initial sampling.

property samples_entropy: float

Differential entropy of all of the samples (nested + live).

Notes

Compute the Monte Carlo approximation of

\[-\int W(x) \log W(x) dx\]

where \(W(x) = \pi(x)/Q(x)\).

property current_proposal_entropy: float

Differential entropy of the current proposal

property live_points_unit: numpy.ndarray

The current set of live points

property nested_samples_unit: numpy.ndarray

The current set of discarded points

property reached_tolerance: bool

Indicates if tolerance has been reached.

Checks if any or all of the criteria have been met, this depends on the value of check_criteria.

stopping_criterion_aliases

Dictionary of available stopping criteria and their aliases.

static add_fields()

Add extra fields logW and logQ

configure_stopping_criterion(stopping_criterion: str | List[str], tolerance: float | List[float], check_criteria: Literal[any, all]) None

Configure the stopping criterion

get_proposal(subdir: str = 'levels', **kwargs)

Configure the proposal.

configure_iterations(min_iteration: int | None = None, max_iteration: int | None = None) None

Configure the minimum and maximum iterations.

Note: will override any existing values when called.

check_configuration() bool

Check sampler configuration is valid.

Returns true if all checks pass.

populate_live_points() None

Draw the initial live points from the prior.

The live points are automatically sorted and assigned the iteration number -1.

initialise() None

Initialise the nested sampler.

Draws live points, initialises the proposal.

initialise_history() None

Initialise the dictionary to store history

update_history() None

Update the history dictionary

determine_threshold_quantile(samples: numpy.ndarray, q: float = 0.8, include_likelihood: bool = False) int

Determine where the next likelihood threshold should be located.

Computes the q’th quantile based on log-likelihood and log-weights.

Parameters:
samplesnp.ndarray

An array of samples

qfloat

Quantile to use. Defaults to 0.8

include_likelihoodbool

If True, the likelihood is included in the weights.

Returns:
int

The number of live points to discard.

determine_threshold_entropy(samples: numpy.ndarray, q: float = 0.5, include_likelihood: bool = False, use_log_weights: bool = True) int

Determine where the next likelihood threshold should be located using the entropy method.

Parameters:
q

Fraction by which to shrink the current level.

include_likelihood

Boolean to indicate whether the likelihood is included in the weights for each samples.

use_log_weights

Boolean to determine if the CDF is computed using the weights or log-weights.

plot_level_cdf(log_likelihood_values: numpy.ndarray, cdf: numpy.ndarray, threshold: float, q: float, filename: str | None = None) matplotlib.figure.Figure | None

Plot the CDF of the log-likelihood

Parameters:
log_likelihood_valuesnp.ndarray

The log-likelihood values for the CDF

cdfnp.ndarray

The CDF to plot

filenameOptional[str]

Filename for saving the figure. If not specified the figure will be returned instead.

Returns:
matplotlib.figure.Figure

Level CDF figure. Only returned when the filename is not specified.

determine_log_likelihood_threshold(samples: numpy.ndarray, method: Literal[entropy, quantile] = 'entropy', **kwargs) int

Determine the next likelihood threshold

Parameters:
samplesnumpy.ndarray

An array of samples

methodLiteral[“entropy”, “quantile”]

The method to use

kwargs

Keyword arguments passed to the function for the chosen method.

Returns:
float

The log-likelihood threshold

add_new_proposal()

Add a new proposal to the meta proposal

draw_n_samples(n: int, **kwargs)

Draw n samples from the current proposal

Includes computing the log-likelihood of the samples

Parameters:
nint

Number of samples to draw

kwargs

Keyword arguments passed to the draw method of the proposal class.

compute_leakage(samples: numpy.ndarray, weights: bool = True) float

Compute the leakage for a number of samples.

Parameters:
samplesnumpy.ndarray

Array of samples.

weightsbool

If True, the weight of each sample is accounted for in the calculation.

Returns:
float

The leakage as a fraction of the total number of samples (or effective sample size if weights is True).

add_and_update_points(n: int)

Add new points to the current set of live points.

Parameters:
nint

The number of points to add.

remove_samples() int

Remove samples from the current set of live points.

adjust_final_samples(n_batches=5)

Adjust the final samples

finalise() None

Finalise the sampling process.

add_level_post_sampling(samples: numpy.ndarray, n: int) None

Add a level to the nested sampler after initial sampling has completed.

compute_stopping_criterion() List[float]

Compute the stopping criterion.

The method used will depend on how the sampler was configured.

checkpoint(periodic: bool = False, force: bool = False)

Checkpoint the sampler.

log_state()

Log the state of the sampler

update_proposal_weights()

Update the proposal weights based on the current sample counts.

Uses the values from sample_counts.

See also: code:update_sample_counts.

update_sample_counts() None

Update the sample counts for each proposal based on the current samples.

Uses the samples from samples_unit

See also: update_proposal_weights.

add_new_proposal_weight(iteration: int, n_new: int) None

Set the weights for a new proposal.

Samples cannot have been drawn from the proposal already.

nested_sampling_loop()

Main nested sampling loop.

draw_posterior_samples(sampling_method: str = 'rejection_sampling', n: int | None = None, use_final_samples: bool = True) numpy.ndarray

Draw posterior samples from the current nested samples.

static kl_divergence(samples: numpy.ndarray) float

Compute the KL divergence between the meta-proposal and posterior.

Uses all samples drawn from the meta-proposal

draw_more_nested_samples(n: int) numpy.ndarray

Draw more nested samples from g

draw_final_samples(n_post: int | None = None, n_draw: int | None = None, max_its: int = 1000, max_batch_size: int = 20000, max_samples_ratio: float | None = 1.0, use_counts: bool = False, optimise_weights: bool = False, optimise_kwargs: dict | None = None, optimisation_method: Literal[evidence, kl] = 'kl')

Draw final unbiased samples until a desired ESS is reached.

The number of samples drawn is based on the efficiency of the existing nested samples up to a maximum size determined by max_batch_size or on the value of :code:`n_draw. The number is increased by 1% to account for samples being rejected.

Returns nested samples, NOT posterior samples.

Restarts the multiprocessing pool for evaluations the likelihood.

Parameters:
n_post

Target effective sample size for the posterior distribution. May not be reached if max_its is reached first. If not specified then the number of samples drawn will match the nested samples.

n_draw

Number of samples to draw from the meta proposal. Should only be specified if not specifying n_post.

max_its

Maximum number of iterations before stopping.

max_batch_size

Maximum number of samples to draw in a single batch.

max_samples_ratio

Maximum number of samples in terms of the number of samples drawn during sampling. For example if max_samples=1, up to half the initial number of samples will be drawn. If None, no limit is set.

optimise_weights

If True, the weights for each proposal are optimised before redrawing the samples.

optimise_kwargs

Keyword arguments passed to the optimiser function.

use_counts

Use the exact counts for each proposal rather than the weights. Not recommended. Ignored if optimise_weights is True.

Returns:
log_evidence

The log evidence for the new samples

samples

Structured array with the new nested samples.

train_final_flow()

Train a final flow using all of the nested samples

plot_state(filename: str | None = None) matplotlib.figure.Figure | None

Produce plots with the current state of the nested sampling run. Plots are saved to the output directory specified at initialisation.

Parameters:
filename

If specified the figure will be saved, otherwise the figure is returned.

plot_extra_state(filename: str | None = None) matplotlib.figure.Figure | None

Produce a state plot that contains extra tracked statistics.

Parameters:
filenameOptional[str]

Filename name for the plot when saved. If specified the figure will be saved, otherwise the figure is returned.

Returns:
Union[matplotlib.figure.Figure, None]

Returns the figure if a filename name is not given.

plot_trace(enable_colours: bool = True, filename: str | None = None) matplotlib.figure.Figure | None

Produce a trace-like plot of the nested samples.

Parameters:
enable_coloursbool

If True, the iteration will be plotted on the colour axis. If False, the points will be plotted with a single colour.

filenameOptional[str]

Filename for saving the figure. If not specified the figure will be returned instead.

Returns:
matplotlib.figure.Figure

Trace plot figure. Only returned when the filename is not specified.

plot_likelihood_levels(filename: str | None = None, cmap: str = 'viridis', max_bins: int = 50) matplotlib.figure.Figure | None

Plot the distribution of the likelihood at each level.

Parameters:
filename

Name of the file for saving the figure. If not specified, then the figure is returned.

cmap

Name of colourmap to use. Must be a valid colourmap in matplotlib.

max_bins

The maximum number of bins allowed.

produce_plots(override: bool = False) None

Produce all of the relevant plots.

Checks if plotting is enabled.

Parameters:
forcebool

Override the plotting setting and force the plots to be produced.

get_result_dictionary()

Get a dictionary contain the main results from the sampler.

classmethod resume_from_pickled_sampler(sampler, model, flow_config=None, weights_path=None, **kwargs)

Resume from a pickled sampler.

Parameters:
samplerAny

Pickled sampler.

modelnessai.model.Model

User-defined model

flow_configOptional[dict]

Dictionary for configuring the flow

weights_pathOptional[dict]

Path to the weights files that will override the value stored in the proposal.

kwargs

Keyword arguments passed to the parent class’s method.

Returns:
Instance of ImportanceNestedSampler