Least Squares

Shipping
matrix_lstsq

Least-squares solution of A·x ≈ b with residual, rank, and singular values

Signature

Inputs

  • aSignal|ArrayrequiredThe design matrix $A$ (may be rectangular).
  • bSignal|ArrayrequiredThe observation / right-hand-side vector $b$.

Outputs

  • solutionVectorThe minimum-norm least-squares solution $x$.
  • residual_sum_of_squaresScalarThe residual sum of squares $\lVert Ax-b\rVert_2^2$ (NaN when not returned by the backend).
  • rankIntegerThe effective numerical rank of $A$.
  • singular_valuesVectorThe singular values of $A$.

Description

Least Squares solves the over- or under-determined system in the least-squares sense via the MKL LAPACK SVD-based driver (numpy's lstsq). It returns the full diagnostic set on four ports: the solution , the residual sum of squares , the effective rank of , and its singular values.

Unlike matrix_solve, the design matrix a may be rectangular. When the residual is not available from the backend (for example, an under-determined or rank-deficient system), the residual_sum_of_squares port reports NaN. The solution is dimensionless, consistent with the toolkit. The rank and singular values are especially useful for diagnosing rank deficiency and conditioning of a fit.

Mathematics

Examples

Fitting a line

Stack a design matrix (a column of ones and a time column) into a and the measured values into b. The solution port returns the intercept and slope, residual_sum_of_squares quantifies the fit error, and rank/singular_values reveal whether the columns are well-conditioned.

Applications

  • Polynomial and linear regression / curve fitting from over-determined data.
  • Parameter estimation and system identification with more equations than unknowns.
  • Diagnosing rank deficiency and conditioning of a design matrix via the rank and singular-value outputs.
  • Under-determined minimum-norm solutions where a unique inverse does not exist.

Neat

It returns the SVD by-products (rank and full singular-value spectrum) for free, so a fit and its conditioning diagnostics come from one node.

The residual port degrades gracefully to NaN when the backend cannot report it, rather than fabricating a misleading zero.

Known issues

For rank-deficient or under-determined problems the residual sum of squares may be reported as NaN.

The returned solution is the minimum-norm one when the system is under-determined — not necessarily the solution a caller expects if additional constraints are implied.

See also

least-squaresregressionsvdlinear-algebralapackmklstateless