nessai.proposal.importance

Proposals specifically for use with the importance based nested sampler.

Module Contents

Classes

ImportanceFlowProposal

Flow-based proposal for importance-based nested sampling.

class nessai.proposal.importance.ImportanceFlowProposal(model: nessai.model.Model, output: str, flow_config: dict = None, reparameterisation: str = 'logit', weighted_kl: bool = True, reset_flow: bool | int = True, clip: bool = False, plot_training: bool = False)

Bases: nessai.proposal.base.Proposal

Flow-based proposal for importance-based nested sampling.

Parameters:
modelnessai.model.Model

User-defined model.

outputstr

Output directory.

flow_configdict

Configuration for the flow.

reparameterisationstr

Reparameterisation to use. If None, only the unit-hypercube reparameterisation is used.

weighted_klbool

If true, use the weights (prior / meta-proposal) when training the next flow.

rest_flowUnion[bool, int]

When to reset the flow. If False, the flow is not reset. If True the flow is reset every time a new flow is added. If an integer, every nth flow is reset.

clipbool

If true the samples generated by flow will be clipped to [0, 1] before being mapped back from the unit-hypercube. This is only needed when the mapping cannot be defined outside of [0, 1]. In cases where it can, these points will be rejected when the prior bounds are checked.

plot_trainingbool

If True, produce plots during training. If False, no plots are produced.

property weights: dict

Dictionary containing the weights for each proposal

property weights_array: numpy.ndarray

Array of weights for each proposal

property n_proposals: int

Current number of proposals in the meta proposal

property flow_config: dict

Return the configuration for the flow

initialise()

Initialise the proposal

verify_rescaling(n: int = 1000, rtol: float = 1e-08, atol: float = 1e-08) None

Verify the rescaling is invertible.

Uses numpy.allclose, see numpy documentation for more details.

Parameters:
nint

Number of samples to test.

atolfloat

The absolute tolerance.

rtolfloat

The relative tolerance.

to_prime(x: numpy.ndarray) Tuple[numpy.ndarray, numpy.ndarray]

Convert samples from the unit hypercube to samples in x’-space

Parameters:
x_prime

Unstructured array of samples in the unit hypercube

Returns:
x

Unstructured array of samples in x’-space

log_j

Corresponding log-Jacobian determinant.

from_prime(x_prime: numpy.ndarray) Tuple[numpy.ndarray, numpy.ndarray]

Convert samples the x’-space to samples in the unit hypercube.

Parameters:
x_prime

Unstructured array of samples.

Returns:
x

Unstructured array of samples in the unit hypercube.

log_j

Corresponding log-Jacobian determinant.

rescale(x: numpy.ndarray) Tuple[numpy.ndarray, numpy.ndarray]

Convert to the space in which the flow is trained.

Returns an unstructured array.

inverse_rescale(x_prime: numpy.ndarray) numpy.ndarray

Convert from the space in which the flow is trained.

Returns a structured array.

update_proposal_weights(weights: dict) None

Method to update the proposal weights dictionary.

Raises:
RuntimeError

If the weights do not sum to 1 are the update.

train(samples: numpy.ndarray, plot: bool = False, output: str | None = None, weights: numpy.ndarray = None, **kwargs) None

Train the proposal with a set of samples.

Parameters:
samplesnumpy.ndarray

Array of samples for training.

plotbool

Flag to enable or disable plotting.

outputUnion[str, None]

Output directory to use instead of default output. If None the default that was set when the class what initialised is used.

kwargs

Key-word arguments passed to nessai.flowmodel.FlowModel.train().

compute_log_Q(x_prime: numpy.ndarray, log_j: numpy.ndarray | None = None) Tuple[numpy.ndarray, numpy.ndarray]

Compute the log meta proposal (log Q) for an array of points.

Parameters:
x_primenumpy.ndarray

Array of samples in the unit hypercube.

log_jOptional[numpy.ndarray]

Log-Jacobian determinant of the prime samples. Must be supplied if proposal includes flows.

Returns:
log_Qnumpy.ndarray

Value of the meta-proposal in the prime space.

log_qnumpy.ndarray

Array of values for flow in the flow in the proposal. Array will have shape (# samples, # flows)

draw(n: int, flow_number: int | None = None) Tuple[numpy.ndarray, numpy.ndarray]

Draw n new points.

Parameters:
nint

Number of points to draw.

flow_numberOptional[int]

Specifies which flow to use. If not specified the last flow will be used.

Returns:
np.ndarray

Array of new points.

np.ndarray

Log-proposal probabilities (log_q)

update_log_q(samples: numpy.ndarray, log_q: numpy.ndarray) numpy.ndarray

Update the array of proposal probabilities for a set of samples

compute_meta_proposal_from_log_q(log_q)

Compute the meta-proposal from an array of proposal log-probabilities

compute_meta_proposal_samples(samples: numpy.ndarray) numpy.ndarray

Compute the meta proposal Q for a set of samples.

Includes any rescaling that has been configured.

Returns:
log_meta_proposalnumpy.ndarray

Array of meta-proposal log probabilities (log Q)

log_qnumpy.ndarray

Array of log q for each flow.

get_proposal_log_prob(it: int) Callable

Get a pointer to the function for ith proposal.

compute_kl_between_proposals(x: numpy.ndarray, p_it: int | None = None, q_it: int | None = None) float

Compute the KL divergence between two proposals.

Samples should be drawn from p. If proposals aren’t specified the current and previous proposals are used.

draw_from_prior(n: int) Tuple[numpy.ndarray, numpy.ndarray]

Draw from the prior

draw_from_flows(n: int, weights=None, counts=None) Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

Draw n points from all flows (g).

Parameters:
nint

Number of points

weights

Array of (normalised) weights. Ignored if counts is specified.

counts

Number of samples to draw from each proposal.

resume(model, flow_config, weights_path=None)

Resume the proposal