1

I am trying to generate the solutions to PCA weights for some (yield-curve) market-data. However, I am getting an error message in my scipy.optimize.minimize function.

The main error is that it seems to be reading the arguments into the minimization function wrong (error_sum).

I looked up the generic form here, but it doesn't work for my code when I utilize it. Scipy Minimize - Unable to minimize objective function

import scipy as sc
import scipy.optimize as optimize
from scipy.optimize import minimize

w1 = 1.0
w2 = 1.0
w3 = 1.0

row_C = np.zeros(len(df_.columns)) # initialize current row as zero
row_T = df_.iloc[-1].values # get the target row, which we have set as the last row of the panda dataframe

row_c = np.array([-0.35865725, 0.52793819, 0.70654759, -0.28909144, 1.08467752, 0.91287324])
row_t = np.array([1.7971, 2.5756, 2.2005, 1.4966, 1.45  , 1.8022])

def error_sum(row_c, row_t, params): # row_c is estimated and row_t is target
    w1 = params[0]
    w2 = params[1]
    w2 = params[2]

    if len(row_c) != len(row_t): return print('error where x and y points are not same length')
    for cnt in range(len(row_c)):
        row_c[cnt] = w1 * row1[cnt] + w2 * row2[cnt] + w3 * row3[cnt]

    return np.sum(np.abs(row_c - row_t))

for cnt in range(len(df_.columns)): # loop to calculate the PCA-based moves
    row_c[cnt] = w1 * row1[cnt] + w2 * row2[cnt] + w3 * row3[cnt]

print(np.sum(np.abs(row_c - row_t))) # this is to get the sum of absolute difference errors
print(error_sum(row_c, row_t, x0))

x0 = np.array([1.0, 1.0, 1.0]) # parameters to optimize
bnds = ((-10.0, 10.0), (-10.0, 10.0), (-10.0, 10.0)) # boundary conditions of x0 parameter set
options = {'maxiter': 100}

res = minimize(error_sum, x0 ,(row_c, row_t), bounds = bnds, method='nelder-mead', options={'xtol': 1e-8, 'disp': True})

The error message as per below

error where x and y points are not same length

TypeError                                 Traceback (most recent call last)
<ipython-input-158-8c50b421e58a> in <module>()
     32 options = {'maxiter': 100}
     33 
---> 34 res = minimize(error_sum, x0 ,(row_c, row_t), bounds = bnds, method='nelder-mead', options={'xtol': 1e-8, 'disp': True})

C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    473                       callback=callback, **options)
    474     elif meth == 'nelder-mead':
--> 475         return _minimize_neldermead(fun, x0, args, callback, **options)
    476     elif meth == 'powell':
    477         return _minimize_powell(fun, x0, args, callback, **options)

C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in _minimize_neldermead(func, x0, args, callback, maxiter, maxfev, disp, return_all, initial_simplex, xatol, fatol, **unknown_options)
    549         doshrink = 0
    550 
--> 551         if fxr < fsim[0]:
    552             xe = (1 + rho * chi) * xbar - rho * chi * sim[-1]
    553             fxe = func(xe)

TypeError: '>' not supported between instances of 'float' and 'NoneType'

1 Answer 1

1

Try to change the order of the arguments in the definition of error_sum to

def error_sum(params, row_c, row_t)

if you want to get the optimum of params and call the function like this:

minimize(error_sum, x0, args=(row_c, row_t), bounds = bnds, method='nelder-mead', options={'xtol': 1e-8, 'disp': True})
Sign up to request clarification or add additional context in comments.

1 Comment

that was it! Thanks!

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.