pysolvegn.Term#

class Term(*, residual_func=None, jacobian_func=None, gradient_func=None, hessian_func=None, cost_func=None, weight=1.0, loss=None, loss_scale=1.0, finite_difference=None, finite_difference_epsilon=None)[source]#

A class representing a term in the least squares problem.

See also

For more details on the notations for the optimization problem, please refer to the mathematical section of the documentation.

For the following definition, the \(i\)-th term in the least squares problem is denoted as \(\mathbf{r}_i(\mathbf{p}) \in \mathbb{R}^{n_{r_i}}`\).

\[\min_{\mathbf{p}} \frac{1}{2} \sum_{i} w_i \sum_j \rho_i\left(\| \mathbf{r}_{i,j}(\mathbf{p}) \|^2\right)\]

where \(i\) indexes the different least squares terms (pysolvegn.Term) and \(j\) indexes the residuals within each term. Each term can be defined by two callable functions:

  1. The Residual and Jacobian functions

    The residual function \(\mathbf{r}_i(\mathbf{p})\) to minimize and the associated Jacobian function \(\mathbf{J}_i(\mathbf{p}) = \frac{\partial \mathbf{r}_i}{\partial \mathbf{p}}\) to compute the Jacobian matrix of the residuals with respect to the parameters.

    Then the solver will build the gradient \(\mathbf{g}(\mathbf{p})\) and the Hessian of the least squares problem \(\mathbf{H}(\mathbf{p})\) as:

    \[\mathbf{g} = w_i \tilde{\mathbf{J}}_i^T \tilde{\mathbf{r}}_i\]
    \[\mathbf{H} = w_i \tilde{\mathbf{J}}_i^T \tilde{\mathbf{J}}_i\]

    such that \(\mathbf{H} \Delta \mathbf{p} = -\mathbf{g}\).

  2. The gradient and Hessian functions

    The Hessian function \(\mathbf{H}_i(\mathbf{p})\) to compute the Hessian of the least squares problem and the gradient function \(\mathbf{g}_i(\mathbf{p})\) to compute the gradient of the least squares problem.

    Then the solver will use them directly without building them from the residuals and Jacobian, such that:

    \[\mathbf{g} = w_i \mathbf{g}_i(\mathbf{p})\]
    \[\mathbf{H} = w_i \mathbf{H}_i(\mathbf{p})\]

    Such that \(\mathbf{H} \Delta \mathbf{p} = -\mathbf{g}\).

This class is used to represent each term in the least squares problem, and can be used to define the residual and Jacobian functions for each term, as well as the gradient and Hessian functions for each term, which can be used to build the least squares problem.

Important

The term must be define by (residual + jacobian) or (gradient + Hessian) not both.

Parameters:
  • residual_func (Optional[Callable] (default=None)) – The function to compute the residuals \(\mathbf{r}_i: \mathbb{R}^{n_p} \rightarrow \mathbb{R}^{n_{r_i}}\) of the least squares problem. The function should take as inputs the parameters (1D array-like with shape (n_parameters,)) and return the residuals as a 1D array-like with shape (n_residuals,) representing each \(\mathbf{r}_{i,j}(\mathbf{p})\) where \(j=0,...,n_{r_i}-1\).

  • jacobian_func (Optional[Callable] (default=None)) – The function to compute the Jacobian matrix of the residuals with respect to the parameters. The function should take as inputs the parameters (1D array-like with shape (n_parameters,)) and return the Jacobian matrix as a 2D array-like with shape (n_residuals, n_parameters) representing each \(\mathbf{J}_{i,(j,l)}(\mathbf{p}) = \frac{\partial \mathbf{r}_{i,j}}{\partial \mathbf{p}_l}\) where \(j=0,...,n_{r_i}-1}\) and \(l=0,...,n_{p}-1}\). Can be numericallty computed using finite_difference argument.

  • gradient_func (Optional[Callable] (default=None)) – The function to compute the gradient \(\mathbf{g}_i(\mathbf{p}) = \mathbf{J}_i^T \mathbf{r}_i\) of the least squares problem. The function should take as inputs the parameters (1D array-like with shape (n_parameters,)) and return the gradient as a 1D array-like with shape (n_parameters,).

  • hessian_func (Optional[Callable] (default=None)) – The function to compute the Hessian \(\mathbf{H}_i(\mathbf{p}) = \mathbf{J}_i^T \mathbf{J}_i\) of the least squares problem. The function should take as inputs the parameters (1D array-like with shape (n_parameters,)) and return the Hessian matrix as a 2D array-like with shape (n_parameters, n_parameters).

  • cost_func (Optional[Callable] (default=None)) – The function to compute the cost of the term in the least squares problem. The function should take as inputs the parameters (1D array-like with shape (n_parameters,)) and return the cost as a positive floating value independent of the weight. If not provided, the default cost during optimization will be \(\frac{1}{2} \sum_j \rho_i(\| \mathbf{r}_{i,j}(\mathbf{p}_{out}) \|^2)\) for "rJ" terms and 0.0 for "gH" terms.

  • weight (Real (default=1.0)) – The weight of the term in the least squares problem. The weight will be applied to both the gradient and the Hessian matrix.

  • loss (Optional[Union[str, Callable]] (default=None)) – The loss function to use for the term in the least squares problem (only for "rJ" terms !). The loss function will affect how the gradient and Hessian are computed from the residuals and Jacobian. If loss is a string, one of the predefined loss functions is used. Available loss functions are "linear" (\(\rho(x) = x\)) [default for None], "cauchy" (\(\rho(x) = \log(1 + x)\)), "arctan" (\(\rho(x) = \arctan(x)\)), "soft_l1" (\(\rho(x) = 2(\sqrt{1 + x} - 1)\)), "huber" (\(\rho(x) = x\) if \(x \leq 1\), \(2\sqrt{x} - 1\) otherwise), and "tukey" (\(\rho(x) = (1 - (1 - x)^3) / 3\) if \(x \leq 1\), \(1/3\) otherwise). If loss is a callable, it is used as a custom loss function. The callable must take as input a 1D array-like containing the squared residuals and return three 1D array-like containing, respectively, the loss value, its first derivative, and its second derivative. When a custom loss function is provided, the loss property is set to "custom" and the corresponding callable is stored in the loss_func property.

  • loss_scale (Real (default=1.0)) – The soft threshold \(C\) of the loss function (only for "rJ" terms !), expressed in the unit of the residuals: the residuals with \(|r| \lesssim C\) are treated as inliers and the larger ones as outliers. The loss function (predefined or custom) is replaced by its scaled version \(\rho_C(x) = C^2 \rho\left(\frac{x}{C^2}\right)\). Same as f_scale in scipy.optimize.least_squares. It has no effect on the "linear" loss. Must be finite and strictly positive.

  • finite_difference (Optional[str] (default=None)) – The finite difference method used to numerically compute the Jacobian when jacobian_func is not provided. If None, the Jacobian is not computed numerically. In this case, jacobian_func must be provided. Available methods are "central", "forward", and "backward".

  • finite_difference_epsilon (Optional[Real] (default=None)) – The relative step of the finite differences (only with finite_difference). If None, the default step of the method is used: about 1.5e-8 for "forward" and "backward", about 6.1e-6 for "central" (see pysolvegn.build_numerical_jacobian()).

__init__(*, residual_func=None, jacobian_func=None, gradient_func=None, hessian_func=None, cost_func=None, weight=1.0, loss=None, loss_scale=1.0, finite_difference=None, finite_difference_epsilon=None)[source]#
Parameters:
  • residual_func (Callable | None)

  • jacobian_func (Callable | None)

  • gradient_func (Callable | None)

  • hessian_func (Callable | None)

  • cost_func (Callable | None)

  • weight (Real)

  • loss (str | Callable | None)

  • loss_scale (Real)

  • finite_difference (str | None)

  • finite_difference_epsilon (Real | None)

Methods

__init__(*[, residual_func, jacobian_func, ...])

from_gH([gradient_func, hessian_func, ...])

Create a pysolvegn.Term object from the gradient and Hessian functions.

from_rJ([residual_func, jacobian_func, ...])

Create a pysolvegn.Term object from the residual and Jacobian functions.

Attributes

H_func

J_func

c_func

cost_func

[Get] the cost function \(C_i: \mathbb{R}^{n_p} \rightarrow \mathbb{R}`\) of the term.

