pybop.costs.fitting_costs#

Classes#

Minkowski

The Minkowski distance is a generalisation of several distance metrics,

ObserverCost

Observer cost function.

RootMeanSquaredError

Root mean square error cost function.

SumSquaredError

Sum of squared errors cost function.

SumofPower

The Sum of Power [1] is a generalised cost function based on the p-th power

Module Contents#

class pybop.costs.fitting_costs.Minkowski(problem, p: float = 2.0)[source]#

Bases: pybop.costs.base_cost.BaseCost

The Minkowski distance is a generalisation of several distance metrics, including the Euclidean and Manhattan distances. It is defined as:

\[L_p(x, y) = ( \sum_i |x_i - y_i|^p )^(1/p)\]

where p > 0 is the order of the Minkowski distance. For p ≥ 1, the Minkowski distance is a metric. For 0 < p < 1, it is not a metric, as it does not satisfy the triangle inequality, although a metric can be obtained by removing the (1/p) exponent.

Special cases:

  • p = 1: Manhattan distance

  • p = 2: Euclidean distance

  • p → ∞: Chebyshev distance (not implemented as yet)

This class implements the Minkowski distance as a cost function for optimisation problems, allowing for flexible distance-based optimisation across various problem domains.

Additional Attributes#

pfloat, optional

The order of the Minkowski distance.

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

Computes the cost function for the given predictions.

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 Minkowski cost.

Return type:

float

p[source]#
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.

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

Computes the cost function for the given predictions.

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

Return type:

float

_has_separable_problem = False[source]#
_observer[source]#
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.

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

Computes the cost function for the given predictions.

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

Return type:

float

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.

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

Computes the cost function for the given predictions.

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 Sum of Squared Error.

Return type:

float

class pybop.costs.fitting_costs.SumofPower(problem, p: float = 2.0)[source]#

Bases: pybop.costs.base_cost.BaseCost

The Sum of Power [1] is a generalised cost function based on the p-th power of absolute differences between two vectors. It is defined as:

\[C_p(x, y) = \sum_i |x_i - y_i|^p\]

where p ≥ 0 is the power order.

This class implements the Sum of Power as a cost function for optimisation problems, allowing for flexible power-based optimisation across various problem domains.

Special cases:

  • p = 1: Sum of Absolute Differences

  • p = 2: Sum of Squared Differences

  • p → ∞: Maximum Absolute Difference

Note that this is not normalised, unlike distance metrics. To get a distance metric, you would need to take the p-th root of the result.

[1]: https://mathworld.wolfram.com/PowerSum.html

Additional Attributes#

pfloat, optional

The power order for Sum of Power.

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

Computes the cost function for the given predictions.

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 Sum of Power cost.

Return type:

float

p[source]#