nessai.model
Object for defining the use-defined model.
Module Contents
Classes
Base class for the user-defined model being sampled. |
- 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 methodslog_likelihood
andlog_prior
.The user can also define the reparemeterisations here instead of in the keyword arguments passed to the sampler.
- property names
List of the names of each parameter in the model.
- property bounds
Dictionary with the lower and upper bounds for each parameter.
- property dims
Number of dimensions in the model
- property lower_bounds
Lower bounds on the priors
- property upper_bounds
Upper bounds on the priors
- property vectorised_likelihood
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
toFalse
.
- 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_prior = True
- bool
Allow the model to use a vectorised prior. If True, nessai will try to check if the log-prior is vectorised and use call the method as a vectorised function. If False, nessai won’t check and, even if the log-prior is vectorised, it will only evaluate the log-prior one sample at a time.
- 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.
- likelihood_chunksize
- int
Chunksize to use with a vectorised likelihood. If specified the likelihood will be called with at most chunksize points at once.
- allow_multi_valued_likelihood = False
- bool
Allow for a multi-valued likelihood function that will return different likelihood values for the same point in parameter space. This is only recommended when the variation is significantly smaller that the variations in the likelihood across the prior.
- parallelise_prior = False
- bool
Parallelise calculating the log-prior using the multiprocessing pool.
- configure_pool(pool=None, n_pool=None)
Configure a multiprocessing pool for the likelihood computation.
Configuration will be skipped if the pool has already been configured.
- Parameters:
- pool
User provided pool. Must call
nessai.utils.multiprocessing.initialise_pool_variables()
before creating the pool or pass said function to the initialiser with the model.- n_poolint
Number of threads to use to create an instance of
multiprocessing.Pool
.
- close_pool(code=None)
Close the the multiprocessing pool.
Also resets the pool configuration.
- new_point(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(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(x)
Check if a set of live point are within the prior bounds.
- Parameters:
- x
numpy.ndarray
Structured array of live points. Must contain all of the parameters in the model.
- x
- Returns:
- Array of bool
Array with the same length as x where True indicates the point is within the prior bounds.
- in_unit_hypercube(x: numpy.ndarray) numpy.ndarray
Check if samples are within the unit hypercube
- 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(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(x, name)
Check if an array of values for specific parameter are in the prior bounds.
- Parameters:
- x
numpy:ndarray
Array of values. Not a structured array.
- x
- Returns:
- Array of bool
Array with the same length as x where True indicates the value is within the prior bounds.
- sample_unit_hypercube(n: int = 1) numpy.ndarray
“Sample from the unit hypercube.
- Parameters:
- nint
Number of samples
- Returns:
- numpy.ndarray
Structured array of samples
- abstract from_unit_hypercube(x)
Map from the unit hypercube to the priors.
Not implemented by default.
- abstract to_unit_hypercube(x)
Map from the prior space to the unit hypercube.
Not implemented by default.
- abstract log_prior(x)
Returns log-prior, must be defined by the user.
- abstract log_likelihood(x)
Returns the log-likelihood, must be defined by the user.
- evaluate_log_likelihood(x)
Evaluate the log-likelihood and track the number of calls.
- Returns:
- float
Log-likelihood value
- batch_evaluate_log_likelihood(x: numpy.ndarray, unit_hypercube: bool = False) numpy.ndarray
Evaluate the likelihood for a batch of samples.
Uses the pool if available.
- Parameters:
- x
numpy.ndarray
Array of samples
- unit_hypercubebool
Indicates if input samples are from the unit hypercube or not.
- x
- Returns:
numpy.ndarray
Array of log-likelihood values
- batch_evaluate_log_prior(x: numpy.ndarray, unit_hypercube: bool = False) numpy.ndarray
Evaluate the log-prior for a batch of samples.
Uses the pool if available.
- Parameters:
- x
numpy.ndarray
Array of samples
- unit_hypercubebool
Indicates if input samples are from the unit hypercube or not.
- x
- Returns:
numpy.ndarray
Array of log-prior values
- unstructured_view(x)
An unstructured view of point(s) x that only contains the parameters in the model.
This is quicker than converting to a unstructured array and does not create a copy of the array.
Calls
nessai.livepoint.unstructured_view()
with a pre-computed dtype.Warning
Will only work if all of the model parameters use the same dtype.
- Parameters:
- xstructured_array
Structured array of points
- Returns:
- numpy.ndarray
View of x as an unstructured array that contains only the parameters in the model. Shape is (x.size, self.dims).
- verify_model()
Verify that the model is correctly setup. This includes checking the names, bounds and log-likelihood.
- Returns:
- bool
True if the model was verified as valid.