[SciPy-User] Fit function to multiple datasets

Bevan Jenkins bevan07 at gmail.com
Wed Mar 30 19:17:23 EDT 2011


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.




More information about the SciPy-User mailing list