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) – If False, tells the optimiser to switch the sign of the cost and gradient to maximise by default rather than minimise (default: True).

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

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]]]

abstractmethod 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) – A 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.

single_call(inputs: pybop.parameters.parameter.Inputs | numpy.ndarray, calculate_grad: bool) float | tuple[float, numpy.ndarray][source]#

Evaluate the cost and (optionally) the gradient for a single set of inputs.

stack_sensitivities(dy) numpy.ndarray[source]#

Stack the sensitivities for each signal and parameter into a single array.

Parameters:
  • dict[str – A dictionary of the sensitivities dy/dx(t) for each parameter x and signal y.

  • dict[str – A dictionary of the sensitivities dy/dx(t) for each parameter x and signal y.

  • np.ndarray[np.float64]]] – A dictionary of the sensitivities dy/dx(t) for each parameter x and signal y.

Returns:

The combined sensitivities dy/dx(t) for each parameter and signal, with dimensions of (len(parameters), len(signal), len(domain_data)).

Return type:

np.ndarray[np.float64]

verify_prediction(y: dict)[source]#

Verify that the prediction matches the target data.

Parameters:

y (dict) – A dictionary of predictions with keys designating the signals for fitting.

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]#
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]#
verbose = False[source]#
y = None[source]#