API Reference#
This section contains a detailed description of the functions
included in pysolvegn. The reference describes how the methods work and which
parameters can be used. It assumes that you have an understanding of the key concepts.
The API is organized in three parts:
Single problem: solve one least squares problem with
solve().Batch of independent problems: solve
kindependent least squares problems at once withsolve_batch(). Each object of the single API has a batched counterpart, prefixed byBatch(classes) orbuild_batch_(builders).Shared tools: the robust loss functions, used by both modes.
For more detailed explanations of the mathematical background, please refer to the Mathematical Background section of the documentation.
See also
Mathematical Background for the mathematical foundation and theoretical concepts underlying the package.
Single problem#
Solver#
The main function of the package is the solve() function, which implements the
robust Gauss-Newton (or Levenberg-Marquardt) algorithm for solving a non-linear least
squares problem. This function takes as input Term objects that define the
residuals and Jacobians of the problem, an optional Parametrization, and
returns a SolveResult containing the optimized parameters and information
about the optimization.
|
Function to solve a least squares problem using the Gauss-Newton method with robust cost functions. |
|
Result of the optimization performed by |
|
A class representing a term in the least squares problem. |
|
A parametric transformation \(P\) that maps input parameters \(\vec{p_{in}}\) from an input parametric space \(\mathbb{R}^{n_{\mathrm{parameters}}}\) to output parameters \(\vec{p_{out}}\) in an output parametric space \(\mathbb{R}^{n_{\mathrm{p\_outputs}}}\). |
Implemented Parametrizations#
The package provides several implemented parametrizations that can be used to define how the input parameters are transformed into the output parameters.
|
Build a |
|
Build a parametrization that fixes some output parameters while optimizing only a selected subset of them. |
|
Build a parametrization that constrains parameters to a finite interval using a sigmoid transformation. |
|
Build a parametrization that constrains parameters to be strictly positive. |
Implemented Regularizations#
The package provides several implemented regularizations that can be used to define additional constraints or penalties on the parameters in the least squares problem.
|
Build a squared regularization term based on a Gaussian prior. |
|
Build a soft squared regularization term based on a Gaussian prior. |
|
Build an absolute value (L1) regularization term, approximated by a smooth Charbonnier (pseudo-Huber) function. |
Additional Utilities#
The package also includes additional utility functions, such as computing the Jacobian of the residuals by finite differences, selecting a regularization weight or studying the conditioning of the problem.
|
Build a Jacobian function for the Gauss-Newton optimization by computing the numerical derivatives of the residual function with respect to the parameters using finite differences. |
|
Analyze the regularization parameter using the L-curve method. |
|
Study the Gauss-Newton Hessian built by the solver. |
Batch of independent problems#
Solver#
The solve_batch() function solves k independent least squares problems
sharing the same terms, each with its own parameters (array with shape
(k, n_parameters)). The stopping criteria are applied to each problem independently:
a converged problem is removed from the processing while the others keep iterating.
Each problem gives the same result as if it was solved alone with solve().
The callables of a BatchTerm take the parameters of the m problems in
processing with shape (m, n_parameters) and their indices in the batch with shape
(m,), where m changes during the optimization. A BatchParametrization
applies the same transformation to all the problems (its callables do not take the
indices).
|
Function to solve a batch of |
|
Result of the optimization performed by |
|
A class representing a term in a batch of independent least squares problems. |
|
A parametric transformation \(P\) applied to each problem of a batch of independent least squares problems. |
Implemented Parametrizations#
Batched counterparts of the implemented parametrizations: the same transformation is applied to all the problems of the batch.
|
Build a |
|
Build a |
|
Build a |
Build a |
Implemented Regularizations#
Batched counterparts of the implemented regularizations. The prior values can be
shared by all the problems (shape (n,)) or defined for each problem
(shape (k, n)), and the weight can be a scalar or one value per problem
(shape (k,)).
|
Build a batched squared regularization term based on a Gaussian prior (batched counterpart of |
Build a batched soft squared regularization term (batched counterpart of |
|
|
Build a batched absolute value (L1) regularization term (batched counterpart of |
Additional Utilities#
|
Build a batched Jacobian function for the batched Gauss-Newton optimization by computing the numerical derivatives of a batched residual function with respect to the parameters using finite differences. |
Robust loss functions#
The package includes several robust cost functions that can be used to reduce the
influence of outliers in the optimization process. They are selected with the loss
argument of Term and BatchTerm (by name, or as a custom callable).
\(x = r^2\) denotes the squared residual.
Robust Function \(\rho\) |
Equation |
|---|---|
|
\(\rho(x) = x\) |
|
\(\rho(x) = 2 ((1 + x)^{1/2} - 1)\) |
|
\(\rho(x) = x\) if \(x \leq 1\), \(2 x^{1/2} - 1\) otherwise |
|
\(\rho(x) = \log(1 + x)\) |
|
\(\rho(x) = \arctan(x)\) |
|
\(\rho(x) = (1 - (1 - x)^3) / 3\) if \(x \leq 1\), \(1/3\) otherwise |
The transition between inliers and outliers is located at \(|r| = 1\). The
loss_scale argument \(C\) of Term and BatchTerm moves it to
\(|r| = C\) (in the unit of the residuals) by replacing any loss function, predefined
or custom, by \(\rho_C(x) = C^2 \rho(x / C^2)\) (see scale_rho_function()).
|
Compute the linear robust cost function \(\rho(x) = x\) and its derivatives. |
|
Compute the soft L1 robust cost function \(\rho(x) = 2(\sqrt{1 + x} - 1)\) and its derivatives. |
|
Compute the Huber robust cost function and its derivatives (same definition as |
|
Compute the Cauchy robust cost function \(\rho(x) = \log(1 + x)\) and its derivatives. |
|
Compute the arctan robust cost function \(\rho(x) = \arctan(x)\) and its derivatives. |
|
Compute the Tukey biweight (bisquare) robust cost function and its derivatives. |
|
Wrap any loss function \(\rho\) (predefined or custom) into its scaled version \(\rho_C\) with the soft threshold \(C\) ( |
|
Get a predefined rho function by name. |