tBuLi / symfit

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

Clarification on Fixing the Value of Parameter #150

Closed bhclowers closed 6 years ago

bhclowers commented 6 years ago

For the following example, I was hoping to get some clarification on the parameter c. My reading of this syntax is that the value of c should not change through the fitting routine. However, upon examination of the fit results it appears as though it has, in fact, be altered. Is there a way to truly fix the value so it doesn't change or am I simply misunderstanding the approach? Currently, I'm using symfit 0.4.0 and python 3.

from symfit import parameters, variables, exp, Fit, Parameter

a, b, c, d = parameters('a, b, c, d')
x, y = variables('x, y')

c.value = 4.0
c.fixed = True

model_dict = {y: a * exp(-(x - b)**2 / (2 * c**2)) + d}
fit = Fit(model_dict, x=xdata, y=ydata)
fit_result = fit.execute()

The output of the fit_result.params call is and I would expect c to still be 4.0:

OrderedDict([('a', 16.374368575343127),
             ('b', 0.49201249437123556),
             ('c', 0.5337962977235504),
             ('d', -9.55593614465743)])

Cheers, Brian

pckroon commented 6 years ago

Hi Brian,

That is indeed the expected output. However, I can't reproduce your issue:

from symfit import parameters, variables, exp, Fit

import numpy as np

xdata = np.arange(100)
ydata = np.arange(100)

a, b, c, d = parameters('a, b, c, d')
x, y = variables('x, y')

c.value = 4.0
c.fixed = True

model_dict = {y: a * exp(-(x - b)**2 / (2 * c**2)) + d}
fit = Fit(model_dict, x=xdata, y=ydata)
fit_result = fit.execute()

print(fit_result)
Parameter Value        Standard Deviation
a         -6.714024e+01 1.147315e+01
b         6.743333e+00 7.826861e-01
c         4.000000e+00 8.516989e-01
d         5.599811e+01 2.655657e+00
Fitting status message: Optimization terminated successfully.
Number of iterations:   24
Regression Coefficient: 0.3309511795685749

What's your scipy version? And can you double check the version of symfit you're running?

bhclowers commented 6 years ago

Using the pip show package command I have tried the example with: scipy 1.0.1 numpy 1.14.3 symfit 0.4.0

I have tried this with python 2.7 and 3.6.4.

The answer I get for python 2.7 is (which still isn't correct) but only if I change the variable c to 3.0. If I leave it at 4.0 I get a LinAlgError: Singular matrix exception:

Parameter Value        Standard Deviation
a         -6.231973e+04 6.465677e+02
b         -3.759177e+04 3.899764e+02
c         3.779818e+04 3.921493e+02
d         3.800524e+04 3.950286e+02
Fitting status message: b'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'
Number of iterations:   84
Regression Coefficient: 0.9999999999914775

For python 3.6 I get the following output when c is set to 4.0:

Parameter Value        Standard Deviation
a         -6.231973e+04 6.465677e+02
b         -3.759177e+04 3.899764e+02
c         3.779818e+04 3.921493e+02
d         3.800524e+04 3.950286e+02
Fitting status message: b'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'
Number of iterations:   84
Regression Coefficient: 0.9999999999914775

For clarity, I've tried this on 2 different systems each having both 2.7 and >=3.5 installed. I cannot appear to fix the value.

pckroon commented 6 years ago

Ok, this is getting interesting. On a different machine (py3.6.2) I can reproduce your issue.

Whats your version of six and sympy?

pip list package:

certifi (2018.4.16)                                                                                                
mpmath (1.0.0)                                                                                                          
numpy (1.14.3)                                                                                                          
pip (9.0.3)                                                                                                             
scipy (1.0.1)                                                                                                           
setuptools (39.0.1)                                                                                                     
six (1.11.0)                                                                                                            
symfit (0.4.0)                                                                                                          
sympy (1.1.1)                                                                                                           
wheel (0.31.0)                                                                                                          
wincertstore (0.2)  
bhclowers commented 6 years ago

Peter,

The versions I am using on the present box that still yields the odd error are:

symfit (0.4.0) scipy (1.0.1) six (1.11.0) numpy (1.14.3) sympy (1.1.1)

pckroon commented 6 years ago

Cheers. I also found the issue: we never actually implemented fixed parameters in 0.4.0... pr #151 should fix it though.

Peter

On Tue, 1 May 2018, 20:25 bhclowers, notifications@github.com wrote:

Peter,

The versions I am using on the present box that still yields the odd error are:

symfit (0.4.0) scipy (1.0.1) six (1.11.0) numpy (1.14.3) sympy (1.1.1)

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/tBuLi/symfit/issues/150#issuecomment-385748156, or mute the thread https://github.com/notifications/unsubscribe-auth/AMXfuRqXwKJWXoKC2vDM-tJiRxhvov_Tks5tuKiGgaJpZM4Tr41d .