Robust losses

The loss and f_scale arguments of least_squares() and least_squares_system(), and the same cost as a differentiable function.

piazzi.robust_cost(r, loss='linear', f_scale=1.0)[source]

Robust least-squares cost 0.5 * sum(f_scale^2 * rho((r / f_scale)^2)).

This is the cost that piazzi.least_squares() minimises, written so that autograd can differentiate it. Use it with piazzi.minimize() or piazzi.lbfgs() to minimise the same cost without a Jacobian.

Parameters:
  • r (torch.Tensor) – residuals, of any shape.

  • loss (str or Callable, optional) – a name from piazzi.LOSSES, or a callable rho(z) written in PyTorch and applied elementwise. Default: "linear"

  • f_scale (float, optional) – soft margin between inlier and outlier residuals, in the units of r. Default: 1.0

Returns:

Tensor – the cost, a scalar tensor.

Example:

>>> r = torch.tensor([0.1, -0.2, 3.0])
>>> piazzi.robust_cost(r)
tensor(4.5250)
>>> piazzi.robust_cost(r, loss="huber", f_scale=0.5)
tensor(1.4000)
piazzi.LOSSES: dict[str, Callable]

The built-in robust losses by name: "linear", "huber", "soft_l1", "cauchy" and "arctan", as in scipy.optimize.least_squares(). Each maps z to (rho(z), rho'(z), rho''(z)) in closed form, so the normal equations need no autograd.