0

I am doing some utility modeling but I got stuck when trying to optimize my objective function and I don't understand the problem.

I have a utility function (1) that takes experimental data as arguments as well as two parameters (theta and phi) with which I want to optimize function (1). The function returns the optimal choice given aforementioned data and parameter combinations.

The objective function (2) is basically a sum of squared residuals (SSE) which takes function (1) as an input and determines how well the optimal choice from function (1) performs against actual data.

This is where things go wrong. I try to optimize function 2 in respects of parameters theta and phi. Here's the code:

from scipy.optimize import minimize
    def costmixed(theta,phi,data):
    predictions = []
    for trialNr,trialDat in data.iterrows():
        predictions.append(mixed_model( 
        trialDat['inv'],trialDat['mult'],trialDat['belMult'],theta,phi))

    return np.sum(np.square(data['ret'] - np.array(predictions))) #returns sum of squared residuals.
#initial guesses
x0=[1000,0.5]
sol=minimize(costmixed,x0,args=[data],bounds=bnds)

And the subsequent errors I receive are:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-87cded55e0f8> in <module>()
      7     return np.sum(np.square(data['ret'] - np.array(predictions))) #returns sum of squared residuals.
      8 
----> 9 sol=minimize(costmixed,x0,args=[data],bounds=bnds)

C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    448     elif meth == 'l-bfgs-b':
    449         return _minimize_lbfgsb(fun, x0, args, jac, bounds,
--> 450                                 callback=callback, **options)
    451     elif meth == 'tnc':
    452         return _minimize_tnc(fun, x0, args, jac, bounds, callback=callback,

C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\lbfgsb.py in _minimize_lbfgsb(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, **unknown_options)
    326             # until the completion of the current minimization iteration.
    327             # Overwrite f and g:
--> 328             f, g = func_and_grad(x)
    329         elif task_str.startswith(b'NEW_X'):
    330             # new iteration

C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\lbfgsb.py in func_and_grad(x)
    271     if jac is None:
    272         def func_and_grad(x):
--> 273             f = fun(x, *args)
    274             g = _approx_fprime_helper(x, fun, epsilon, args=args, f0=f)
    275             return f, g

C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in function_wrapper(*wrapper_args)
    290     def function_wrapper(*wrapper_args):
    291         ncalls[0] += 1
--> 292         return function(*(wrapper_args + args))
    293 
    294     return ncalls, function_wrapper

TypeError: costmixed() missing 1 required positional argument: 'data'

I hope you wizards are able to help me! I am new to python so I may be missing something quite simple. Thanks in advance.

  • Maybe this can be helpful: https://stackoverflow.com/questions/19843752/structure-of-inputs-to-scipy-minimize-function – Georgy May 25 '18 at 15:01

1 Answers1

0

I figured it out eventually! The least_squares function (or any of the optimization functions in scipy I believe) take only one argument of optimization. This does not mean that n parameters can't be added; it means that when you define a cost/objective function, you must define one parameter as a list.

def function(params,data):
     theta, phi = params
     utility = p2*theta - (1-theta)*np.minimum((guilt + phi), (inequity - phi))
return utility

an alternative construction

def function(params,data):
     utility = p2*params[0]- (1-params[0])*np.minimum((guilt + params[1]), (inequity - params[1]))
return utility

Hope this helps. Note that the function above might not actually minimize, it's a random function.