Search code examples
pythonpython-3.xoptimizationleast-squares

object is not callable in function of least_squares, how to make it callable?


Trying to run a least_squares function, i am constantly facing the same error.

The object is never callable what ever the type of the return, from a tulpe to a ndarray


res_PSI = least_squares(fun=msecost(PSI, y, X, W, q, m), x0=PSI, jac='2-point', bounds=(-np.inf, np.inf), method='lm', ftol=1e-10,
                        xtol=1e-10, gtol=1e-10, max_nfev=4000)

The variables y, X, W, q and m are input variables. PSI is an output variable from another function actually used in msecost.

The stackoverflow is giving this :

  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 2060, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 2054, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 1405, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\pydevd.py", line 1412, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Azerty/PycharmProjects/OptionsTest/venv37/HARST/HAR.py", line 27, in <module>
    x = HARST.mrstar(y=dependent_variable,X=regressors,W=dummies,q=transition,T=T,nX=nX,nW=nW,M=M,rob=rob,flag=flag,sig=sig)
  File "C:\Users\Azerty\PycharmProjects\OptionsTest\venv37\HARST\HARST.py", line 216, in mrstar
    alpha, llambda, beta, gamma, c, fX, yhat, ehat, G = parestlm(y, X, W, q, T, nX, nW, m, gamma_0, c_0)
  File "C:\Users\Azerty\PycharmProjects\OptionsTest\venv37\HARST\HARST.py", line 335, in parestlm
    xtol=1e-10, gtol=1e-10, max_nfev=4000)
  File "C:\Users\Azerty\PycharmProjects\OptionsTest\venv37\lib\site-packages\scipy\optimize\_lsq\least_squares.py", line 807, in least_squares
    f0 = fun_wrapped(x0)
  File "C:\Users\Azerty\PycharmProjects\OptionsTest\venv37\lib\site-packages\scipy\optimize\_lsq\least_squares.py", line 802, in fun_wrapped
    return np.atleast_1d(fun(x, *args, **kwargs))
TypeError: 'numpy.ndarray' object is not callable

The actual msecost function is this:

def msecost(PSI, y, X, W, q, m):
    T, nX = X.shape
    nW = W.shape[1]

    gamma= getpar(PSI)[0][0]
    c = getpar(PSI)[0][1]

    Z = np.concatenate([X, W],axis=1)
    fX = np.zeros((T, m))
    dfX = np.zeros((T, m))
    for i in range(m):
        fX[:, i] = siglog(gamma * (q - c))
        dfX[:, i] = dsiglog(fX[:, i])
        Z = np.concatenate([Z, np.tile(fX[:, i], (nX, 1)).transpose() * X],axis=1)  # repmat
    theta = np.linalg.pinv(Z.transpose() @ Z) @ Z.transpose() @ y
    alpha = theta[0:nX]
    if not (W.all):
        beta = []
    else:
        beta = theta[nX:nX + nW]
    llambda = theta[nX + nW:].reshape(nX, m, order='F').copy()  # reshape
    if not (W.all):
        yhat = X @ alpha + np.sum(fX @ llambda.transpose() * X, axis=1)
    else:
        yhat = X @ alpha + W @ beta + np.sum(fX @ llambda.transpose() * X, axis=1)
    ehat = y - yhat
    f = np.sum(ehat ** 2) / T

    ggamma, gc = gradG(PSI, X, q, llambda, dfX, m)

    J = np.sum(-2 * np.tile(ehat, (2 * m,1 )).transpose() * np.concatenate([ggamma, gc],axis=1) / T)

    return np.array([f, J])

I excpect from least_squares that it takes msecost as a callable function and minimaze the returns f and J by using PSI as variable. I've absolutly no other constrain. Am I doing it wrong ?


Solution

  • You need to pass the reference of the function, so

    res_PSI = least_squares(fun=msecost, x0=PSI, ...)
    

    That is, the fit wants to call your msecost function with various parameter values. When you use

    res_PSI = least_squares(fun=msecost(PSI, y, X, W, q, m), x0=PSI, ...) # Nope!
    

    that sets the ndarray result of calling msecost() as the function -- that is why the exceptions are complaining about the ndarray object not being "callable"