pybop.costs.fitting_costs#

Classes#

ObserverCost

Observer cost function.

RootMeanSquaredError

Root mean square error cost function.

SumSquaredError

Sum of squared errors cost function.

Module Contents#

class pybop.costs.fitting_costs.ObserverCost(observer: pybop.observers.observer.Observer)[source]#

Bases: pybop.costs.base_cost.BaseCost

Observer cost function.

Computes the cost function for an observer model, which is log likelihood of the data points given the model parameters.

Inherits all parameters and attributes from BaseCost.

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

Calculate the observer cost 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 observer cost (negative of the log likelihood).

Return type:

float

abstract 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.

class pybop.costs.fitting_costs.RootMeanSquaredError(problem)[source]#

Bases: pybop.costs.base_cost.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.

_evaluate(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

_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.

class pybop.costs.fitting_costs.SumSquaredError(problem)[source]#

Bases: pybop.costs.base_cost.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.

_evaluate(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

_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.