nessai.model

Object for defining the use-defined model.

Module Contents

Classes

Model

Base class for the user-defined model being sampled.

Attributes

logger

nessai.model.logger
exception nessai.model.OneDimensionalModelError

Bases: Exception

Exception raised when the model is one-dimensional

class nessai.model.Model

Bases: abc.ABC

Base class for the user-defined model being sampled.

The user must define the attributes names bounds and the methods log_likelihood and log_prior.

The user can also define the reparemeterisations here instead of in the keyword arguments passed to the sampler.

reparameterisations
dict

Dictionary of reparameterisations that overrides the values specified.

likelihood_evaluations = 0
int

Number of likelihood evaluations.

likelihood_evaluation_time
datetime.timedelta()

Time spent evaluating the likelihood.

pool
obj

Multiprocessing pool for evaluating the log-likelihood.

allow_vectorised = True
bool

Allow the model to use a vectorised likelihood. If True, nessai will try to check if the model is vectorised and use call the likelihood as a vectorised function. If False, nessai won’t check and, even if the likelihood is vectorised, it will only evaluate the likelihood one sample at a time.

property names(self)

List of the names of each parameter in the model.

property bounds(self)

Dictionary with the lower and upper bounds for each parameter.

property dims(self)

Number of dimensions in the model

property lower_bounds(self)

Lower bounds on the priors

property upper_bounds(self)

Upper bounds on the priors

property vectorised_likelihood(self)

Boolean to indicate if the likelihood is vectorised or not.

Checks that the values returned by computing the likelihood for individual samples matches those return by evaluating the likelihood in a batch. If a TypeError or ValueError are raised the likelihood is assumed to be vectorised.

This check can be prevented by setting nessai.model.Model.allowed_vectorised to False.

configure_pool(self, pool=None, n_pool=None)

Configure a multiprocessing pool for the likelihood computation.

Parameters
pool

User provided pool. Must call nessai.utils.multiprocessing.initialise_pool_variables() before creating the pool.

n_poolint

Number of threads to use to create an instance of multiprocessing.Pool.

close_pool(self, code=None)

Close the the multiprocessing pool

new_point(self, N=1)

Create a new LivePoint, drawn from within bounds.

See new_point_log_prob if changing this method.

Parameters
Nint, optional

Number of points to draw. By default draws one point. If N > 1 points are drawn using a faster method.

Returns
ndarray

Numpy structured array with fields for each parameter and log-prior (logP) and log-likelihood (logL)

new_point_log_prob(self, x)

Computes the proposal probability for a new point.

This does not assume the that points will be drawn according to the prior. If new_point is redefined this method must be updated to match.

Parameters
xndarray

Points in a structured array

Returns
ndarray

Log proposal probability for each point

in_bounds(self, x)

Check if a set of live point are within the prior bounds.

Parameters
xnumpy.ndarray

Structured array of live points. Must contain all of the parameters in the model.

Returns
Array of bool

Array with the same length as x where True indicates the point is within the prior bounds.

abstract sample_parameter(self, name, n=1)

Draw samples for a specific parameter from the prior.

Should be implemented by the user and return a numpy array of length n. The array should NOT be a structured array. This method is not required for standard sampling with nessai. It is intended for use with nessai.conditional.ConditionalFlowProposal.

Parameters
namestr

Name for the parameter to sample

nint, optional

Number of samples to draw.

parameter_in_bounds(self, x, name)

Check if an array of values for specific parameter are in the prior bounds.

Parameters
xnumpy:ndarray

Array of values. Not a structured array.

Returns
Array of bool

Array with the same length as x where True indicates the value is within the prior bounds.

abstract log_prior(self, x)

Returns log-prior, must be defined by the user.

abstract log_likelihood(self, x)

Returns the log-likelihood, must be defined by the user.

evaluate_log_likelihood(self, x)

Evaluate the log-likelihood and track the number of calls.

Returns
float

Log-likelihood value

batch_evaluate_log_likelihood(self, x)

Evaluate the likelihood for a batch of samples.

Uses the pool if available.

Parameters
xnumpy.ndarray

Array of samples

Returns
numpy.ndarray

Array of log-likelihood values

verify_model(self)

Verify that the model is correctly setup. This includes checking the names, bounds and log-likelihood.