[英]Scipy.optimize minimize 'inequality constraints incompatible'
我试图将 100 种证券的投资组合的差异最小化。
def portvol(w, x):
return np.dot(w.T, np.dot(x, w))*252
covmat = annreturn.cov()
w0 = np.ones(len(covmat)) * (1 / len(covmat)) #equal weighting initially
bounds = ((0,1),) * len(covmat)
constraints = {'fun': lambda i: np.sum(i)-1.0, 'type': 'eq'}
optweights = minimize(portvol, w0, args = (covmat), method = 'SLSQP', bounds = bounds, constraints =
constraints)
annereturn.cov() 是一个 100x100 DataFrame。 output 是相同的。01 甚至我开始使用的权重和此失败消息:
message: 'Inequality constraints incompatible'
nfev: 102
nit: 1
njev: 1
状态:4 成功:假
这就是我计算年化回报的方式...
annreturn = data.pct_change() #again, assuming percentage change
annreturn = annreturn.iloc[1:]
annreturn = (annreturn+1)**252-1
如果你没有注意到任何事情,没关系。 我花了 2 天的时间才意识到我没有将 PCT_CHANGE() 结果除以 100。时间花的很值。 我得到了与 15+ 的幂的相关性。 这是最后一行的样子,原始问题中的最小化 function 工作正常。
annreturn = (annreturn/100+1)**252-1
抱歉,如果有人在没有上述内容的情况下花时间做这件事!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.