pybop.costs.base_cost#

Classes#

BaseCost

Base class for defining cost functions.

Module Contents#

class pybop.costs.base_cost.BaseCost(problem: pybop.BaseProblem | None = None)[source]#

Base class for defining cost functions.

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.

  • n_outputs (int) – The number of outputs in the model.

  • has_separable_problem (bool) – If True, the problem is separable from the cost function and will be evaluated in advance of the call to self.compute() (default: False).

  • _de (float) – The gradient of the cost function to use if an error occurs during evaluation. Defaults to 1.0.

  • minimising (bool, optional, default=True) – If False, switches the sign of the cost and gradient to perform maximisation instead of minimisation.

class DeferredPrediction[source]#

Class used to indicate a prediction has yet to be, but is expected during a cost call.

__call__(inputs: pybop.parameters.parameter.Inputs | list | numpy.ndarray, calculate_grad: bool = False, apply_transform: bool = False, for_optimiser: bool = False) float | list | tuple[float, numpy.ndarray] | list[tuple[float, numpy.ndarray]][source]#

Compute cost and optional gradient for given input parameters.

Parameters:
  • inputs (Union[Inputs, list, np.ndarray]) – Input parameters for cost computation. Supports list-like evaluation of multiple input values, shaped [N,M] where N is the number of input positions to evaluate and M is the number of inputs for the underlying model (i.e. parameters).

  • calculate_grad (bool, default=False) – If True, both the cost and gradient will be computed. Otherwise, only the cost is computed.

  • apply_transform (bool, default=False) – If True, applies a transformation to the inputs before evaluating the model.

  • for_optimiser (bool, default=False) – If True, adjusts output sign based on minimisation/maximisation setting.

Returns:

  • Single input, no gradient: float

  • Multiple inputs, no gradient: list[float]

  • Single input with gradient: tuple[float, np.ndarray]

  • Multiple inputs with gradient: list[tuple[float, np.ndarray]]

Return type:

Union[float, list, tuple[float, np.ndarray], list[tuple[float, np.ndarray]]]

_apply_transformations(inputs)[source]#

Apply transformation if needed

_evaluate_non_separable_problem(calculate_grad: bool, sign: int) float | tuple[float, numpy.ndarray][source]#

Evaluation for non-separable problems.

_evaluate_separable_problem(input_value: pybop.parameters.parameter.Inputs | numpy.ndarray, calculate_grad: bool, sign: int) float | tuple[float, numpy.ndarray][source]#

Evaluation for separable problems.

_evaluate_single_input(input_value: pybop.parameters.parameter.Inputs | numpy.ndarray, calculate_grad: bool, apply_transform: bool, sign: int) float | tuple[float, numpy.ndarray][source]#

Evaluate cost (and optional gradient) for a single input.

abstract compute(y: dict, dy: numpy.ndarray | None)[source]#

Compute the cost and, if dy is not None, its gradient with respect to the parameters.

This method only computes the cost, without calling the problem.evaluate(). This method must be implemented by subclasses.

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.

Raises:

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

join_parameters(parameters)[source]#

Setter for joining parameters. This method sets the fail gradient if the join adds parameters.

sensitivity_analysis(n_samples: int = 256)[source]#

Computes the parameter sensitivities on the cost function using SOBOL analyse from the SALib module [1].

Parameters:

n_samples (int, optional) – Number of samples for SOBOL sensitivity analysis, performs best as order of 2, i.e. 128, 256, etc.

References

Returns:

Sensitivities

Return type:

dict

set_fail_gradient(de: float = 1.0)[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.

verify_prediction(y: dict)[source]#

Verify that the prediction matches the target data.

Parameters:

y (dict) – The model predictions.

Returns:

True if the prediction matches the target data, otherwise False.

Return type:

bool

_de = 1.0[source]#
_has_separable_problem = False[source]#
_parameters[source]#
_transformation = None[source]#
dy = None[source]#
property has_separable_problem[source]#
minimising = True[source]#
property n_parameters[source]#
property name[source]#
property parameters[source]#
problem = None[source]#
property pybamm_solution[source]#
property target[source]#
property transformation[source]#
verbose = False[source]#
y = None[source]#