nessai.proposal.flowproposal.base#

Base proposal class that contains common methods.

Classes#

BaseFlowProposal

Object that handles training and proposal points

Module Contents#

class nessai.proposal.flowproposal.base.BaseFlowProposal(model, rng: numpy.random.Generator | None = None, flow_config=None, training_config=None, output=None, poolsize=None, plot='min', check_acceptance=False, max_poolsize_scale=10, update_poolsize=True, accumulate_weights=False, save_training_data=False, reparameterisations=None, fallback_reparameterisation='zscore', use_default_reparameterisations=None, reverse_reparameterisations=False, map_to_unit_hypercube=False)#

Bases: nessai.proposal.rejection.RejectionProposal

Object that handles training and proposal points

Parameters:
modelnessai.model.Model

User defined model.

flow_configdict, optional

Configuration for training the normalising flow. If None, uses default settings. Defaults to None.

outputstr, optional

Path to output directory.

plot{True, False, ‘all’, ‘min’}, optional

Controls the plotting level: True - all plots; False - no plots; 'all' - all plots and 'min' - 1d plots and loss.

poolsizeint, optional

Size of the proposal pool. Defaults to 10000.

update_poolsizebool, optional

If True the poolsize is updated using the current acceptance of the nested sampler.

max_poolsize_scaleint, optional

Maximum scale for increasing the poolsize. E.g. if this value is 10 and the poolsize is 1000 the maximum number of points in the pool is 10,000.

check_acceptancebool, optional

If True the acceptance is computed after populating the pool. This includes computing the likelihood for every point. Default False.

reparameterisationsUnion[dict, str], optional

Dictionary for configure more flexible reparameterisations. This ignores any of the other settings related to rescaling. For more details see the documentation.

fallback_reparameterisationNone or str

Name of the reparameterisation to be used for parameters that have not been specified in the reparameterisations dictionary. If None, the NullReparameterisation is used. Reparameterisation should support multiple parameters.

use_default_reparameterisationsbool, optional

If True then reparameterisations will be used even if reparameterisations is None. The exact reparameterisations used will depend on add_default_reparameterisations() which may be overloaded by child classes. If not specified then the value of the attribute use_default_reparameterisations is used.

reverse_reparameterisationsbool

Passed to reverse_order in CombinedReparameterisation. Reverses the order of the reparameterisations.

map_to_unit_hypercubebool

If true, samples will be mapped to the unit hypercube before any reparameterisations are applied.

use_default_reparameterisations = False#

Indicates whether reparameterisations will be used be default in this class. Child classes can change this value a force the default behaviour to change without changing the keyword arguments.

property flow_config#

Return the configuration for the flow

property training_config#

Return the configuration for the flow

property poolsize#

Return the poolsize based of the base value and the current value of the scaling

property dims#

Return the number of dimensions

property rescaled_dims#

Return the number of rescaled dimensions

property x_dtype#

Return the dtype for the x space

property x_prime_dtype#

Return the dtype for the x prime space

property population_dtype#

dtype used for populating the proposal, depends on if the prior is defined in the x space or x-prime space

property prior_bounds#

The priors bounds used when computing the priors.

If map_to_unit_hypercube is true, these will be [0, 1]

configure_poolsize(poolsize, update_poolsize, max_poolsize_scale)#

Configure settings related to the pool size

configure_plotting(plot)#

Configure plotting.

Plotting is split into training and pool. Training refers to plots produced during training and pool refers to plots produces during the population stage.

Parameters:
plot{True, False, ‘all’, ‘train’, ‘pool’, ‘min’, ‘minimal’}

Level of plotting. all, train and pool enable corner style plots. All other values that evaluate to True enable 1d histogram plots. False disables all plotting.

update_flow_config()#

Update the flow configuration dictionary.

initialise(resumed: bool = False) None#

Initialise the proposal class.

This includes:
  • Setting up the rescaling

  • Verifying the rescaling is invertible

  • Initialising the FlowModel

Parameters:
resumedbool

Indicates if the proposal is being initialised after being resumed or not. When true, the reparameterisations will not be reinitialised.

update_poolsize_scale(acceptance)#

Update poolsize given the current acceptance.

Parameters:
acceptancefloat

Current acceptance.

add_default_reparameterisations()#

Add any reparameterisations which are assumed by default

get_reparameterisation(name)#

Get the reparameterisation from the name

configure_reparameterisations(reparameterisations)#

Configure the reparameterisations.

Parameters:
reparameterisationsUnion[dict, str, None]

Dictionary of reparameterisations. If None, then the defaults from :py:func`get_default_reparameterisations` are used.

set_rescaling()#

Set function and parameter names for rescaling

verify_rescaling()#

Verify the rescaling functions are invertible

rescale(x, **kwargs)#

