pybop.costs.base_cost#

Classes#

BaseCost

Base class for defining cost functions.

Module Contents#

class pybop.costs.base_cost.BaseCost(problem=None)[source]#

Base class for defining cost functions.

This class is intended to be subclassed to create specific cost functions for evaluating model predictions against a set of data. The cost function quantifies the goodness-of-fit between the model predictions and the observed data, with a lower cost value indicating a better fit.

Parameters:
  • problem (object) – A problem instance containing the data and functions necessary for evaluating the cost function.

  • _target (array-like) – An array containing the target data to fit.

  • n_outputs (int) – The number of outputs in the model.

__call__(x, grad=None)[source]#

Call the evaluate function for a given set of parameters.

abstract _evaluate(inputs: pybop.parameters.parameter.Inputs, grad=None)[source]#

Calculate the cost function value for a given set of parameters.

This method must be implemented by subclasses.

Parameters:
  • inputs (Inputs) – The parameters for which to evaluate the cost.

  • grad (array-like, optional) – An array to store the gradient of the cost function with respect to the parameters.

Returns:

The calculated cost function value.

Return type:

float

Raises:

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

abstract _evaluateS1(inputs: pybop.parameters.parameter.Inputs)[source]#

Compute the cost and its gradient with respect to the parameters.

Parameters:

inputs (Inputs) – The parameters for which to compute the cost and gradient.

Returns:

A tuple containing the cost and the gradient. The cost is a float, and the gradient is an array-like of the same length as x.

Return type:

tuple

Raises:

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

evaluate(x, grad=None)[source]#

Call the evaluate function for a given set of parameters.

Parameters:
  • x (array-like) – The parameters for which to evaluate the cost.

  • grad (array-like, optional) – An array to store the gradient of the cost function with respect to the parameters.

Returns:

The calculated cost function value.

Return type:

float

Raises:

ValueError – If an error occurs during the calculation of the cost.

evaluateS1(x)[source]#

Call _evaluateS1 for a given set of parameters.

Parameters:

x (array-like) – The parameters for which to compute the cost and gradient.

Returns:

A tuple containing the cost and the gradient. The cost is a float, and the gradient is an array-like of the same length as x.

Return type:

tuple

Raises:

ValueError – If an error occurs during the calculation of the cost or gradient.

property n_parameters[source]#