I would like to use the lmfit module to fit a function to a variable number of data-sets, with some shared and some individual parameters.
Here is an example generating Gaussian data, and fitting to each data-set individually:
import numpy as np
import matplotlib.pyplot as plt
from lmfit import minimize, Parameters, report_fit
def func_gauss(params, x, data=[]):
A = params['A'].value
mu = params['mu'].value
sigma = params['sigma'].value
model = A*np.exp(-(x-mu)**2/(2.*sigma**2))
if data == []:
return model
return data-model
x = np.linspace( -1, 2, 100 )
data = []
for i in np.arange(5):
params = Parameters()
params.add( 'A' , value=np.random.rand() )
params.add( 'mu' , value=np.random.rand()+0.1 )
params.add( 'sigma', value=0.2+np.random.rand()*0.1 )
data.append(func_gauss(params,x))
plt.figure()
for y in data:
fit_params = Parameters()
fit_params.add( 'A' , value=0.5, min=0, max=1)
fit_params.add( 'mu' , value=0.4, min=0, max=1)
fit_params.add( 'sigma', value=0.4, min=0, max=1)
minimize(func_gauss, fit_params, args=(x, y))
report_fit(fit_params)
y_fit = func_gauss(fit_params,x)
plt.plot(x,y,'o',x,y_fit,'-')
plt.show()
# ideally I would like to write:
#
# fit_params = Parameters()
# fit_params.add( 'A' , value=0.5, min=0, max=1)
# fit_params.add( 'mu' , value=0.4, min=0, max=1)
# fit_params.add( 'sigma', value=0.4, min=0, max=1, shared=True)
# minimize(func_gauss, fit_params, args=(x, data))
#
# or:
#
# fit_params = Parameters()
# fit_params.add( 'A' , value=0.5, min=0, max=1)
# fit_params.add( 'mu' , value=0.4, min=0, max=1)
#
# fit_params_shared = Parameters()
# fit_params_shared.add( 'sigma', value=0.4, min=0, max=1)
# call_function(func_gauss, fit_params, fit_params_shared, args=(x, data))
I think you're most of the way there. You need to put the data sets into an array or structure that can be used in a single, global objective function that you give to minimize() and fits all data sets with a single set of Parameters for all the data sets. You can share this set among data sets as you like. Expanding on your example a bit, the code below does work to do a single fit to the 5 different Gaussian functions. For an example of tying parameters across data sets, I used nearly identical value for sigma the 5 datasets the same value. I created 5 different sigma Parameters ('sig_1', 'sig_2', ..., 'sig_5'), but then forced these to have the same values using a mathematical constraint. Thus there are 11 variables in the problem, not 15.
For what it's worth, I would consider holding the multiple data sets in a dictionary or list of DataSet class instead of a multi-dimensional array. Anyway, I hope this helps get you going onto what you really need to do.