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 k independent least squares problems at once with solve_batch(). Each object of the single API has a batched counterpart, prefixed by Batch (classes) or build_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

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.

solve(terms, p0[, parametrization, ...])

Function to solve a least squares problem using the Gauss-Newton method with robust cost functions.

SolveResult(parameters, history, success, ...)

Result of the optimization performed by pysolvegn.solve().

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

A class representing a term in the least squares problem.

Parametrization([parametric_func, ...])

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_affine_parametrization(modes[, offset])

Build a Parametrization based on an affine transformation.

build_fixed_parametrization(n_p_outputs, ...)

Build a parametrization that fixes some output parameters while optimizing only a selected subset of them.

build_sigmoid_parametrization(lower, upper)

Build a parametrization that constrains parameters to a finite interval using a sigmoid transformation.

build_positive_parametrization(n_parameters)

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_squared_regularization(means, stds, *)

Build a squared regularization term based on a Gaussian prior.

build_soft_squared_regularization(means, ...)

Build a soft squared regularization term based on a Gaussian prior.

build_absolute_regularization(means, stds, *)

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_numerical_jacobian(residual_func[, ...])

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.

perform_Lcurve_analysis(data_term, reg_term, ...)

Analyze the regularization parameter using the L-curve method.

study_optimization(terms, p0[, ...])

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).

solve_batch(terms, p0[, parametrization, ...])

Function to solve a batch of k independent least squares problems using the Gauss-Newton method with robust cost functions.

BatchSolveResult(parameters, history, ...)

Result of the optimization performed by pysolvegn.solve_batch().

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

A class representing a term in a batch of independent least squares problems.

BatchParametrization([parametric_func, ...])

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_batch_affine_parametrization(modes[, ...])

Build a BatchParametrization based on an affine transformation (batched counterpart of build_affine_parametrization()).

build_batch_fixed_parametrization(...[, ...])

Build a BatchParametrization that fixes some output parameters while optimizing only a selected subset of them (batched counterpart of build_fixed_parametrization()).

build_batch_sigmoid_parametrization(lower, upper)

Build a BatchParametrization that constrains parameters to a finite interval using a sigmoid transformation (batched counterpart of build_sigmoid_parametrization()).

build_batch_positive_parametrization(...)

Build a BatchParametrization that constrains parameters to be strictly positive (batched counterpart of build_positive_parametrization()):

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_batch_squared_regularization(means, ...)

Build a batched squared regularization term based on a Gaussian prior (batched counterpart of build_squared_regularization()).

build_batch_soft_squared_regularization(...)

Build a batched soft squared regularization term (batched counterpart of build_soft_squared_regularization()).

build_batch_absolute_regularization(means, ...)

Build a batched absolute value (L1) regularization term (batched counterpart of build_absolute_regularization()).

Additional Utilities#

build_batch_numerical_jacobian(residual_func)

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

linear

\(\rho(x) = x\)

soft_l1

\(\rho(x) = 2 ((1 + x)^{1/2} - 1)\)

huber

\(\rho(x) = x\) if \(x \leq 1\), \(2 x^{1/2} - 1\) otherwise

cauchy

\(\rho(x) = \log(1 + x)\)

arctan

\(\rho(x) = \arctan(x)\)

tukey

\(\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()).

linear_rho(x)

Compute the linear robust cost function \(\rho(x) = x\) and its derivatives.

soft_l1_rho(x)

Compute the soft L1 robust cost function \(\rho(x) = 2(\sqrt{1 + x} - 1)\) and its derivatives.

huber_rho(x)

Compute the Huber robust cost function and its derivatives (same definition as scipy.optimize.least_squares).

cauchy_rho(x)

Compute the Cauchy robust cost function \(\rho(x) = \log(1 + x)\) and its derivatives.

arctan_rho(x)

Compute the arctan robust cost function \(\rho(x) = \arctan(x)\) and its derivatives.

tukey_rho(x)

Compute the Tukey biweight (bisquare) robust cost function and its derivatives.

scale_rho_function(rho_func, loss_scale)

Wrap any loss function \(\rho\) (predefined or custom) into its scaled version \(\rho_C\) with the soft threshold \(C\) (loss_scale):

get_rho_function_by_name(name)

Get a predefined rho function by name.