ScientificPython - LeastSquareFit diverges

Harold Fellermann dadapapa at googlemail.com
Tue Jul 18 09:59:01 EDT 2006


Dear all,

I am trying to fit a powerlaw to a small dataset using
Scientific.Functions.LeastSquares fit.
Unfortunately, the algorithm seems to diverge and throws an
OverflowException.
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),
...     (17.5, 36.0, 0.10000000000000001),
...     (22.5, 16.0, 0.10000000000000001),
...     (27.5, 7.0, 0.10000000000000001),
...     (32.5, 6.0, 0.10000000000000001),
...     (37.5, 3.0, 0.10000000000000001),
...     (42.5, 3.0, 0.10000000000000001),
...     (47.5, 1.0, 0.10000000000000001),
...     (52.5, 3.0, 0.10000000000000001),
...     (57.5, 1.0, 0.10000000000000001),
...     (67.5, 1.0, 0.10000000000000001),
...     (77.5, 2.0, 0.10000000000000001),
...     (82.5, 1.0, 0.10000000000000001),
...     (87.5, 2.0, 0.10000000000000001)
... ]
>>>
>>> def powerlaw((a,b),x) :
...     return a*x**b
...
>>> params,chisq = leastSquaresFit(powerlaw,(10,-3),data)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File
"/usr/lib/python2.4/site-packages/Scientific/Functions/LeastSquares.py",
line 72, in leastSquaresFit
    next_chi_sq, next_alpha = _chiSquare(model, next_p, data)
  File
"/usr/lib/python2.4/site-packages/Scientific/Functions/LeastSquares.py",
line 22, in _chiSquare
    f = model(parameters, point[0])
  File "<stdin>", line 2, in powerlaw
  File
"/usr/lib/python2.4/site-packages/Scientific/Functions/FirstDerivatives.py",
line 182, in __rpow__
    return pow(other, self)
  File
"/usr/lib/python2.4/site-packages/Scientific/Functions/FirstDerivatives.py",
line 171, in __pow__
    raise OverflowError, "Numerical result of pow(%s,%s) out of range."
% (self.value,other.value-1)
OverflowError: Numerical result of pow(2.5,8376.79243687) out of range.
>>>


I added some debugging output in
/usr/lib/python-2.4/site-packages/Scientifc/Functions/LeastSquares.py
in the function _chiSquare that prints the fit parameters during the
Levenberg-Marquardt iteration.
The procedure seems do diverge after the first step:

((10, [1]), (-3, [0, 1]))
[(-67402.311817579117, [1]), (8377.7924368716158, [0, 1])]

Note that I could easily fit the above data using gnuplots internal
fitting procedure. Any idea what is going wrong here? Is it a known
problem? Are there any work arounds or other packages?

Any help is appreciated!

- harold -




More information about the Python-list mailing list