I have a simple equation that I plotted via
def chernoff_bound(beta):
return 0.5 * np.exp(-beta * (1-beta))
betas = np.arange(0, 1, 0.01)
c_bound = chernoff_bound(betas)
plt.plot(betas, c_bound)
plt.title('Chernoff Bound')
plt.ylabel('P(error)')
plt.xlabel('parameter beta')
plt.show()
Now, I want to find the value where P(error) is minimum.
I tried to do it via the scipy.optimize.minimize()
function (honestly, I haven't used it before and there is maybe some error of thought here ...)
from scipy.optimize import minimize
x0 = [0.1,0.2,0.4,0.5,0.9]
fun = lambda x: 0.5 * np.exp(-x * (1-x))
res = minimize(fun, x0, method='Nelder-Mead')
And the error I get is:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-25-2b04597c4341> in <module>()
3 x0 = [0.1,0.2,0.4,0.5,0.9]
4 fun = lambda x: 0.5 * np.exp(-x * (1-x))
----> 5 res = minimize(fun, x0, method='Nelder-Mead')
6 print(res)
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
364
365 if meth == 'nelder-mead':
--> 366 return _minimize_neldermead(fun, x0, args, callback, **options)
367 elif meth == 'powell':
368 return _minimize_powell(fun, x0, args, callback, **options)
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/scipy/optimize/optimize.py in _minimize_neldermead(func, x0, args, callback, xtol, ftol, maxiter, maxfev, disp, return_all, **unknown_options)
436 if retall:
437 allvecs = [sim[0]]
--> 438 fsim[0] = func(x0)
439 nonzdelt = 0.05
440 zdelt = 0.00025
ValueError: setting an array element with a sequence.
I would very much appreciate it if someone can point me to the right track here!
See Question&Answers more detail:
os