滑动t检验

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
'''
滑动t检验
输入:
inputdata:输入数据,一维序列
step:滑动步长
输出:t1:t统计量
注:自由度v =2*n-2
'''
def slide_t(inputdata,step):
inputdata = np.array(inputdata)
n = inputdata.shape[0]
t = np.zeros(n)
t1 = np.empty(n)
n1 = step #n1, n2为滑动步长,需调整
n2 = step
n11 = 1 / n1
n22 = 1 / n2
m = np.sqrt(n11 + n22)
for i in range (step, n-step+1):
x1_mean = np.mean(inputdata[i-step : i])
x2_mean = np.mean(inputdata[i : i+step])
s1 = np.var(inputdata[i-step : i])
s2 = np.var(inputdata[i : i+step])
s = np.sqrt((n1 * s1 + n2 * s2) / (n1 + n2 - 2))
t[i-step] = (x2_mean - x1_mean) / (s * m)
t1 = np.roll(t , step-1)
t1[:step]=np.nan
t1[n-step+1:]=np.nan
return t1
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
from scipy import signal
import numpy as np

y = np.array([15.4,14.6,15.8,14.8,15.0,15.1,15.1,15.0,15.2,15.4,
14.8,15.0,15.1,14.7,16.0,15.7,15.4,14.5,15.1,15.3,
15.5,15.1,15.6,15.1,15.1,14.9,15.5,15.3,15.3,15.4,
15.7,15.2,15.5,15.5,15.6,15.1,15.1,16.0,16.0,16.8,
16.2,16.2,16.0,15.6,15.9,16.2,16.7,15.8,16.2,15.9,
15.8,15.5,15.9,16.8,15.5,15.8,15.0,14.9,15.3,16.0,
16.1,16.5,15.5,15.6,16.1,15.6,16.0,15.4,15.5,15.2,
15.4,15.6,15.1,15.8,15.5,16.0,15.2,15.8,16.2,16.2,
15.2,15.7,16.0,16.0,15.7,15.9,15.7,16.7,15.3,16.1])

t = slide_t(y,10)

fig = plt.figure(figsize=(15,15))
f_ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.3])
f_ax1.plot(np.arange(1900,1990,1),y,'k')

f_ax2 = fig.add_axes([0.6, 0.1, 0.4, 0.3])
f_ax2.plot(np.arange(1900,1990,1),t,'k')
f_ax2.set_xlim(1900,1990)
f_ax2.set_ylim(-4,7)
# 0.01显著性检验
f_ax2.axhline(2.9)
f_ax2.axhline(-2.9)
plt.show()

image-20200521090717453