import numpy as np import pandas as pd import matplotlib.pyplot as plt cqlq=pd.read_csv("cqlq.txt",sep="\s+",encoding="gbk") dxnt=pd.read_csv("dxnt.txt",sep="\s+",encoding="gbk") ggdq=pd.read_csv("ggdq.txt",sep="\s+",encoding="gbk") giyy=pd.read_csv("gjyy.txt",sep="\s+",encoding="gbk") cqlq.columns = ["date","oppr","hipr","lopr","clpr","TR"] dxnt.columns = ["date","oppr","hipr","lopr","clpr"] ggdq.columns = ["date","oppr","hipr","lopr","clpr","TR"] giyy.columns = ["date","oppr","hipr","lopr","clpr","TR"] a=cqlq b=dxnt c=ggdq d=giyy ua=(a["clpr"]-a["clpr"].shift(1))/a["clpr"] ub=(b["clpr"]-b["clpr"].shift(1))/b["clpr"] uc=(c["clpr"]-c["clpr"].shift(1))/c["clpr"] ud=(d["clpr"]-d["clpr"].shift(1))/d["clpr"] u=pd.concat([ua,ub,uc,ud],axis=1) u.dropna() miu=u.mean()+0.005 jz=u.cov() yi = np.ones(4) miu= np.mat(miu) jz = np.mat(jz) yi = np.mat(yi) nijz = jz.I a = miu*nijz*miu.T b =yi*nijz*miu.T c = yi*nijz*yi.T deta=a*c-b**2 stock_y=[i*0.0001 for i in range(100)] stock_x=[(np.sqrt(( c/deta)*(rp-b/c)**2+1/c)).max() for rp in stock_y] plt.rcParams['font.sans-serif']=['SimHei'] plt.plot(stock_x,stock_y) plt.xlabel("方差") plt.ylabel("期望") print(miu) print(jz) plt.show()
2020-05-26