Python 做大量组合的均值方差模型

 时间:2026-02-14 12:21:57

1、导入相关包和模块

Python 做大量组合的均值方差模型

2、读取数据  zf=pd.read_csv('zf.csv',index_col='date')

并查看前5项  zf,head()

Python 做大量组合的均值方差模型

3、对缺失值的删除操作  zf=zf.dropna() 

对数据的时间序列操作   zf.index=pd.to_datetime(zf.index)

Python 做大量组合的均值方差模型

4、连续收益率    zfr=np.log(zf/zf.shift(1))  

累积收益率分布图    (1+zfr).cumprod().plot(),plt.title('ljrate')

Python 做大量组合的均值方差模型

Python 做大量组合的均值方差模型

5、做大量的随机资金权重相应的收益率与方差值

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')

Python 做大量组合的均值方差模型

Python 做大量组合的均值方差模型

  • 单机版王者荣耀怎么玩
  • 如何利用SPSSAU进行季节Sarima?
  • 中药浸膏在烤干时溢出如何解决
  • state怎么导入日期
  • CAD图纸中添加图形标注的方法
  • 热门搜索
    如何预防艾滋病 嘉陵摩托车怎么样 狗翻肠子怎么办 公司倒闭员工怎么赔偿 如何共享打印机 苏州东山旅游攻略 玫瑰花泡水喝的功效 如何提高自信心 如何去腿毛 手机怎么做系统