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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
- #!/usr/bin/env python
- # coding: utf-8
- # # jidt 基本使用教程
- # In[1]:
- get_ipython().run_line_magic('reload_ext', 'autoreload')
- get_ipython().run_line_magic('autoreload', '2')
- get_ipython().run_line_magic('matplotlib', 'inline')
- # 导入基本库
- # In[2]:
- import os, sys
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- from pathlib import Path
- Path.ls = lambda x: list(x.iterdir())
- from tqdm import tqdm
- # 打开java虚拟机
- # In[3]:
- import jpype
- from jpype import *
- try :
- jarLocation = "/home/fsf/software/inforDynamics-dist-1.5/infodynamics.jar"
- jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation)
- except:
- print("JVM has already started !")
- # 添加jidt源码路径
- # In[4]:
- sys.path.append('../../srcs')
- # ## 加载数据
- # In[5]:
- from jidt.data import example1, example2
- # In[6]:
- x1,y1 = example1(length=3600,delay=10)
- x2,y2 = example2(length=3600,noise_level=[0.1,0.15])
- # In[7]:
- fig, axs = plt.subplots(2,1,figsize=(12,3))
- axs[0].plot(x1-20,label='x',color='r')
- axs[0].plot(y1,label='y',color='b')
- axs[0].legend()
- axs[1].plot(x2,label='sin',color='blue')
- axs[1].plot(y2,label='cos',color='red')
- axs[1].legend()
- plt.tight_layout()
- plt.show()
- # ## 计算转移熵
- # In[8]:
- from jidt.TransferEntropyCalculatorBinned import TransferEntropyCalculatorBinned
- from jidt.TransferEntropyCalculatorGaussian import TransferEntropyCalculatorGaussian
- from jidt.TransferEntropyCalculatorKraskov import TransferEntropyCalculatorKraskov
- from jidt.TransferEntropyCalculatorKernel import TransferEntropyCalculatorKernel
- # In[9]:
- estimator_ksg = TransferEntropyCalculatorKraskov(ALG_NUM=2)
- estimator_bin = TransferEntropyCalculatorBinned(base=2)
- estimator_gauss = TransferEntropyCalculatorGaussian()
- estimator_kernel = TransferEntropyCalculatorKernel()
- # In[10]:
- estimator_ksg(x1,y1,tau=3), estimator_bin(x1,y1,tau=1), estimator_gauss(x1,y1,tau=1), estimator_kernel(x1,y1,tau=1)
- # 计算重要性
- # In[11]:
- test_sig = TransferEntropyCalculatorKraskov(cal_sig=True,sig_num=10)
- # In[12]:
- test_sig(x1,y1,tau=10)
- # In[13]:
- value, [mean, std, pvalue] = test_sig(x1,y1,tau=1)
- # 这里的pvalue应该是越小越说明计算得到的转移熵比较合理,因为假设的是他们之间不存在转移熵,或者说是`null hypothesis`,
- #
- # p-value的定义:P值就是当原假设为真时所得到的样本观察结果或更极端结果出现的概率。
- #
- # 如果p值越小,说明原假设情况的发生的概率很小,而如果出现了,根据小概率原理,我们就有理由拒绝原假设,P值越小,我们拒绝原假设的理由约充分。
- #
- # 总之,P值越小,表明结果越显著。小的pvalue说明一件事,不是小概率事件发生了,就是你的原假设时错误的。
- # 比较ksg算法的两个不同形式:
- # In[14]:
- es1 = TransferEntropyCalculatorKraskov(ALG_NUM=1)
- es2 = TransferEntropyCalculatorKraskov(ALG_NUM=2)
- # In[15]:
- es1(x1,y1,10),es2(x1,y1,10)
- # In[16]:
- fig, ax = plt.subplots(1,1,figsize=(6,2))
- TE_XY = []
- TE_YX = []
- taus = list(range(1,101))
- for tau in tqdm(taus):
- TE_XY.append(estimator_ksg(x1,y1,tau))
- TE_YX.append(estimator_ksg(y1,x1,tau))
- ax.plot(taus, TE_XY, label='X->Y', color='blue')
- ax.plot(taus, TE_YX, label='Y->X', color='red')
- ax.set_xlabel(r'Time Delay : $\tau$')
- ax.set_ylabel('Transfer Entropy')
- ax.legend()
- plt.show()
- # In[17]:
- fig, ax = plt.subplots(1,1,figsize=(6,2))
- TE_XY = []
- TE_YX = []
- taus = list(range(1,361))
- for tau in tqdm(taus):
- TE_XY.append(estimator_ksg(x2,y2,tau))
- TE_YX.append(estimator_ksg(y2,x2,tau))
- ax.plot(taus, TE_XY, label='X->Y', color='blue')
- ax.plot(taus, TE_YX, label='Y->X', color='red')
- ax.set_xlabel(r'Time Delay : $\tau$')
- ax.set_ylabel('Transfer Entropy')
- ax.legend()
- plt.show()
- # In[18]:
- fig, ax = plt.subplots(1,1,figsize=(6,2))
- TE_XY = []
- TE_YX = []
- taus = list(range(1,361))
- for tau in tqdm(taus):
- TE_XY.append(estimator_bin(x2,y2,tau))
- TE_YX.append(estimator_bin(y2,x2,tau))
- ax.plot(taus, TE_XY, label='X->Y', color='blue')
- ax.plot(taus, TE_YX, label='Y->X', color='red')
- ax.set_xlabel(r'Time Delay : $\tau$')
- ax.set_ylabel('Transfer Entropy')
- ax.legend()
- plt.show()
- # 不同estimator得到的结果并不一样
- # ## 计算互信息
- #
|