g_func

gradient_func

[Get] the gradient function \(\mathbf{g}_i: \mathbb{R}^{n_p} \rightarrow \mathbb{R}^{n_p}\) of the term.

hessian_func

[Get] the Hessian function \(\mathbf{H}_i: \mathbb{R}^{n_p} \rightarrow :math:\)mathbb{M}_{n_p, n_p}(mathbb{R})`` of the term.

jacobian_func

[Get] the Jacobian function \(\mathbf{J}_i: \mathbb{R}^{n_p} \rightarrow :math:\)mathbb{M}_{n_{r_i}, n_p}(mathbb{R})`` of the term.

loss

[Get] the name of the loss function associated with the term in the least squares problem.

loss_func

[Get] the loss function \(\rho: \mathbb{R} \rightarrow \mathbb{R}`\) of the term in the least squares problem.

loss_scale

[Get] the soft threshold \(C\) of the loss function, in the unit of the residuals.

r_func

residual_func

[Get] the residual function \(\mathbf{r}_i: \mathbb{R}^{n_p} \rightarrow \mathbb{R}^{n_{r_i}}\) of the term.

type

[Get] the type of the term, which can be either "rJ" for terms defined by residual and Jacobian functions, or "gH" for terms defined by second term and Hessian functions.

weight

[Get/Set] the weight of the term in the least squares problem.