ScientificPython - LeastSquareFit diverges

Terry Reedy tjreedy at udel.edu
Tue Jul 18 15:28:06 EDT 2006


"Harold Fellermann" <dadapapa at googlemail.com> wrote in message 
news:1153231141.343981.153630 at s13g2000cwa.googlegroups.com...
> I am trying to fit a powerlaw to a small dataset using
> Scientific.Functions.LeastSquares fit.

This is a bit off-topic here, and normally better for the scipy list, but I 
have some experience with nonlinear least squares.

> Unfortunately, the algorithm seems to diverge and throws an
> OverflowException.

Assuming the program is okay, this means that either the function 
mismatches the data or the initial values are too far off to converge.

> Here is how I try it:
>>>> from Scientific.Functions.LeastSquares import leastSquaresFit
>>>>
>>>> data = [
> ...     (2.5, 589.0, 0.10000000000000001),
> ...     (7.5, 442.0, 0.10000000000000001),
> ...     (12.5, 96.0, 0.10000000000000001),

I presume that tuples are x, y, some_error_indicator.  But the last does 
not matter here.

>>>> def powerlaw((a,b),x) :
> ...     return a*x**b

Did you try plotting logx versus log y to see if you get approximately a 
straight line?  If so, the intercept and slope are estimates of loga and b.
> ...
>>>> params,chisq = leastSquaresFit(powerlaw,(10,-3),data)

I presume (10,-3) is the starting (a,b).  But, for instance 10*7.5**-3 = 
.02, which has no relation to 442, whereas, for instance, 1000*7.5-.75 = 
221, which is in the ballpark, at least.  So (a,b)=(1000, -.75) might have 
a chance.

Terry Jan Reedy






More information about the Python-list mailing list