我正在使用从quantopian博客中提取的python代码,以便找到优化的产品组合。
我已经完成了详细的整个练习:https://blog.quantopian.com/markowitz-portfolio-optimization-2/
在Optimal_portfolio函数的最后一个等式之前,一切都像魅力一样:
wt = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x']
我得到一个超级凌乱的错误:
\ Software \ Anaconda2 \ envs \ gl-env \ lib \ site-packages \ cvxopt-1.1.8-py2.7-win-amd64.egg \ cvxopt \ coneprog.py qp(P, q, G, h, A, b, solver, initvals, **kwargs)
4466 'residual as dual infeasibility certificate': dinfres}
4467
-> 4468 return coneqp(P, q, G, h, None, A, b, initvals, options = options)
< / p>
\ Software \ Anaconda2 \ envs \ gl-env \ lib \ site-packages \ cvxopt-1.1.8-py2.7-win-amd64.egg \ cvxopt \ coneprog.py coneqp(P, q, G, h, dims, A, b, initvals, kktsolver, xnewcopy, xdot, xaxpy, xscal, ynewcopy, ydot, yaxpy, yscal, **kwargs)
2241 # lmbdasq = lambda o lambda.
2242
-> 2243 if iters == 0: W = misc.compute_scaling(s, z, lmbda, dims)
2244 misc.ssqr(lmbdasq, lmbda, dims)
2245
< / p>
\ Software \ Anaconda2 \ envs \ gl-env \ lib \ site-packages \ cvxopt-1.1.8-py2.7-win-amd64.egg \ cvxopt \ misc.py compute_scaling(s, z, lmbda, dims, mnl)
283
284 m = dims['l']
--> 285 W['d'] = base.sqrt( base.div( s[mnl:mnl+m], z[mnl:mnl+m] ))
286 W['di'] = W['d']**-1
287 lmbda[mnl:mnl+m] = base.sqrt( base.mul( s[mnl:mnl+m], z[mnl:mnl+m] ) )
< / p>
ValueError:域错误
似乎我可能试图取负数的sqrt。关于如何解决这个问题的任何想法?