cofi.utils.BaseLikelihood#
- class cofi.utils.BaseLikelihood[source]#
Base class for a likelihood function
This base class provides a consistent interface for likelihood functions used in Bayesian inverse problems. Concrete implementations should define how to evaluate the log-likelihood and its derivatives.
Basic interface
The basic properties / methods for a likelihood function in
cofi.utilsinclude the following:The shape of models that current likelihood function accepts.
The number of unknowns that current likelihood function accepts.
Evaluate the log-likelihood function at a given model.
BaseLikelihood.gradient(model)The gradient of log-likelihood with respect to model parameters.
BaseLikelihood.hessian(model)The Hessian of log-likelihood with respect to model parameters.
BaseLikelihood.__call__(model)A class instance itself can also be called as a function.
BaseLikelihood.get_ml_cov(model)Get maximum likelihood estimate of data covariance (if applicable).
Reference Details
- __call__(model: ndarray) float[source]#
A class instance itself can also be called as a function.
It works exactly the same as
log_likelihood.In other words, the following two usages are exactly the same:
>>> my_likelihood = GaussianLikelihood(data, forward_func) >>> log_p = my_likelihood(np.array([1,2,3])) # usage 1 >>> log_p = my_likelihood.log_likelihood(np.array([1,2,3])) # usage 2
- Parameters:
model (np.ndarray) – The model parameters to evaluate
- Returns:
The log-likelihood value
- Return type:
float
- get_ml_cov(model: ndarray) ndarray | None[source]#
Get maximum likelihood estimate of data covariance (if applicable).
This is an optional method that some likelihood implementations may provide to return the maximum likelihood estimate of the data covariance matrix for a given model.
- Parameters:
model (np.ndarray) – The model parameters
- Returns:
The estimated data covariance matrix, or None if not applicable
- Return type:
Optional[np.ndarray]
- abstract gradient(model: ndarray) ndarray[source]#
The gradient of log-likelihood with respect to model parameters.
The gradient has shape \((M,)\) where \(M\) is the number of model parameters.
- Parameters:
model (np.ndarray) – The model parameters at which to evaluate the gradient
- Returns:
The gradient vector of shape (M,)
- Return type:
np.ndarray
- abstract hessian(model: ndarray) ndarray[source]#
The Hessian of log-likelihood with respect to model parameters.
The Hessian has shape \((M,M)\) where \(M\) is the number of model parameters.
- Parameters:
model (np.ndarray) – The model parameters at which to evaluate the Hessian
- Returns:
The Hessian matrix of shape (M, M)
- Return type:
np.ndarray
- abstract log_likelihood(model: ndarray) float[source]#
Evaluate the log-likelihood function at a given model.
- Parameters:
model (np.ndarray) – The model parameters at which to evaluate the log-likelihood
- Returns:
The log-likelihood value log p(d|m)
- Return type:
float
- model_shape#
The shape of models that current likelihood function accepts.
- Returns:
The shape of the model parameter array
- Return type:
tuple
- model_size#
The number of unknowns that current likelihood function accepts.
- Returns:
The total number of model parameters
- Return type:
Number