pybop._costs#

Module Contents#

Classes#

BaseCost

Base class for defining cost functions.

RootMeanSquaredError

Root mean square error cost function.

SumSquaredError

Sum of squared errors cost function.

class pybop._costs.BaseCost(problem)[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.

  • x0 (array-like) – The initial guess for the model parameters.

  • bounds (tuple) – The bounds for the model parameters.

  • n_parameters (int) – The number of parameters in the model.

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

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

This method must be implemented by subclasses.

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:

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

class pybop._costs.RootMeanSquaredError(problem)[source]#

Bases: BaseCost

Root mean square error cost function.

Computes the root mean square error between model predictions and the target data, providing a measure of the differences between predicted values and observed values.

Inherits all parameters and attributes from BaseCost.

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

Calculate the root mean square error 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 root mean square error.

Return type:

float

Raises:

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

class pybop._costs.SumSquaredError(problem)[source]#

Bases: BaseCost

Sum of squared errors cost function.

Computes the sum of the squares of the differences between model predictions and target data, which serves as a measure of the total error between the predicted and observed values.

Inherits all parameters and attributes from BaseCost.

Additional Attributes#

_defloat

The gradient of the cost function to use if an error occurs during evaluation. Defaults to 1.0.

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

Calculate the sum of squared errors 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 sum of squared errors.

Return type:

float

Raises:

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

evaluateS1(x)[source]#

Compute the cost and its gradient with respect to the 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.

set_fail_gradient(de)[source]#

Set the fail gradient to a specified value.

The fail gradient is used if an error occurs during the calculation of the gradient. This method allows updating the default gradient value.

Parameters:

de (float) – The new fail gradient value to be used.