[SciPy-User] Fit function to multiple datasets
David Baddeley
david_baddeley at yahoo.com.au
Wed Mar 30 20:08:35 EDT 2011
how about something like:
def objFunc(params, time, Q):
scale =params[0]
loc = params[1]
res = []
extra_params = params[2:].reshape(-1, 2)
for p_n, t_n, Q_n in zip(extra_params, time, Q):
res.append(solve(t_n, loc, scale, p_n[0], p_n[1]) - np.log(Q_n))
return np.hstack(res)**2
p0 = [est_loc,est_scale, est_shape_1, est_arbit_multi_1, est_shape_2,
est_arbit_multi_2, .....]
optimize.leastsq(objfunc, p0, args=([time_1, time_2 ....], [a_1, a_2, ...]))
cheers,
David
----- Original Message ----
From: Bevan Jenkins <bevan07 at gmail.com>
To: scipy-user at scipy.org
Sent: Thu, 31 March, 2011 12:17:23 PM
Subject: [SciPy-User] Fit function to multiple datasets
Hello,
I have a function that I would like to fit to multiple datasets. The function
has 4 parameters. I would like to find the best fit across all datasets for 2
(scale and shape) and I would like to fit the remaining 2 (loc and arbit_multi)
to each set.
I have searched the scipy and numpy mailing lists but despite finding some
information I haven't managed to piece it together.
The code below shows what I am currently doing. There will also be the
situation where the datasets are different lengths and I would like to weight
the results by the length.
import numpy as np
from scipy import optimize
def gen_pdf(time, loc, scale, shape, arbit_multi):
'''define the 3-param Weibull distn f(x) with arbitary positive multipler
'''
return arbit_multi*(shape/scale)*((time-loc)/scale)**(shape-1)*np.exp(-
(((time-loc)/scale)**shape))
def solve(time, est_loc, est_scale, est_shape, est_arbit_multi):
return (np.log(est_arbit_multi*(est_shape/est_scale))+
(est_shape-1)*np.log((time-est_loc)/est_scale)-
((time-est_loc)/est_scale)**est_shape)
def objfunc(params,time,Q):
'''error func
'''
return (solve(time, params[0],params[1],params[2],params[3])- np.log(Q))**2
n=30
time = np.linspace(1,n,n)
a = gen_pdf(time, loc=-15.0, scale=10.0, shape=0.5, arbit_multi=100.0)
b = gen_pdf(time, loc=-10.0, scale=10.0, shape=0.5, arbit_multi=10.0)
c = gen_pdf(time, loc=-10.0, scale=10.0, shape=0.5, arbit_multi=25.0)
alldata= np.array((a,b,c))
est_loc = 0.0
est_scale = 1.0
est_shape = 1.0
est_arbit_multi = 1.0
p0 = [est_loc,est_scale, est_shape, est_arbit_multi]
p1, success = optimize.leastsq(objfunc, p0, args=(time, a))
print 'a ests=',p1
p1, success = optimize.leastsq(objfunc, p0, args=(time, b))
print 'b ests=',p1
p1, success = optimize.leastsq(objfunc, p0, args=(time, c))
print 'c ests=',p1
Any help would be appreciated.
_______________________________________________
SciPy-User mailing list
SciPy-User at scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user
More information about the SciPy-User
mailing list