user1975135
user1975135

Reputation: 1

lmfit- tying fitting parameters one to the other

I'm using python lmfit module to fit multiple Gaussian. What I want is to tide one parameter to the other trough mathematical expression, for example:

def gaussian(x,a1,c1,w1,a2,w2,c2):
         g1=a1*np.exp(-(x-c1)**2/(2*w1**2))
         g2=a2*np.exp(-(x-c2)**2/(2*w2**2))
         return g1+g2

gmodel=Model(gaussian)
result=gmodel.fit(y=y,x=x,params...)

What i want is to tie parameters, ex. that a1=a2/2. Is there way to that with lmfit package?

Upvotes: 0

Views: 1127

Answers (2)

L Selter
L Selter

Reputation: 352

I have just worked out the answer to my own, very similar question here:

Scipy curve_fit bounds and conditions

I hope that this is helpful, these helped me:

https://lmfit.github.io/lmfit-py/constraints.html

http://blog.danallan.com/projects/2013/model/

Upvotes: 0

M Newville
M Newville

Reputation: 7862

Yes, with lmfit you can use a mathematical expression to control the value of any parameter in terms of the others. You might do:

from lmfit.models import GaussianModel

# create model with two Gaussians
model = GaussianModel(prefix='g1_') + GaussianModel(prefix='g2_')

# create parameters for composite model, but with default values:
params = model.make_params()

# now set starting values, boundaries, constraints
params['g1_center'].set(5, min=1, max=7)
params['g2_center'].set(8, min=5, max=10)   

# constrain sigma for 'g2' to be the same as for 'g1'
params['g2_sigma'].set(expr='g1_sigma')

# you could also do something like this:
params['g2_amplitude'].set(expr='g1_amplitude / 10.0')

# now you're ready to fit this model to some data:
result = model.fit(data, params, x=x)

Upvotes: 1

Related Questions