optimization - scipy.optimize.minimize keeps returning math error, going out of bounds -
i'm using scipy.opimize.minimize find best fit parameters negative log-likelihood of dataset (which has given pdf). functions calculating pdf , negative log-likelihood given below:
def density(time, x, y, z): w = (z/x)*mt.exp(-time/x) + ((1-z)/y)*mt.exp(-time/y) return w def nll2(params): sig = 0 a, b, c = params[0], params[1], params[2] in range(0, 100): sig = sig - mt.log(density(xval[i], a, b, c)) return sig where xval dataset (list of 100time values between 0-10). it's worth, pdf of double decay process.
i have been given starting values x, y , z (stored in list called "initial_guesses" given nll2 function "param" argument), , have find best-fit values of x, y , z (i.e. values minimise nll2 given dataset xval).
i try following line of code:
initial_guesses = [1.30719636, 0.19783642, 0.25150731] best_fit = sci.optimize.minimize(nll2, initial_guesses, bounds = [(0.1, 20), (0.1, 20), (0.1, 1)], method = 'l-bfgs-b').x the bounds have been chosen i'm "supposed" come [1.3, 0.2, 0.25] i.e. close initial values. however, when this, end error message:
file "newcode2.py", line 125, in nll2 sig = sig - mt.log(density(xval[i], x, y, z)) valueerror: math domain error
i know happening because logarithm being taken of negative value, bounds i've given minimizer should prevent happening.
to investigate further, changed nll2 function following:
def nll2(params): sig = 0 a, b, c = params[0], params[1], params[2] in range(0, 100): if density(xval[i], a, b, c) > 0: sig = sig - mt.log(density(xval[i], a, b, c)) else: print density(xval[i], a, b, c), xval[i], params, '\n' return sig an example of terminal prints out following:
-4.25654045048e-10 2.76533572107 [ 0.1 20. 1.00000001]
so somehow parameters going limits of bounds i've set, in last case going on 1e-8. causing density function return minutely negative number, causing crash.
does know how can stop minimizer doing this? i've tried using slsqp method, same problem.
i apologize if i've given detail, i'm tired , want give out information might useful.
nb i've imported math class mt.
Comments
Post a Comment