Scipy.optimize 最小化函数参数不适用于多个向量输入
Scipy.optimize minimize function arguments don't work for multiple vector inputs
我正在尝试为某些(收益率曲线)市场数据生成 PCA 权重的解决方案。但是,我在 scipy.optimize.minimize 函数中收到一条错误消息。
主要的错误是它似乎在将参数读入最小化函数时出错(error_sum)。
我在这里查找了通用形式,但是当我使用它时它对我的代码不起作用。
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})
报错信息如下
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'
尝试将 error_sum 定义中参数的顺序更改为
def error_sum(params, row_c, row_t)
如果你想获得最佳的参数并像这样调用函数:
minimize(error_sum, x0, args=(row_c, row_t), bounds = bnds, method='nelder-mead', options={'xtol': 1e-8, 'disp': True})
我正在尝试为某些(收益率曲线)市场数据生成 PCA 权重的解决方案。但是,我在 scipy.optimize.minimize 函数中收到一条错误消息。
主要的错误是它似乎在将参数读入最小化函数时出错(error_sum)。
我在这里查找了通用形式,但是当我使用它时它对我的代码不起作用。
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})
报错信息如下
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'
尝试将 error_sum 定义中参数的顺序更改为
def error_sum(params, row_c, row_t)
如果你想获得最佳的参数并像这样调用函数:
minimize(error_sum, x0, args=(row_c, row_t), bounds = bnds, method='nelder-mead', options={'xtol': 1e-8, 'disp': True})