pybop.costs.fitting_costs#
Classes#
Overwrites and extends BaseCost class for fitting-type cost functions. |
|
Mean absolute error (MAE) cost function. |
|
Mean square error (MSE) cost function. |
|
The Minkowski distance is a generalisation of several distance metrics, |
|
Observer cost function. |
|
Root mean square error (RMSE) cost function. |
|
Sum of squared error (SSE) cost function. |
|
The Sum of Power [1] is a generalised cost function based on the p-th power |
Module Contents#
- class pybop.costs.fitting_costs.FittingCost(problem)[source]#
Bases:
pybop.costs.base_cost.BaseCostOverwrites and extends BaseCost class for fitting-type 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.
- abstract _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.
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
- Returns:
If dy is not None, returns a tuple containing the cost (float) and the gradient (np.ndarray), otherwise returns only the computed cost (float).
- Return type:
tuple or float
- compute(y: dict, dy: numpy.ndarray | None = None) 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.
- Returns:
If dy is not None, returns a tuple containing the cost (float) and the gradient (np.ndarray), otherwise returns only the computed cost (float).
- Return type:
tuple or float
- class pybop.costs.fitting_costs.MeanAbsoluteError(problem)[source]#
Bases:
FittingCostMean 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.
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
- Returns:
If dy is not None, returns a tuple containing the cost (float) and the gradient (np.ndarray), otherwise returns only the computed cost (float).
- Return type:
tuple or float
- class pybop.costs.fitting_costs.MeanSquaredError(problem)[source]#
Bases:
FittingCostMean 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.
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
- Returns:
If dy is not None, returns a tuple containing the cost (float) and the gradient (np.ndarray), otherwise returns only the computed cost (float).
- Return type:
tuple or float
- class pybop.costs.fitting_costs.Minkowski(problem, p: float = 2.0)[source]#
Bases:
FittingCostThe 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.
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
- Returns:
If dy is not None, returns a tuple containing the cost (float) and the gradient (np.ndarray), otherwise returns only the computed cost (float).
- Return type:
tuple or float
- class pybop.costs.fitting_costs.ObserverCost(observer: pybop.observers.observer.Observer)[source]#
Bases:
pybop.costs.base_cost.BaseCostObserver 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 = None) 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.
- Returns:
The observer cost (negative of the log likelihood).
- Return type:
float
- class pybop.costs.fitting_costs.RootMeanSquaredError(problem)[source]#
Bases:
FittingCostRoot 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.
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
- Returns:
If dy is not None, returns a tuple containing the cost (float) and the gradient (np.ndarray), otherwise returns only the computed cost (float).
- Return type:
tuple or float
- class pybop.costs.fitting_costs.SumSquaredError(problem)[source]#
Bases:
FittingCostSum 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.
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
- Returns:
If dy is not None, returns a tuple containing the cost (float) and the gradient (np.ndarray), otherwise returns only the computed cost (float).
- Return type:
tuple or float
- class pybop.costs.fitting_costs.SumofPower(problem, p: float = 2.0)[source]#
Bases:
FittingCostThe 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.
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
- Returns:
If dy is not None, returns a tuple containing the cost (float) and the gradient (np.ndarray), otherwise returns only the computed cost (float).
- Return type:
tuple or float