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.
expected_profitwhich is not defined. So it's unclear what you are doing. But if you ask to minimize a function which has no scalar-objective, scipy can't help you (last time i checked). In those cases it's unclear how solutions are ordered? Which output is better? Compare with wiki's vector opt page. Model-wie your problem might be as simple as collapsing into a min-norm model of your time-series like predictions (like least-squares), but that's guessing.scipybuilt onnumpy, but doesn't 'know' anything aboutpandas. Neither the documentation or the code treats a DataFrame in special way. It's either just another python object, or it may try to turn it into an array.argsparameter - provided your function knows how to use it. But make sure you are passing a tuple, e.g.args: (df,)(the comma matters if there's only one element).x0, a list with number 1. But it looks likepred_moneyexpects a row ofdata, the dataframe. The optimizing code varies thatx0seeking to minimize thefunc. Sopred_money(np.array(x0))should run and produce a meaningful initial objective vaue.