pysolvegn.build_absolute_regularization#

build_absolute_regularization(means, stds, *, weight=1.0, epsilon=0.001, sparse=False)[source]#

Build an absolute value (L1) regularization term, approximated by a smooth Charbonnier (pseudo-Huber) function.

The cost of the term is, for each parameter:

\[C_{\mathrm{reg},i}(\mathbf{p}) = w \left( \sqrt{z_i^2 + \varepsilon^2} - \varepsilon \right) \quad \text{with} \quad z_i = \frac{p_i - \mu_i}{\sigma_i}\]

which behaves as:

\[C_{\mathrm{reg},i} \approx w \left( |z_i| - \varepsilon \right) \quad \text{if } |z_i| \gg \varepsilon \qquad C_{\mathrm{reg},i} \approx \frac{w}{2 \varepsilon} z_i^2 \quad \text{if } |z_i| \ll \varepsilon\]

The L1 penalty \(w |z_i|\) favors sparse solutions (many parameters exactly at their mean), unlike the squared regularization which only shrinks them. The smooth quadratic zone of width \(\varepsilon\) around \(\mu_i\) keeps the Gauss-Newton system well defined.

It is implemented as a Term with the residuals \(z_i\), the "soft_l1" loss function with loss_scale=epsilon and the weight \(w / \varepsilon\):

\[\frac{1}{2} \frac{w}{\varepsilon} \rho_{\varepsilon}(z_i^2) = \frac{w}{\varepsilon} \varepsilon^2 \left( \sqrt{1 + z_i^2 / \varepsilon^2} - 1 \right) = w \left( \sqrt{z_i^2 + \varepsilon^2} - \varepsilon \right)\]

Note

Using the residual \(\sqrt{|z_i|}\) (such that \(\frac{1}{2} r_i^2 = \frac{1}{2}|z_i|\)) does not work with the Gauss-Newton method: its Jacobian is infinite at \(z_i = 0\), and the Gauss-Newton step is \(\Delta z_i = -2 z_i\), so the parameters oscillate between \(z_i\) and \(-z_i\) without converging.

Tip

A small epsilon gives a sharper L1 penalty but a stiffer problem: use damping="lm-diag" in pysolvegn.solve(), and a tolerance on the parameters larger than epsilon * stds.

The resulting Term can be directly passed to pysolvegn.solve().

See also

build_batch_absolute_regularization() for the batched version.

Parameters:
  • means (ArrayLike) – Center of the regularization for each parameter.

  • stds (ArrayLike) – Scale of the regularization for each parameter (the penalty is \(w |p_i - \mu_i| / \sigma_i\)). All values must be strictly positive.

  • weight (Real (default=1.0)) – Weight \(w\) of the L1 penalty.

  • epsilon (Real (default=1e-3)) – Width \(\varepsilon\) of the smooth quadratic zone, in the unit of \(z_i\) (number of stds). Must be strictly positive.

  • sparse (bool (default=False)) – If True, the diagonal Jacobian is a scipy.sparse.csr_matrix (n entries), otherwise a dense numpy.ndarray of shape (n, n). Use sparse=True for a large number of parameters.

Returns:

A Term representing the absolute value regularization.

Return type:

Term