I know that there are plenty of posts similar to mine but I really don't manage to apply them to my situation so please I ask for your help. Basically, my code is the following:
def Black_min(f, k, ann, vol, ex, cp):
d1=(math.log(f/k)+0.5*vol**2*ex)/math.sqrt(vol**2*ex)
d2=(math.log(f/k)-0.5*vol**2*ex)/math.sqrt(vol**2*ex)
Price=cp*(f*norm.cdf(cp*d1)-k*norm.cdf(cp*d2))*ann*20000
return Price
banana = lambda x: (Price_Cube[0][4]-Black_min(F[0], K[0][4], Annuity[0], x, Expiry[0], CP[4]))**2
xopt, fopt = scipy.optimize.fmin(func=banana, x0=[Vol_Cube[0][0]], disp=False)
Price_Cube, K are lists of lists with numbers inside, Annuity, Expiry and CP are simply lists. When I run the code, I obtain this.
xopt, fopt = scipy.optimize.fmin(func=banana, x0=[Vol_Cube[0][0]], disp=False)
ValueError: need more than 1 value to unpack
I'm aware that it is a silly mistake, but I really don't see it. Any help is appreciated, thanks in advance.
The problem is that you try to assign the return value of fmin
to two variables, xopt
and fopt
, but by default fmin
only returns xopt
. You should try
xopt = scipy.optimize.fmin(func=banana, x0=[Vol_Cube[0][0]])
If you also want the function value at the minimum, you have to set full_output
to True
, and then you also get some more diagnostics:
xopt, fopt, iter, funccalls, warnflags = scipy.optimize.fmin(func=banana, x0=[Vol_Cube[0][0]], full_output=True)