I am trying to use scipy's basinhopping in conjunction with a function that I created. Although my question is related to this thread, the difference is that my function does not return a single value, but rather a python series
from scipy.optimize import basinhopping
############ Simple DataFrame ###########
data = pd.DataFrame({'def': [0, 0, 1, 1, 1], 'amt': [40, 20, 30, 50, 60],
'prob': [0.20, 0.10, 0.15, 0.30, 0.28],
'cost': [0.05, 0.01, 0.02, 0.09, 0.08],
'rate': [0.98, 0.75, 0.95, 0.76, 0.89]})
############ Function ############
def pred_money(row):
if (row["def"] == 0):
money = (row["amt"] * row["cost"]) * (1 - row["prob"])
return money
else:
money = row["amt"] * row["rate"] * row["prob"]
return money
I have tried using the optimization function as follows:
x0=[1.]
minimizer_kwargs = {"method": "BFGS", "args":(something_goes_here)}
ret = basinhopping(func=pred_money, x0=x0,
minimizer_kwargs=minimizer_kwargs,niter=200)
According to the basinhopping documentation other arguments of the function can be passed as part of the minimizer_kwargs dictionary. It is left blank in the code above because I simply do not know what goes in there. My suspicion is that it does not return a single value. I could be wrong.
Most examples in the documentation are fairly straight forward functions that does not have anything to do with a dataframe. Any help is appreciated. Thank you.