Rescale from the physical space to the primed physical space

Parameters:
x: array_like

Array of live points to rescale

compute_radius: bool (False)

Used to indicate when rescaling is being used for computing the radius for population. This is important for rescaling that uses inversions.

Returns:
array

Array of rescaled values

array

Array of log det|J|

inverse_rescale(x_prime, return_unit_hypercube=False, **kwargs)#

Rescale from the primed physical space to the original physical space.

Parameters:
x_primearray_like

Array of live points to rescale.

Returns:
array

Array of rescaled values in the data space.

array

Array of log-Jacobian determinants.

check_state(x)#

Update the state of the proposal given some training data.

Includes updating the reparameterisations.

Parameters:
x: array_like

Array of training live points which can be used to set parameters

train(x, plot=True)#

Train the normalising flow given some of the live points.

Parameters:
xstructured_array

Array of live points

plot{True, False, ‘all’}

Enable or disable plots for during training. By default the plots are only one-dimensional histograms, ‘all’ includes corner plots with samples, these are often a few MB in size so proceed with caution!

reset_model_weights(**kwargs)#

Reset the flow weights.

Parameters:
kwargs

Keyword arguments passed to nessai.flowmodel.FlowModel.reset_model().

check_prior_bounds(x, *args)#

Return only values that are within the prior bounds

Parameters:
x: array_like

Array of live points which will compared against prior bounds

*args:

Additional arrays which correspond to the array of live points. Only those corresponding to points within the prior bounds are returned

Returns:
out: tuple of arrays

Array containing the subset of the original arrays which fall within the prior bounds

forward_pass(x, rescale=True, **kwargs)#

Pass a vector of points through the model

Parameters:
xarray_like

Live points to map to the latent space

rescalebool, optional (True)

Apply rescaling function

compute_radiusbool, optional (True)

Flag parsed to rescaling for rescaling specific to radius computation

Returns:
xarray_like

Samples in the latent space

log_probarray_like

Log probabilities corresponding to each sample (including the jacobian)

backward_pass(z, rescale=True, **kwargs)#

A backwards pass from the model (latent -> real)

Parameters:
zarray_like

Structured array of points in the latent space

rescalebool, optional (True)

Apply inverse rescaling function

Returns:
xarray_like

Samples in the data space

log_jarray_like

Log Jacobian determinant

log_prior(x)#

Compute the prior probability using the user-defined model

Parameters:
xstructured_array

Array of samples

Returns:
array_like

Array of log prior probabilities

unit_hypercube_log_prior(x)#

Compute the prior in the unit hypercube space.

Parameters:
xarray

Samples in the unit hypercube.

compute_weights(x, log_q, return_log_prior=False)#

Compute weights for the samples.

Does NOT normalise the weights

Parameters:
xstructured_arrays

Array of points

log_qarray_like

Array of log proposal probabilities.

return_log_prior: bool

If true, the log-prior probability is also returned.

Returns:
array_like

Log-weights for rejection sampling.

convert_to_samples(x, plot=True)#

Convert the array to samples ready to be used.

This removes are auxiliary parameters, (e.g. auxiliary radial parameters) and ensures the prior is computed. These samples can be directly used in the nested sampler.

Parameters:
xarray_like

Array of samples

plotbool, optional

If true a 1d histogram for each parameter of the pool is plotted. This includes a comparison the live points used to train the current realisation of the flow.

Returns:
array

Structured array of samples.

abstract populate(worst_point, n_samples=10000)#

Populate the pool by drawing from the proposal distribution and using rejection sampling.

Will also evaluate the likelihoods if the proposal contains a multiprocessing pool.

Parameters:
Nint, optional

Number of samples to draw. Not all samples will be accepted to the number of samples saved will be less than N. If not specified poolsize will be used.

compute_acceptance(logL)#

Compute how many of the current pool have log-likelihoods greater than the specified log-likelihood using the current value in the logL field.

Parameters:
floatlogL

Log-likelihood to use as the lower bound

Returns:
float

Acceptance defined on [0, 1]

draw(worst_point)#

Draw a replacement point. The new point is independent of the worst point. The worst point is only used during population.

Parameters:
worst_pointstructured_array

The current worst point used to compute the radius of the contour in the latent space.

Returns:
structured_array

New live point

plot_pool(x)#

Plot the pool of points.

Parameters:
xarray_like

Corresponding samples to plot in the physical space.

resume(model, flow_config, weights_file=None)#

Resume the proposal.

The model and config are not stored so these must be provided.

Parameters:
modelnessai.model.Model

User-defined model used.

flow_configdict

Configuration dictionary for the flow.

weights_filesstr, optional

Weights file to try and load. If not provided the proposal tries to load the last weights file.

reset()#

Reset the proposal