做大量的随机资金权重相应的收益率与方差值
zfset=['rk','sy','hb','sd']
noa=len(zfset)
weights=np.random.random(noa)
weights/=np.sum(weights)
port_zfr=[]
port_variance=[]
for p in range(10000):
weights=np.random.random(noa)
weights/=np.sum(weights)
port_zfr.append(np.sum(zfr.mean()*252*weights))
port_variance.append(np.sqrt(np.dot(weights.T, np.dot(zfr.cov()*252, weights))))
port_zfr=np.array(port_zfr)
port_variance=np.array(port_variance)
risk_free = 0.04
plt.figure(figsize=(8,4))
plt.scatter(port_variance, port_zfr, c=(port_zfr-risk_free)/port_variance, marker='o')
plt.grid(True)
plt.xlabel('excepted volatility')
plt.ylabel('expected return')
plt.colorbar(label='Sharpe ratio')