0

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.

9
  • Your code optimizesexpected_profit which 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. Commented Sep 2, 2018 at 18:59
  • scipy built on numpy, but doesn't 'know' anything about pandas. 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. Commented Sep 2, 2018 at 19:00
  • You could pass a dataframe to your function via the args parameter - 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). Commented Sep 2, 2018 at 19:02
  • I've corrected the name of the objective function @sacha Commented Sep 2, 2018 at 19:04
  • You are giving an initial value of x0, a list with number 1. But it looks like pred_money expects a row of data, the dataframe. The optimizing code varies that x0 seeking to minimize the func. So pred_money(np.array(x0)) should run and produce a meaningful initial objective vaue. Commented Sep 2, 2018 at 19:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.