:py:mod:`nessai.model` ====================== .. py:module:: nessai.model .. autoapi-nested-parse:: Object for defining the use-defined model. .. !! processed by numpydoc !! Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: nessai.model.Model Attributes ~~~~~~~~~~ .. autoapisummary:: nessai.model.logger .. py:data:: logger .. !! processed by numpydoc !! .. py:exception:: OneDimensionalModelError Bases: :py:obj:`Exception` Exception raised when the model is one-dimensional .. !! processed by numpydoc !! .. py:class:: Model Bases: :py:obj:`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. .. !! processed by numpydoc !! .. py:attribute:: reparameterisations dict Dictionary of reparameterisations that overrides the values specified. .. !! processed by numpydoc !! .. py:attribute:: likelihood_evaluations :annotation: = 0 int Number of likelihood evaluations. .. !! processed by numpydoc !! .. py:attribute:: likelihood_evaluation_time :py:obj:`datetime.timedelta()` Time spent evaluating the likelihood. .. !! processed by numpydoc !! .. py:attribute:: pool obj Multiprocessing pool for evaluating the log-likelihood. .. !! processed by numpydoc !! .. py:attribute:: allow_vectorised :annotation: = 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. .. !! processed by numpydoc !! .. py:method:: names(self) :property: List of the names of each parameter in the model. .. !! processed by numpydoc !! .. py:method:: bounds(self) :property: Dictionary with the lower and upper bounds for each parameter. .. !! processed by numpydoc !! .. py:method:: dims(self) :property: Number of dimensions in the model .. !! processed by numpydoc !! .. py:method:: lower_bounds(self) :property: Lower bounds on the priors .. !! processed by numpydoc !! .. py:method:: upper_bounds(self) :property: Upper bounds on the priors .. !! processed by numpydoc !! .. py:method:: vectorised_likelihood(self) :property: 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 :py:attr:`nessai.model.Model.allowed_vectorised` to ``False``. .. !! processed by numpydoc !! .. py:method:: configure_pool(self, pool=None, n_pool=None) Configure a multiprocessing pool for the likelihood computation. :Parameters: **pool** User provided pool. Must call :py:func:`nessai.utils.multiprocessing.initialise_pool_variables` before creating the pool. **n_pool** : int Number of threads to use to create an instance of :py:obj:`multiprocessing.Pool`. .. !! processed by numpydoc !! .. py:method:: close_pool(self, code=None) Close the the multiprocessing pool .. !! processed by numpydoc !! .. py:method:: new_point(self, N=1) Create a new LivePoint, drawn from within bounds. See `new_point_log_prob` if changing this method. :Parameters: **N** : int, 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) .. !! processed by numpydoc !! .. py:method:: 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: **x** : ndarray Points in a structured array :Returns: ndarray Log proposal probability for each point .. !! processed by numpydoc !! .. py:method:: in_bounds(self, x) Check if a set of live point are within the prior bounds. :Parameters: **x** : :obj:`numpy.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. .. !! processed by numpydoc !! .. py:method:: sample_parameter(self, name, n=1) :abstractmethod: 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 :py:class:`nessai.conditional.ConditionalFlowProposal`. :Parameters: **name** : str Name for the parameter to sample **n** : int, optional Number of samples to draw. .. !! processed by numpydoc !! .. py:method:: parameter_in_bounds(self, x, name) Check if an array of values for specific parameter are in the prior bounds. :Parameters: **x** : :obj:`numpy: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. .. !! processed by numpydoc !! .. py:method:: log_prior(self, x) :abstractmethod: Returns log-prior, must be defined by the user. .. !! processed by numpydoc !! .. py:method:: log_likelihood(self, x) :abstractmethod: Returns the log-likelihood, must be defined by the user. .. !! processed by numpydoc !! .. py:method:: evaluate_log_likelihood(self, x) Evaluate the log-likelihood and track the number of calls. :Returns: float Log-likelihood value .. !! processed by numpydoc !! .. py:method:: batch_evaluate_log_likelihood(self, x) Evaluate the likelihood for a batch of samples. Uses the pool if available. :Parameters: **x** : :obj:`numpy.ndarray` Array of samples :Returns: :obj:`numpy.ndarray` Array of log-likelihood values .. !! processed by numpydoc !! .. py:method:: verify_model(self) Verify that the model is correctly setup. This includes checking the names, bounds and log-likelihood. .. !! processed by numpydoc !!