[SciPy-User] [SciPy-user] Fit function to multiple datasets

bevan j bevan07 at gmail.com
Wed Mar 30 21:56:22 EDT 2011


Thanks, your solution works.

However, there are still three things (at least...) that I need to get my
head around:

- a quick test in which I vary the shape parameter (in one dataset) does not
give me sensible answers.  I have been more concerned with getting this
working than thinking about the actual answer.

- also for any given site, I will have an unknown ( in advance) number of
datasets but I should just be able to put them all into one array to pass to
the function and iterate through the array.

- how to weight the answer based on the length of each data set.

Thanks again,

Bevan

code at present:
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): 
    scale  =params[0] 
    shape = 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, p_n[0], scale, shape, p_n[1]) - np.log(Q_n)) 

    return np.hstack(res)**2 

n=30
time = np.linspace(1,n,n)
time_3 = time_1 = time_2 = time

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.8, 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

est_loc_1 = est_loc_2 = est_loc_3 = est_loc

est_arbit_multi_1 = est_arbit_multi_2 = est_arbit_multi_3 = est_arbit_multi

p0 = [est_scale, est_shape, est_loc_1, est_arbit_multi_1, est_loc_2, 
est_arbit_multi_2, est_loc_3, est_arbit_multi_3] 
p1 = optimize.leastsq(objFunc, p0, args=([time_1, time_2,time_3], [a,b,c])) 
print p1



David Baddeley wrote:
> 
> 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
> 
> 

-- 
View this message in context: http://old.nabble.com/Fit-function-to-multiple-datasets-tp31282087p31282699.html
Sent from the Scipy-User mailing list archive at Nabble.com.




More information about the SciPy-User mailing list