tBuLi / symfit

Symbolic Fitting; fitting as it should be.
http://symfit.readthedocs.org
MIT License
235 stars 19 forks source link

Gaussian fit example doesnt fit #118

Closed Jhsmit closed 6 years ago

Jhsmit commented 7 years ago

When running the file examples/gaussian.py the fit is not successful.

Python 2.7 symfit v 0.3.6

C:\Miniconda3\envs\py27_cp\lib\site-packages\scipy\optimize\slsqp.py:341: RuntimeWarning: invalid value encountered in greater
  bnderr = where(bnds[:, 0] > bnds[:, 1])[0]

Parameter Value        Standard Deviation
A         2.591857e+03 None
sig       1.412056e+00 None
x0        1.318367e+02 None
Fitting status message: Optimization terminated successfully.
Number of iterations:   16
Regression Coefficient: -0.896846508307

A*exp(-(-x0 + x)**2/(2*sig**2))

Process finished with exit code 0

Python 3.5 symfit v 0.3.5:

C:\Miniconda3\envs\py_main\python.exe C:/Users/Smit/.PyCharm2017.1/config/scratches/scratch.py

Parameter Value        Standard Deviation
A         3.974905e+03 None
sig       1.415379e+00 None
x0        1.594154e+02 None
Fitting status message: Optimization terminated successfully.
Number of iterations:   16
Regression Coefficient: -0.8784196878447796

A*exp(-(-x0 + x)**2/(2*sig**2))

The problem can be fixed by providing an accurate guess for the A parameter, by changing line 11 to A = Parameter(value=300), however when the guess if too far off the fitting fails (already when `value=200').

pckroon commented 7 years ago

In my experience Gaussian and ODE models are indeed extremely sensitive to the initial guess. I would also not be surprised if the chosen algorithm (and meta-parameters) has a major influence on this. Good catch on the example though! Make a PR to fix it ;)

As far as I'm concerned there's a major code refactor pending, after which it should be possible to easily add a few global optimization methods --- which would solve this in a very permanent fashion.

Jhsmit commented 7 years ago

update: in python 3.5 the fitting sometimes works due to the random nature of the data.