simultaneous fitting python parameter sharing
<p>I have six datasets, I wish to fit all six datasets simultaneously, with two parameters common between the six datasets and one to be fit seperately.</p>

<p>I'm planning to fit a simple ax**2+bx+c polynomial to the datasets, where a and b is shared between the six datasets and the offset, c, is not shared between the six. </p>

<p>Therefore I'm fitting a common slope between the datasets but with a variable offset.</p>

<p>I'm fully competent in fitting them individually, however as the slopes are similar between each dataset, the error on the offset, c, would be greatly improved using simultaneous fitting.</p>

<p>I typically fit using the scipy.optmize.curve_fit.</p>

<pre><code>import numpy as np
from scipy.optimize import curve_fit

def func(x,a,b,c):
    return (a*(x**2)+b*x+c)
def fit(x,y,yerr):
    popt, pcov = curve_fit(func,x,y,p0=[-0.6,5,-12],sigma=yerr)
    chi=np.sum( ((func(x, *popt) - y) / yerr)**2)
    redchi=(chi-1)/len(y)
    return popt,pcov,redchi,len(y)
</code></pre>

<p>I'm handling 6 sets of: x,xerr,y,yerr
len(x) and len(y) is different for each set.</p>

<p>I understand I have to concatenate the datasets and fit them this way.</p>

<p>If anyone can offer any advice or help, I'm sure it would be beneficial for both me and the community.</p>

<p>One possibility is to change the function to be fitted so that each data set has its own "a" and "b" parameter with a common "c", similar to this crude code snippet:</p>

<pre><code>def func(x,a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6, c):
    if x in data_set_1:
        return (a1*(x**2)+b1*x+c)
    if x in data_set_2:
        return (a2*(x**2)+b2*x+c)
    if x in data_set_3:
        return (a3*(x**2)+b3*x+c)
    if x in data_set_4:
        return (a4*(x**2)+b4*x+c)
    if x in data_set_5:
        return (a5*(x**2)+b5*x+c)
    if x in data_set_6:
        return (a6*(x**2)+b6*x+c)
    raise Exception('Data outside fitting range') # just in case
</code></pre>

python curve-fitting least-squares data-fitting