我正在尝试执行投资组合优化,返回使我的效用函数最大化的权重。我可以很好地完成这一部分,包括权重总和为 1 的约束,并且权重也给我一个目标风险。我还包括了 [0 <= weights <= 1] 的边界。此代码如下所示:
def rebalance(PortValue, port_rets, risk_tgt):
#convert continuously compounded returns to simple returns
Rt = np.exp(port_rets) - 1
covar = Rt.cov()
def fitness(W):
port_Rt = np.dot(Rt, W)
port_rt = np.log(1 + port_Rt)
q95 = Series(port_rt).quantile(.05)
cVaR = (port_rt[port_rt < q95] * sqrt(20)).mean() * PortValue
mean_cVaR = (PortValue * (port_rt.mean() * 20)) / cVaR
return -1 * mean_cVaR
def solve_weights(W):
import scipy.optimize as opt
b_ = [(0.0, 1.0) for i in Rt.columns]
c_ = …Run Code Online (Sandbox Code Playgroud)