繁体   English   中英

SciPy优化-约束函数中的参数

[英]SciPy optimization - args in constraint function

我正在尝试构建一个投资组合优化算法,该算法在权重边界和收益约束的约束下,将预期缺口(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

将约束更改为'fun': lambda w, mu: (np.matrix(w).dot(mu.T) - .0036)[0,0]解决了该问题。

将“ sigma”添加到“ c_”的“ args”中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM