我正在尝试构建一个投资组合优化算法,该算法在权重边界和收益约束的约束下,将预期缺口(CVaR)降至最低。尽管它已经可以满足最小返回要求,但添加返回约束会导致以下错误:“ 所有输入数组的维数必须相同”。
我现在已经花了几个小时在不同的论坛和示例中进行搜索,但仍然不知道是什么原因导致了错误。
谢谢您的建议!
代码:
#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
答案 0 :(得分:0)
更改约束'fun': lambda w, mu: (np.matrix(w).dot(mu.T) - .0036)[0,0]
解决了该问题。
答案 1 :(得分:-1)
在'c_'的'args'中添加'sigma'