简体   繁体   中英

overflow in exp using python scipy.optimize.basinhopping

I am using scipy.optimize.basinhopping in order to fit a simple exponential function (a exp(-b time)) to real data. I try to have appropriate initial guesses (for a and b) but in some iterations (for some values basinhopping guesses) "overflow in exp" occurs. I know that it is because of a very large answer to be calculated by exp. By the way the result is something absolutely wrong. Is there anyway to ask the code to ignore those error containing guesses in order to prevent wrong results in output? + time goes from 0 to something around e+06 Thanks for your care and help

here is my code. after running, I get overflow error for some values for bk, so the resulting value for ret is absolutely wrong, something far far from the correct answer. :(

def model(bk):
    s = 0
    realData = data()
    modelData = []
    modelData.append(realData[0])
    for time in range(len(realData) - 1):
        x = realData[0] * np.exp((bk[0] * np.exp(bk[1]*time))*time)
        y = 1 - realData[0] + x
        i = x / y
        modelData.append(i)
        s+=np.abs(i-realData[time])

    return(s)
def optimize():
    bk0 = [1,-1]
    minimizer_kwargs = {"method" : "BFGS"}
    ret = basinhopping(model, bk0, minimizer_kwargs=minimizer_kwargs, niter=100)
    print(ret)

optimize()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM