Accurate least-squares fit algorithm needed

Posted by ggkmath on Stack Overflow See other posts from Stack Overflow or by ggkmath
Published on 2010-12-01T20:14:15Z Indexed on 2010/12/28 14:54 UTC
Read the original article Hit count: 600

Filed under:
|

I've experimented with the two ways of implementing a least-squares fit (LSF) algorithm shown here.

The first code is simply the textbook approach, as described by Wolfram's page on LSF. The second code re-arranges the equation to minimize machine errors. Both codes produce similar results for my data. I compared these results with Matlab's p=polyfit(x,y,1) function, using correlation coefficients to measure the "goodness" of fit and compare each of the 3 routines. I observed that while all 3 methods produced good results, at least for my data, Matlab's routine had the best fit (the other 2 routines had similar results to each other).

Matlab's p=polyfit(x,y,1) function uses a Vandermonde matrix, V (n x 2 matrix) and QR factorization to solve the least-squares problem. In Matlab code, it looks like:

V = [x1,1; x2,1; x3,1; ... xn,1]  % this line is pseudo-code
[Q,R] = qr(V,0);
p = R\(Q'*y);      % performs same as p = V\y

I'm not a mathematician, so I don't understand why it would be more accurate. Although the difference is slight, in my case I need to obtain the slope from the LSF and multiply it by a large number, so any improvement in accuracy shows up in my results.

For reasons I can't get into, I cannot use Matlab's routine in my work. So, I'm wondering if anyone has a more accurate equation-based approach recommendation I could use that is an improvement over the above two approaches, in terms of rounding errors/machine accuracy/etc.

Any comments appreciated! thanks in advance.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about least-squares