absolute_error

Contents

absolute_error#

class braintools.metric.absolute_error(predictions, targets=None, axis=None, reduction='mean')#

Compute element-wise absolute error between predictions and targets.

Calculates the absolute differences between predicted and target values, forming the basis for Mean Absolute Error (MAE) and L1 loss. This metric is more robust to outliers than squared error and provides intuitive error measurements in the same units as the original data.

The absolute error is defined as:

\[\text{AE}(y, \hat{y}) = |y - \hat{y}|\]

where \(y\) are the true values and \(\hat{y}\) are the predictions.

Parameters:
  • predictions (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Predicted values with arbitrary shape. Must be floating-point type.

  • targets (Array | ndarray | bool | number | bool | int | float | complex | Quantity | None) – Ground truth target values with shape broadcastable to predictions. If not provided, targets are assumed to be zeros, making this equivalent to computing the absolute magnitude of predictions.

  • axis (int | tuple[int, ...] | None) – Axis or axes along which to reduce the error. If None, no reduction is performed unless specified by reduction parameter.

  • reduction (str) –

    Reduction operation to apply:

    • 'none': Return element-wise errors without reduction

    • 'mean': Return mean of errors (MAE when no axis specified)

    • 'sum': Return sum of errors

Returns:

Absolute errors. Shape depends on axis and reduction parameters:

  • If reduction='none': same shape as predictions

  • If reduction is applied: reduced according to axis parameter

Return type:

Array | ndarray | bool | number | bool | int | float | complex | Quantity

Notes

Absolute error is equivalent to L1 loss and provides several advantages:

  • Robustness: Less sensitive to outliers than squared error

  • Interpretability: Error magnitude in original units

  • Gradient properties: Constant gradient magnitude (except at zero)

The relationship to other metrics:

  • Mean Absolute Error (MAE): absolute_error(pred, target, reduction='mean')

  • L1 norm of differences: absolute_error(pred, target, reduction='sum')

Examples

Basic element-wise absolute error:

>>> import jax.numpy as jnp
>>> import braintools
>>> predictions = jnp.array([1.0, 2.0, 3.0])
>>> targets = jnp.array([1.1, 1.9, 3.2])
>>> errors = braintools.metric.absolute_error(predictions, targets, reduction='none')
>>> print(errors)  # [0.1, 0.1, 0.2]

Mean Absolute Error (default):

>>> mae = braintools.metric.absolute_error(predictions, targets)
>>> print(f"MAE: {mae:.4f}")  # MAE: 0.1333

Compare robustness to outliers with squared error:

>>> # Data with outlier
>>> pred_outlier = jnp.array([1.0, 2.0, 10.0])  # 10.0 is outlier
>>> target_clean = jnp.array([1.1, 1.9, 3.0])
>>> mae = braintools.metric.absolute_error(pred_outlier, target_clean)
>>> mse = braintools.metric.squared_error(pred_outlier, target_clean, reduction='mean')
>>> print(f"MAE: {mae:.3f}, MSE: {mse:.3f}")  # MAE less affected by outlier

Batch processing with axis reduction:

>>> batch_pred = jnp.array([[1.0, 2.0], [3.0, 4.0]])
>>> batch_targets = jnp.array([[1.1, 1.9], [2.8, 4.2]])
>>> # MAE per sample
>>> per_sample_mae = braintools.metric.absolute_error(batch_pred, batch_targets, axis=1)
>>> print(per_sample_mae)

See also

braintools.metric.squared_error

L2 loss alternative

braintools.metric.l1_loss

Alternative L1 implementation

braintools.metric.huber_loss

Combines L1 and L2 properties

References