python lmfit [nan] values

1k views Asked by At

I am trying to use lmfit for a global fit problem (schild analysis). I have some shared parameters and some that are calculated based on these shared. At one point the function encounters log for a negative number and throws a [nan] list causing it to fail. How do I prevent that? Thank you.

def g1(params,xdata,ydata):

    hillSlope = params['hillSlope'].value
    schildSlope = params['SchildSlope'].value
    top = params['top'].value
    bottom = params['bottom'].value
    pA2 = params['pA2'].value


    EC50_1 = params['ec50_2'].value
    B_1 = params['B_2'].value
    Antag_1 = 1+(B_1/(10**(-1*pA2)))**schildSlope
    LogEC_1=np.log10(EC50_1*Antag_1)
    y_model_1 = y_model_1 = bottom + (top-bottom)/(1+10**((LogEC_1-xdata)*hillSlope))
2

There are 2 answers

0
Andri On

You can set the nan_policy in the lmfit to 'omit'! More info here https://lmfit.github.io/lmfit-py/model.html

0
M Newville On

Using nan_policy as @Andri suggests may be a good thing to try. Even better is to prevent the nans from happening in the first place. Of course, log(x) will give nan if x<0. For example, make sure that your EC50_1 cannot be negative by setting params['ec50_2'].min = 0. Also, check that your Antag_1 is positive. Just to be safe, be mindful of the fact that x**y will be complex for x < 0.

In short, if your fit function can ever generate nan for any combination of parameter values, your fit will fail. You have to handle and/or prevent these cases.