pybop.costs._likelihoods#

Classes#

BaseLikelihood

Base class for likelihoods

BaseMetaLikelihood

Base class for likelihood classes which have a meta-likelihood

GaussianLogLikelihood

This class represents a Gaussian Log Likelihood, which assumes that the

GaussianLogLikelihoodKnownSigma

This class represents a Gaussian Log Likelihood with a known sigma,

LogPosterior

The Log Posterior for a given problem.

ScaledLogLikelihood

This class scaled a BaseLogLikelihood class by the number of observations.

Module Contents#

class pybop.costs._likelihoods.BaseLikelihood(problem: pybop.problems.base_problem.BaseProblem)[source]#

Bases: pybop.costs.base_cost.BaseCost

Base class for likelihoods

minimising = False[source]#
n_data[source]#
class pybop.costs._likelihoods.BaseMetaLikelihood(log_likelihood: BaseLikelihood)[source]#

Bases: BaseLikelihood

Base class for likelihood classes which have a meta-likelihood such as LogPosterior or ScaledLoglikelihood. This class points the required attributes towards the composed likelihood class.

_log_likelihood[source]#
property has_separable_problem[source]#
property likelihood: BaseLikelihood[source]#
property n_parameters[source]#
property parameters[source]#
property transformation[source]#
class pybop.costs._likelihoods.GaussianLogLikelihood(problem: pybop.problems.base_problem.BaseProblem, sigma0: float | list[float] | list[pybop.parameters.parameter.Parameter] = 0.01, dsigma_scale: float = 1.0)[source]#

Bases: BaseLikelihood

This class represents a Gaussian Log Likelihood, which assumes that the data follows a Gaussian distribution and computes the log-likelihood of observed data under this assumption.

This class estimates the standard deviation of the Gaussian distribution alongside the parameters of the model.

_logpi[source]#

Precomputed offset value for the log-likelihood function.

Type:

float

_dsigma_scale[source]#

Scale factor for derivative of standard deviation.

Type:

float

_add_sigma_parameters(sigma0)[source]#
_add_single_sigma(index, value)[source]#
_pad_sigma0(sigma0)[source]#
compute(y: dict, dy: numpy.ndarray = None, calculate_grad: bool = False) float | tuple[float, numpy.ndarray][source]#

Compute the Gaussian log-likelihood for the given parameters.

This method only computes the likelihood, without calling problem.evaluate().

Returns:

The log-likelihood value, or -inf if the standard deviations are non-positive.

Return type:

float

_dsigma_scale = 1.0[source]#
_logpi[source]#
property dsigma_scale[source]#

Scaling factor for the dsigma term in the gradient calculation.

sigma[source]#
transformation = None[source]#
class pybop.costs._likelihoods.GaussianLogLikelihoodKnownSigma(problem: pybop.problems.base_problem.BaseProblem, sigma0: list[float] | float)[source]#

Bases: BaseLikelihood

This class represents a Gaussian Log Likelihood with a known sigma, which assumes that the data follows a Gaussian distribution and computes the log-likelihood of observed data under this assumption.

Parameters:

sigma0 (scalar or array) – Initial standard deviation around x0. Either a scalar value (one standard deviation for all coordinates) or an array with one entry per dimension.

check_sigma0(sigma0: numpy.ndarray | float)[source]#

Check the validity of sigma0.

compute(y: dict, dy: numpy.ndarray = None, calculate_grad: bool = False) float | tuple[float, numpy.ndarray][source]#

Compute the Gaussian log-likelihood for the given parameters with known sigma.

This method only computes the likelihood, without calling the problem.evaluateS1.

_multip[source]#
_offset[source]#
sigma2[source]#
class pybop.costs._likelihoods.LogPosterior(log_likelihood: BaseLikelihood, log_prior: pybop.BasePrior | scipy.stats.rv_continuous | None = None, gradient_step: float = 0.001)[source]#

Bases: BaseMetaLikelihood

The Log Posterior for a given problem.

Computes the log posterior which is proportional to the sum of the log likelihood and the log prior.

Parameters:
  • log_likelihood (BaseLikelihood) – The likelihood class of type BaseLikelihood.

  • log_prior (Optional, Union[pybop.BasePrior, stats.rv_continuous]) – The prior class of type BasePrior or stats.rv_continuous. If not provided, the prior class will be taken from the parameter priors constructed in the pybop.Parameters class.

  • gradient_step (float, default: 1e-3) – The step size for the finite-difference gradient calculation if the log_prior is not of type BasePrior.

compute(y: dict, dy: numpy.ndarray = None, calculate_grad: bool = False) float | tuple[float, numpy.ndarray][source]#

Calculate the posterior cost for a given forward model prediction.

Parameters:
  • y (dict) – The data for which to evaluate the cost.

  • dy (np.ndarray, optional) – The correspond sensitivities in the data.

  • calculate_grad (bool, optional) – Whether to calculate the gradient of the cost function.

Returns:

The posterior cost, and optionally the gradient.

Return type:

Union[float, Tuple[float, np.ndarray]]

gradient_step = 0.001[source]#
property prior: pybop.parameters.priors.BasePrior[source]#
class pybop.costs._likelihoods.ScaledLogLikelihood(log_likelihood: BaseLikelihood)[source]#

Bases: BaseMetaLikelihood

This class scaled a BaseLogLikelihood class by the number of observations. The scaling factor is given below:

\[\mathcal{\hat{L(\theta)}} = \frac{1}{N} \mathcal{L(\theta)}\]

This class aims to provide numerical values with lower magnitude than the canonical likelihoods, which can improve optimiser convergence in certain cases.

compute(y: dict, dy: numpy.ndarray = None, calculate_grad: bool = False) float | tuple[float, numpy.ndarray][source]#

Compute the cost and if calculate_grad is True, its gradient with respect to the predictions.

This method only computes the cost, without calling the problem.evaluate(). This method must be implemented by subclasses.

Parameters:
  • y (dict) – The dictionary of predictions with keys designating the signals for fitting.

  • dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.

  • calculate_grad (bool, optional) – A bool condition designating whether to calculate the gradient.

Returns:

The calculated cost function value.

Return type:

float

Raises:

NotImplementedError – If the method has not been implemented by the subclass.