1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| def min_variance(weights): return statistics(weights)[1]
target_returns = np.linspace(0.0,0.5,50) target_variance = [] for tar in target_returns: cons = ({'type':'eq','fun':lambda x:statistics(x)[0]-tar},{'type':'eq','fun':lambda x:np.sum(x)-1}) res = sco.minimize(min_variance, noa*[1./noa,],method = 'SLSQP', bounds = bnds, constraints = cons) target_variance.append(res['fun'])
target_variance = np.array(target_variance)
plt.figure(figsize = (8,4))
plt.scatter(port_variance, port_returns, c = port_returns/port_variance,marker = 'o')
plt.scatter(target_variance,target_returns, c = target_returns/target_variance, marker = '+',s=200)
plt.plot(statistics(opts['x'])[1], statistics(opts['x'])[0], 'r*', markersize = 15.0) plt.grid(True) plt.xlabel('预期标准差') plt.ylabel('预期组合收益率') plt.colorbar(label = '夏普率')
|