- 积分
- 224
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2017-9-10
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
- import numpy as np
- from math import sqrt
- import matplotlib.pyplot as plt
- #求落后交叉相关系数
- zry=[]
- zrw=[]
- for j in range(1,10):
- def s(a,b):#协方差
- avofa=0.0
- avofb=0.0
- sumofab=0.0
- for i in range(len(a)):
- avofa+=a[i]/20.
- for i in range(len(b)):
- avofb+=b[i]/20.
- for i in range(len(a)-j):
- temp=(a[i]-avofa)*(b[i+j]-avofb)
- sumofab+=temp/(len(a)-j)
- return sumofab
- def variance(a):#平方的期望-期望的平方
- s1=0;
- s2=0;
- for i in a:
- s1+=i**2;
- s2+=i;
- return float(s1)/len(a)-(float(s2)/len(a))**2
- def r(a,b):#落后交叉相关系数
- return s(a,b)/sqrt((variance(a)*variance(b)))
- f1=open('F:\\shixi3\\shixi312.txt')
- data1=f1.readlines()
- f1.close()
- f2=open('F:\\shixi3\\shixi31.txt')
- data2=f2.readlines()
- f2.close()
- f3=open('F:\\shixi3\\shixi32.txt')
- data3=f3.readlines()
- f3.close()
- ave12=[]
- ave1=[]
- ave2=[]
- for i in range(0,20):
- temp1=data1[i]
- temp2=data2[i]
- temp3=data3[i]
- ave12.append(float(temp1.split()[0]))
- ave1.append(float(temp2.split()[0]))
- ave2.append(float(temp3.split()[0]))
- # print 'j=',j
- # print r(yearave),',',r(winterave)
- zry.append(float(r(ave12,ave1)))
- zrw.append(float(r(ave12,ave2)))
- #print zry,zrw
- x=np.linspace(1,9,9)
- y=np.linspace(-0.4,0.4,9)
- cp=plt.plot(x,zry,marker='.',c='black',markersize=10,label='Month12 and month1')
- cp=plt.plot(x,zrw,marker=6,c='gray',markersize=10,label='Month12 and month2')
- for i in range(0,len(zry)):
- plt.text(x[i],zry[i]+0.005,'%.02f' %zry[i],ha='center',va='bottom',color='black',fontsize=9)
- for i in range(0,len(zrw)):
- plt.text(x[i],zrw[i]+0.005,'%.02f' %zrw[i],ha='center',va='bottom',color='gray',fontsize=9)
- plt.title('Backwardness cross correlation coefficient with different lag length',fontsize=10)
- plt.xlabel('Lag Length',fontsize=10)
- plt.ylabel('Backwardness cross correlation coefficient',fontsize=10)
- plt.legend()
- plt.show()
复制代码
|
|