Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

jidt.py 4.2 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # # jidt 基本使用教程
  4. # In[1]:
  5. get_ipython().run_line_magic('reload_ext', 'autoreload')
  6. get_ipython().run_line_magic('autoreload', '2')
  7. get_ipython().run_line_magic('matplotlib', 'inline')
  8. # 导入基本库
  9. # In[2]:
  10. import os, sys
  11. import numpy as np
  12. import pandas as pd
  13. import matplotlib.pyplot as plt
  14. from pathlib import Path
  15. Path.ls = lambda x: list(x.iterdir())
  16. from tqdm import tqdm
  17. # 打开java虚拟机
  18. # In[3]:
  19. import jpype
  20. from jpype import *
  21. try :
  22. jarLocation = "/home/fsf/software/inforDynamics-dist-1.5/infodynamics.jar"
  23. jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation)
  24. except:
  25. print("JVM has already started !")
  26. # 添加jidt源码路径
  27. # In[4]:
  28. sys.path.append('../../srcs')
  29. # ## 加载数据
  30. # In[5]:
  31. from jidt.data import example1, example2
  32. # In[6]:
  33. x1,y1 = example1(length=3600,delay=10)
  34. x2,y2 = example2(length=3600,noise_level=[0.1,0.15])
  35. # In[7]:
  36. fig, axs = plt.subplots(2,1,figsize=(12,3))
  37. axs[0].plot(x1-20,label='x',color='r')
  38. axs[0].plot(y1,label='y',color='b')
  39. axs[0].legend()
  40. axs[1].plot(x2,label='sin',color='blue')
  41. axs[1].plot(y2,label='cos',color='red')
  42. axs[1].legend()
  43. plt.tight_layout()
  44. plt.show()
  45. # ## 计算转移熵
  46. # In[8]:
  47. from jidt.TransferEntropyCalculatorBinned import TransferEntropyCalculatorBinned
  48. from jidt.TransferEntropyCalculatorGaussian import TransferEntropyCalculatorGaussian
  49. from jidt.TransferEntropyCalculatorKraskov import TransferEntropyCalculatorKraskov
  50. from jidt.TransferEntropyCalculatorKernel import TransferEntropyCalculatorKernel
  51. # In[9]:
  52. estimator_ksg = TransferEntropyCalculatorKraskov(ALG_NUM=2)
  53. estimator_bin = TransferEntropyCalculatorBinned(base=2)
  54. estimator_gauss = TransferEntropyCalculatorGaussian()
  55. estimator_kernel = TransferEntropyCalculatorKernel()
  56. # In[10]:
  57. estimator_ksg(x1,y1,tau=3), estimator_bin(x1,y1,tau=1), estimator_gauss(x1,y1,tau=1), estimator_kernel(x1,y1,tau=1)
  58. # 计算重要性
  59. # In[11]:
  60. test_sig = TransferEntropyCalculatorKraskov(cal_sig=True,sig_num=10)
  61. # In[12]:
  62. test_sig(x1,y1,tau=10)
  63. # In[13]:
  64. value, [mean, std, pvalue] = test_sig(x1,y1,tau=1)
  65. # 这里的pvalue应该是越小越说明计算得到的转移熵比较合理,因为假设的是他们之间不存在转移熵,或者说是`null hypothesis`,
  66. #
  67. # p-value的定义:P值就是当原假设为真时所得到的样本观察结果或更极端结果出现的概率。
  68. #
  69. # 如果p值越小,说明原假设情况的发生的概率很小,而如果出现了,根据小概率原理,我们就有理由拒绝原假设,P值越小,我们拒绝原假设的理由约充分。
  70. #
  71. # 总之,P值越小,表明结果越显著。小的pvalue说明一件事,不是小概率事件发生了,就是你的原假设时错误的。
  72. # 比较ksg算法的两个不同形式:
  73. # In[14]:
  74. es1 = TransferEntropyCalculatorKraskov(ALG_NUM=1)
  75. es2 = TransferEntropyCalculatorKraskov(ALG_NUM=2)
  76. # In[15]:
  77. es1(x1,y1,10),es2(x1,y1,10)
  78. # In[16]:
  79. fig, ax = plt.subplots(1,1,figsize=(6,2))
  80. TE_XY = []
  81. TE_YX = []
  82. taus = list(range(1,101))
  83. for tau in tqdm(taus):
  84. TE_XY.append(estimator_ksg(x1,y1,tau))
  85. TE_YX.append(estimator_ksg(y1,x1,tau))
  86. ax.plot(taus, TE_XY, label='X->Y', color='blue')
  87. ax.plot(taus, TE_YX, label='Y->X', color='red')
  88. ax.set_xlabel(r'Time Delay : $\tau$')
  89. ax.set_ylabel('Transfer Entropy')
  90. ax.legend()
  91. plt.show()
  92. # In[17]:
  93. fig, ax = plt.subplots(1,1,figsize=(6,2))
  94. TE_XY = []
  95. TE_YX = []
  96. taus = list(range(1,361))
  97. for tau in tqdm(taus):
  98. TE_XY.append(estimator_ksg(x2,y2,tau))
  99. TE_YX.append(estimator_ksg(y2,x2,tau))
  100. ax.plot(taus, TE_XY, label='X->Y', color='blue')
  101. ax.plot(taus, TE_YX, label='Y->X', color='red')
  102. ax.set_xlabel(r'Time Delay : $\tau$')
  103. ax.set_ylabel('Transfer Entropy')
  104. ax.legend()
  105. plt.show()
  106. # In[18]:
  107. fig, ax = plt.subplots(1,1,figsize=(6,2))
  108. TE_XY = []
  109. TE_YX = []
  110. taus = list(range(1,361))
  111. for tau in tqdm(taus):
  112. TE_XY.append(estimator_bin(x2,y2,tau))
  113. TE_YX.append(estimator_bin(y2,x2,tau))
  114. ax.plot(taus, TE_XY, label='X->Y', color='blue')
  115. ax.plot(taus, TE_YX, label='Y->X', color='red')
  116. ax.set_xlabel(r'Time Delay : $\tau$')
  117. ax.set_ylabel('Transfer Entropy')
  118. ax.legend()
  119. plt.show()
  120. # 不同estimator得到的结果并不一样
  121. # ## 计算互信息
  122. #
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...