nessai.model#
Object for defining the use-defined model.
Exceptions#
Exception raised when the model is not configured correctly. |
|
Exception raised when the model is one-dimensional. |
Classes#
Base class for the user-defined model being sampled. |
Module Contents#
- exception nessai.model.ModelError#
Bases:
ExceptionException raised when the model is not configured correctly.
Added in version 0.15.0.
- exception nessai.model.OneDimensionalModelError#
Bases:
ModelErrorException raised when the model is one-dimensional.
Changed in version 0.15.0: Changed to inherit from ModelError.
- class nessai.model.Model#
Bases:
abc.ABCBase class for the user-defined model being sampled.
The user must define the attributes
namesboundsand the methodslog_likelihoodandlog_prior.The user can also define the reparemeterisations here instead of in the keyword arguments passed to the sampler.
- reparameterisations = None#
- 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 = None#
- 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 = None#
- 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.
- set_rng(rng: numpy.random.Generator | None = None)#
Set the random number generator.
- Parameters:
- rngnp.random.Generator, optional
Random number generator. If not provided, a new generator is created.
- 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 discrete_parameters#
List of discrete parameters.
None if there are no discrete parameters.
- property has_discrete_parameters#
Indicates if the model contains discrete parameters.
- 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_vectorisedtoFalse.
- 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
- log_prior_unit_hypercube(x) numpy.ndarray#
Compute the log-prior in the unit hypercube.
By default this returns log(1) and checks if the values are in the unit hypercube.
- 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.ndarrayArray 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.ndarrayArray of log-prior values
- batch_evaluate_log_prior_unit_hypercube(x: numpy.ndarray) numpy.ndarray#
Evaluate the log-prior in the unit hypercube for a batch of samples.
Uses the pool if available.
- Parameters:
- x
numpy.ndarray Array of samples
- x
- Returns:
numpy.ndarrayArray 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).
- classmethod check_new_point_methods()#
Check if the new_point methods have been redefined.
Added in version 0.15.0.
- Raises:
- ModelError
If the new_point methods have been redefined but not both.
- 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.