Reputation:
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!
Upvotes: 1
Views: 4991
Reputation: 879491
The second argument of optimize.minimize
is an initial guess -- your guess for the minimum x
-value that you wish optimize.minimize
to find. So, for example,
import numpy as np
from scipy import optimize
x0 = 0.1
fun = lambda x: 0.5 * np.exp(-x * (1-x))
res = optimize.minimize(fun, x0, method='Nelder-Mead')
print(res)
yields
status: 0
nfev: 36
success: True
fun: 0.38940039153570244
x: array([ 0.5])
message: 'Optimization terminated successfully.'
nit: 18
x0
need not always be a scalar. It could be an array -- it depends on fun
. In the above example, x0 = np.array([0.1])
would also work. The key point is that for whatever you guess, fun(x0)
should be a scalar.
Upvotes: 2