Straight Line Fitting Algorithm
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
Straight line fitting estimates the two parameters of a linear model from a set of measured
points, in closed form, along with their variances, covariance, and a goodness-of-fit probability. It is the
algorithm behind StraightLineFitter in the com.irurueta.numerical.fitting package, and is described in
Press et al., 2007, Section 15.2 (page 781).
Problem statement
Given data points with known measurement errors on (the are assumed known exactly), the model is:
and, as with every fitter in this package, and are chosen to minimize the chi-square merit function:
This is the simplest possible case of general linear least-squares fitting — only two basis functions, and — solved with dedicated closed-form formulas rather than the general normal-equations matrix machinery, making it considerably cheaper for this one common case.
Closed-form solution
Setting and gives two linear equations in and . Defining the weighted sums:
the minimization condition becomes:
whose solution, with , is:
Uncertainties, covariance, and correlation
Propagating the measurement errors through and (using ) gives their variances and covariance:
and the correlation coefficient between the errors in and :
Goodness of fit
The probability that a fit this poor (or worse) could occur by chance, under the assumption of normally-distributed errors with degrees of freedom, is:
where is the chi-square cumulative distribution function with degrees of freedom (computed via the incomplete gamma function). As a rule of thumb: indicates a believable fit, may still be acceptable if the errors are non-normal or somewhat underestimated, and calls the model — or the assumed measurement errors — into question.
A numerically stable reformulation
The closed-form expressions above are susceptible to roundoff error. StraightLineFitter instead uses the
equivalent, numerically stable formulation from Press et al., 2007, built
around a per-point normalized abscissa :
This is algebraically identical to the naive formulas above but avoids the cancellation that makes unreliable in floating-point arithmetic.
Algorithm
Unlike the iterative Levenberg-Marquardt fitter, there is nothing to configure and no risk of non-convergence: the fit is exact given the formulas above.
When measurement errors are unknown
If the are not known, StraightLineFitter sets for every point, solves for
and using the unweighted sums, and then estimates a common per-point error after the fact
from the residuals:
rescaling and by this factor. Since the data itself was used to estimate the
error bars, this procedure assumes the line is a good fit and provides no independent goodness-of-fit check —
getQ() stays at its default value of in this case.
Library implementation
StraightLineFitter extends Fitter directly rather than SingleDimensionFitter — the model is fixed to
, so unlike the other fitters in the package it needs no evaluator listener, just the raw
x/y (and optionally sig) arrays:
-
StraightLineFitter(double[] x, double[] y)— standard deviations unknown; estimated from the residuals as described above. -
StraightLineFitter(double[] x, double[] y, double[] sig)— standard deviations known.
After fit() completes:
-
getA()/getB()— the fitted parameters and . -
getSigA()/getSigB()— their estimated standard deviations and . -
getChi2()— the final value. -
getQ()— the goodness-of-fit probability (only meaningful whensigwas provided). -
getSigdat()— the estimated common per-point error , set only whensigwas not provided.