SciPy 优化 - 约束函数中的参数
SciPy optimization - args in constraint function
我正在尝试构建一个投资组合优化算法,以最小化受权重边界和 return 约束的预期短缺 (CVaR)。虽然它已经在没有最低 return 要求的情况下工作,但添加 return 约束会导致以下错误:“所有输入数组必须具有相同的维数 ”。
我已经花了几个小时浏览不同的论坛和示例,但仍然不知道是什么导致了这里的错误。
感谢您的指教!
代码:
#Inputs
w_mkt = np.array([[.5203, .1439, .3358]])
mu = np.array([[.005, .003, .002]])
vol = np.array([[.02, .03, .01]])
rho = np.array([[1.00, 0.50, 0.25],
[0.50, 1.00, 0.60],
[0.25, 0.60, 1.00]])
sd_matrix = np.zeros((3,3))
np.fill_diagonal(sd_matrix, vol)
sigma = np.dot(sd_matrix, np.dot(rho, sd_matrix.T))
#Function to be optimized:
def C_VaR(w, mu, sigma, alpha=0.99):
w = np.matrix(w)
mu = np.matrix(mu)
cvar = -np.dot(mu, w.T) + sqrt(np.dot(w, np.dot(sigma, w.T)))/(1-alpha)*norm.pdf(norm.ppf(alpha))
return cvar
#Boundaries:
b_ = [(0.0, 1.0) for i in range(mu.shape[1])]
b_
#Constraints (return constraint is achivable):
c_ = ({'type':'eq', 'fun': lambda w: sum(w) - 1}, #weights sum up to zero
{'type':'eq',
'fun': lambda w, mu: np.matrix(w).dot(mu.T) - .0036,
'args': (mu,)}) #return requirement
c_
#Finally, optimization function:
minCVAR = optimize.minimize(C_VaR,
w_mkt,
args=(mu, sigma),
method="SLSQP",
bounds=tuple(b_),
constraints=c_)
错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-157-be7467bdec1d> in <module>()
4 method="SLSQP",
5 bounds=tuple(b_),
----> 6 constraints=c_)
~/miniconda3/lib/python3.6/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
609 elif meth == 'slsqp':
610 return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 611 constraints, callback=callback, **options)
612 elif meth == 'trust-constr':
613 return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp,
~/miniconda3/lib/python3.6/site-packages/scipy/optimize/slsqp.py in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, **unknown_options)
385 if cons['eq']:
386 c_eq = concatenate([atleast_1d(con['fun'](x, *con['args']))
--> 387 for con in cons['eq']])
388 else:
389 c_eq = zeros(0)
ValueError: all the input arrays must have same number of dimensions
将'sigma'添加到'c_'
的'args'
将约束更改为 'fun': lambda w, mu: (np.matrix(w).dot(mu.T) - .0036)[0,0]
解决了问题。
我正在尝试构建一个投资组合优化算法,以最小化受权重边界和 return 约束的预期短缺 (CVaR)。虽然它已经在没有最低 return 要求的情况下工作,但添加 return 约束会导致以下错误:“所有输入数组必须具有相同的维数 ”。
我已经花了几个小时浏览不同的论坛和示例,但仍然不知道是什么导致了这里的错误。
感谢您的指教!
代码:
#Inputs
w_mkt = np.array([[.5203, .1439, .3358]])
mu = np.array([[.005, .003, .002]])
vol = np.array([[.02, .03, .01]])
rho = np.array([[1.00, 0.50, 0.25],
[0.50, 1.00, 0.60],
[0.25, 0.60, 1.00]])
sd_matrix = np.zeros((3,3))
np.fill_diagonal(sd_matrix, vol)
sigma = np.dot(sd_matrix, np.dot(rho, sd_matrix.T))
#Function to be optimized:
def C_VaR(w, mu, sigma, alpha=0.99):
w = np.matrix(w)
mu = np.matrix(mu)
cvar = -np.dot(mu, w.T) + sqrt(np.dot(w, np.dot(sigma, w.T)))/(1-alpha)*norm.pdf(norm.ppf(alpha))
return cvar
#Boundaries:
b_ = [(0.0, 1.0) for i in range(mu.shape[1])]
b_
#Constraints (return constraint is achivable):
c_ = ({'type':'eq', 'fun': lambda w: sum(w) - 1}, #weights sum up to zero
{'type':'eq',
'fun': lambda w, mu: np.matrix(w).dot(mu.T) - .0036,
'args': (mu,)}) #return requirement
c_
#Finally, optimization function:
minCVAR = optimize.minimize(C_VaR,
w_mkt,
args=(mu, sigma),
method="SLSQP",
bounds=tuple(b_),
constraints=c_)
错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-157-be7467bdec1d> in <module>()
4 method="SLSQP",
5 bounds=tuple(b_),
----> 6 constraints=c_)
~/miniconda3/lib/python3.6/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
609 elif meth == 'slsqp':
610 return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 611 constraints, callback=callback, **options)
612 elif meth == 'trust-constr':
613 return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp,
~/miniconda3/lib/python3.6/site-packages/scipy/optimize/slsqp.py in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, **unknown_options)
385 if cons['eq']:
386 c_eq = concatenate([atleast_1d(con['fun'](x, *con['args']))
--> 387 for con in cons['eq']])
388 else:
389 c_eq = zeros(0)
ValueError: all the input arrays must have same number of dimensions
将'sigma'添加到'c_'
的'args'将约束更改为 'fun': lambda w, mu: (np.matrix(w).dot(mu.T) - .0036)[0,0]
解决了问题。