pybop.costs.error_measures#

Classes#

MeanAbsoluteError

Mean absolute error (MAE) cost function.

MeanSquaredError

Mean square error (MSE) cost function.

Minkowski

The Minkowski distance is a generalisation of several distance metrics,

RootMeanSquaredError

Root mean square error (RMSE) cost function.

SumOfPower

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

SumSquaredError

Sum of squared error (SSE) cost function.

Module Contents#

class pybop.costs.error_measures.MeanAbsoluteError(problem, weighting: str | numpy.ndarray = None)[source]#

Bases: pybop.FittingCost

Mean absolute error (MAE) cost function.

Computes the mean absolute error (MAE) between model predictions and target data. The MAE is a measure of the average magnitude of errors in a set of predictions, without considering their direction.

_error_measure(r: numpy.ndarray, dy: numpy.ndarray | None = None) float | tuple[float, numpy.ndarray][source]#

Computes the cost function for the given predictions.

Parameters:
  • r (np.ndarray) – The residual difference between the model prediction and the target. The dimensions of r are (len(signal), len(domain_data)).

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

Returns:

If dy is not None, returns a tuple containing the cost (float) and the gradient with dimension (len(parameters)), otherwise returns only the cost.

Return type:

np.float64 or tuple[np.float64, np.ndarray[np.float64]]

class pybop.costs.error_measures.MeanSquaredError(problem, weighting: str | numpy.ndarray = None)[source]#

Bases: pybop.FittingCost

Mean square error (MSE) cost function.

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

_error_measure(r: numpy.ndarray, dy: numpy.ndarray | None = None) float | tuple[float, numpy.ndarray][source]#

Computes the cost function for the given predictions.

Parameters:
  • r (np.ndarray) – The residual difference between the model prediction and the target. The dimensions of r are (len(signal), len(domain_data)).

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

Returns:

If dy is not None, returns a tuple containing the cost (float) and the gradient with dimension (len(parameters)), otherwise returns only the cost.

Return type:

np.float64 or tuple[np.float64, np.ndarray[np.float64]]

class pybop.costs.error_measures.Minkowski(problem, p: float = 2.0, weighting: str | numpy.ndarray = None)[source]#

Bases: pybop.FittingCost

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.

_error_measure(r: numpy.ndarray, dy: numpy.ndarray | None = None) float | tuple[float, numpy.ndarray][source]#

Computes the cost function for the given predictions.

Parameters:
  • r (np.ndarray) – The residual difference between the model prediction and the target. The dimensions of r are (len(signal), len(domain_data)).

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

Returns:

If dy is not None, returns a tuple containing the cost (float) and the gradient with dimension (len(parameters)), otherwise returns only the cost.

Return type:

np.float64 or tuple[np.float64, np.ndarray[np.float64]]

p[source]#
class pybop.costs.error_measures.RootMeanSquaredError(problem, weighting: str | numpy.ndarray = None)[source]#

Bases: pybop.FittingCost

Root mean square error (RMSE) 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.

_error_measure(r: numpy.ndarray, dy: numpy.ndarray | None = None) float | tuple[float, numpy.ndarray][source]#

Computes the cost function for the given predictions.

Parameters:
  • r (np.ndarray) – The residual difference between the model prediction and the target. The dimensions of r are (len(signal), len(domain_data)).

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

Returns:

If dy is not None, returns a tuple containing the cost (float) and the gradient with dimension (len(parameters)), otherwise returns only the cost.

Return type:

np.float64 or tuple[np.float64, np.ndarray[np.float64]]

class pybop.costs.error_measures.SumOfPower(problem, p: float = 2.0, weighting: str | numpy.ndarray = None)[source]#

Bases: pybop.FittingCost

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.

_error_measure(r: numpy.ndarray, dy: numpy.ndarray | None = None) float | tuple[float, numpy.ndarray][source]#

Computes the cost function for the given predictions.

Parameters:
  • r (np.ndarray) – The residual difference between the model prediction and the target. The dimensions of r are (len(signal), len(domain_data)).

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

Returns:

If dy is not None, returns a tuple containing the cost (float) and the gradient with dimension (len(parameters)), otherwise returns only the cost.

Return type:

np.float64 or tuple[np.float64, np.ndarray[np.float64]]

p[source]#
class pybop.costs.error_measures.SumSquaredError(problem, weighting: str | numpy.ndarray = None)[source]#

Bases: pybop.FittingCost

Sum of squared error (SSE) 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.

_error_measure(r: numpy.ndarray, dy: numpy.ndarray | None = None) float | tuple[float, numpy.ndarray][source]#

Computes the cost function for the given predictions.

Parameters:
  • r (np.ndarray) – The residual difference between the model prediction and the target. The dimensions of r are (len(signal), len(domain_data)).

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

Returns:

If dy is not None, returns a tuple containing the cost (float) and the gradient with dimension (len(parameters)), otherwise returns only the cost.

Return type:

np.float64 or tuple[np.float64, np.ndarray[np.float64]]