Parameter stuck at the initial value (when I don't think it should)

53 views
Skip to first unread message

Jenny Zhang

unread,
Nov 1, 2024, 1:15:16 PM11/1/24
to lmfit-py
Dear lmfit team,

I find one of my parameters always stuck at the initial value, and I cannot seem to solve that problem even after reading the FAQ webpage. 
The model function is defined as the following:

def gammastep3SFH(p, timegrid_template):
    cal_timegrid = np.linspace(0,14,1000) # This is in lookback time
    if p[0] < 1: # to avoid physical warning
        cal_timegrid[np.argmin(cal_timegrid)]=0.01
    m1 = cal_timegrid**(p[0]-1)*np.exp(-cal_timegrid/p[1]) # This is in time
    m1 = m1[::-1] # Changing array direction
    m2 = p[2] * np.ones(len(cal_timegrid))
    from scipy.special import erf
    sfr = m1 * (erf(cal_timegrid-p[3])+1)/2 + m2 * (1-erf(cal_timegrid-p[3]))/2
    # Rewrite values between 0 - 0.3 to p[4]
    sfr[cal_timegrid<=0.3] = p[4] # p[4] is called "step2"
    sfr = sfr/sci.simps(sfr, cal_timegrid) # normalize the area to 1
    # Interpolate into lookback time
    sfr_out = np.interp(timegrid_template, cal_timegrid, sfr, left=None, right=0)
    return sfr_out

I highlight the problematic part, where I think p[4] should be able to adjust to the "right" values, but always get stuck. 
For the boundary of this parameter, I have:

params.add('width', value=1., min=0., max=13.) # This is p[3]
params.add('step2', value=1., min=0.,) # This is p[4]

where only a lower limit is provided for p[4] for a physical reason. 
And in fact, by adding the highlighted line, I also have another parameter, p[3], often stuck at the initial value.

Here below is a report
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 110
    # data points      = 87
    # variables        = 5
    chi-square         = 0.09297116
    reduced chi-square = 0.00113379
    Akaike info crit   = -585.199545
    Bayesian info crit = -572.870004
##  Warning: uncertainties could not be estimated:
    width:      at initial value
    step2:      at initial value
[[Variables]]
    alpha:      5.75756313 (init = 2)
    tau_gamma:  1.64225733 (init = 10)
    step:       137.556273 (init = 1)
    width:      1.00000000 (init = 1)
    step2:      1.00000000 (init = 1)

I don't know if p[3] is actually causing the trouble, but only looking at the result of p[4], it really does not make any sense.

I appreciate your help in advance!
Best,

matt.n...@gmail.com

unread,
Nov 1, 2024, 1:57:22 PM11/1/24
to lmfi...@googlegroups.com

Hi Jenny,

 

Please include code that shows how you set up and do this fit.  I think your model function cannot work.

For example, you have

 

> The model function is defined as the following:

>     def gammastep3SFH(p, timegrid_template):

>       ….

>       sfr[cal_timegrid<=0.3] = p[4] # p[4] is called "step2"

 

That should not be the case.  If you are using the Model class,  the first argument for the model function (the function to calculate the model that should match the data passed in) is typically the independent variable (say, `x`).  The other named function arguments will have the parameter values (as floats).

 

If using the Minimizer class, the objective function (which returns the quantity to be minimized) does take `params` as the first argument.  But that is an lmfit.Parameters object, which is a dict:  individual parameters are accessed by name, not by integer index.

 

--Matt

--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lmfit-py/3b4c55a3-a85b-49e4-be73-a145aab77863n%40googlegroups.com.

Jenny Zhang

unread,
Nov 4, 2024, 10:03:54 AM11/4/24
to lmfit-py
Dear Matt,

Thanks for your fast response. I actually noticed another bug in the code that actually cause the problem. Sorry that I did not initially include all the code. I was writing everything in different defined functions, that's why it seems that the parameters are in metric format. 

Best,

Reply all
Reply to author
Forward
0 new messages