jidt 基本使用教程¶
%reload_ext autoreload
%autoreload 2
%matplotlib inline
导入基本库
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虚拟机
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源码路径
sys.path.append('../../srcs')
加载数据¶
from jidt.data import example1, example2
x1,y1 = example1(length=3600,delay=10)
x2,y2 = example2(length=3600,noise_level=[0.1,0.15])
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()

计算转移熵¶
from jidt.TransferEntropyCalculatorBinned import TransferEntropyCalculatorBinned
from jidt.TransferEntropyCalculatorGaussian import TransferEntropyCalculatorGaussian
from jidt.TransferEntropyCalculatorKraskov import TransferEntropyCalculatorKraskov
from jidt.TransferEntropyCalculatorKernel import TransferEntropyCalculatorKernel
estimator_ksg = TransferEntropyCalculatorKraskov(ALG_NUM=2)
estimator_bin = TransferEntropyCalculatorBinned(base=2)
estimator_gauss = TransferEntropyCalculatorGaussian()
estimator_kernel = TransferEntropyCalculatorKernel()
estimator_ksg(x1,y1,tau=3), estimator_bin(x1,y1,tau=1), estimator_gauss(x1,y1,tau=1), estimator_kernel(x1,y1,tau=1)
(0.013802834188919255,
0.00018504445103475052,
1.872447415741496e-05,
0.11448805061038238)
计算重要性
test_sig = TransferEntropyCalculatorKraskov(cal_sig=True,sig_num=10)
test_sig(x1,y1,tau=10)
(0.9200227073687028, [(2.474076067793263e-07,), (0.006423865723227758,), 0.0])
value, [mean, std, pvalue] = test_sig(x1,y1,tau=1)
这里的pvalue应该是越小越说明计算得到的转移熵比较合理,因为假设的是他们之间不存在转移熵,或者说是null hypothesis
,
p-value的定义:P值就是当原假设为真时所得到的样本观察结果或更极端结果出现的概率。
如果p值越小,说明原假设情况的发生的概率很小,而如果出现了,根据小概率原理,我们就有理由拒绝原假设,P值越小,我们拒绝原假设的理由约充分。
总之,P值越小,表明结果越显著。小的pvalue说明一件事,不是小概率事件发生了,就是你的原假设时错误的。
比较ksg算法的两个不同形式:
es1 = TransferEntropyCalculatorKraskov(ALG_NUM=1)
es2 = TransferEntropyCalculatorKraskov(ALG_NUM=2)
es1(x1,y1,10),es2(x1,y1,10)
(0.9200227073687028, 1.0449864550570729)
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()
0%| | 0/100 [00:00<?, ?it/s]
1%| | 1/100 [00:00<00:18, 5.47it/s]
3%|▎ | 3/100 [00:00<00:13, 6.96it/s]
6%|▌ | 6/100 [00:00<00:10, 8.70it/s]
9%|▉ | 9/100 [00:00<00:08, 10.63it/s]
12%|█▏ | 12/100 [00:00<00:06, 12.60it/s]
15%|█▌ | 15/100 [00:00<00:05, 14.31it/s]
18%|█▊ | 18/100 [00:00<00:05, 15.99it/s]
21%|██ | 21/100 [00:01<00:04, 17.34it/s]
24%|██▍ | 24/100 [00:01<00:04, 18.45it/s]
27%|██▋ | 27/100 [00:01<00:03, 19.52it/s]
30%|███ | 30/100 [00:01<00:03, 20.18it/s]
33%|███▎ | 33/100 [00:01<00:03, 20.75it/s]
36%|███▌ | 36/100 [00:01<00:03, 21.25it/s]
39%|███▉ | 39/100 [00:01<00:02, 21.84it/s]
42%|████▏ | 42/100 [00:02<00:02, 21.67it/s]
45%|████▌ | 45/100 [00:02<00:02, 22.03it/s]
48%|████▊ | 48/100 [00:02<00:02, 22.22it/s]
51%|█████ | 51/100 [00:02<00:02, 22.28it/s]
54%|█████▍ | 54/100 [00:02<00:02, 22.73it/s]
57%|█████▋ | 57/100 [00:02<00:01, 22.52it/s]
60%|██████ | 60/100 [00:02<00:01, 22.26it/s]
63%|██████▎ | 63/100 [00:02<00:01, 22.54it/s]
66%|██████▌ | 66/100 [00:03<00:01, 22.57it/s]
69%|██████▉ | 69/100 [00:03<00:01, 22.42it/s]
72%|███████▏ | 72/100 [00:03<00:01, 22.53it/s]
75%|███████▌ | 75/100 [00:03<00:01, 22.36it/s]
78%|███████▊ | 78/100 [00:03<00:00, 22.48it/s]
81%|████████ | 81/100 [00:03<00:00, 22.69it/s]
84%|████████▍ | 84/100 [00:03<00:00, 22.63it/s]
87%|████████▋ | 87/100 [00:04<00:00, 22.58it/s]
90%|█████████ | 90/100 [00:04<00:00, 22.59it/s]
93%|█████████▎| 93/100 [00:04<00:00, 22.54it/s]
96%|█████████▌| 96/100 [00:04<00:00, 22.31it/s]
99%|█████████▉| 99/100 [00:04<00:00, 22.26it/s]
100%|██████████| 100/100 [00:04<00:00, 21.53it/s]

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()
0%| | 0/360 [00:00<?, ?it/s]
1%| | 3/360 [00:00<00:14, 24.90it/s]
2%|▏ | 6/360 [00:00<00:14, 24.52it/s]
2%|▎ | 9/360 [00:00<00:14, 24.77it/s]
3%|▎ | 12/360 [00:00<00:13, 25.68it/s]
4%|▍ | 15/360 [00:00<00:13, 25.98it/s]
5%|▌ | 18/360 [00:00<00:13, 26.30it/s]
6%|▌ | 21/360 [00:00<00:12, 26.55it/s]
7%|▋ | 24/360 [00:00<00:12, 26.81it/s]
8%|▊ | 27/360 [00:01<00:12, 27.19it/s]
8%|▊ | 30/360 [00:01<00:12, 27.02it/s]
9%|▉ | 33/360 [00:01<00:11, 27.46it/s]
10%|█ | 36/360 [00:01<00:11, 27.49it/s]
11%|█ | 39/360 [00:01<00:11, 27.28it/s]
12%|█▏ | 42/360 [00:01<00:11, 27.34it/s]
12%|█▎ | 45/360 [00:01<00:11, 26.83it/s]
13%|█▎ | 48/360 [00:01<00:11, 26.86it/s]
14%|█▍ | 51/360 [00:01<00:11, 27.41it/s]
15%|█▌ | 54/360 [00:02<00:11, 27.27it/s]
16%|█▌ | 57/360 [00:02<00:11, 27.39it/s]
17%|█▋ | 60/360 [00:02<00:10, 27.50it/s]
18%|█▊ | 63/360 [00:02<00:11, 26.61it/s]
18%|█▊ | 66/360 [00:02<00:10, 27.27it/s]
19%|█▉ | 69/360 [00:02<00:10, 27.26it/s]
20%|██ | 72/360 [00:02<00:10, 26.86it/s]
21%|██ | 75/360 [00:02<00:10, 27.11it/s]
22%|██▏ | 78/360 [00:02<00:11, 25.23it/s]
22%|██▎ | 81/360 [00:03<00:11, 25.33it/s]
23%|██▎ | 84/360 [00:03<00:10, 26.00it/s]
24%|██▍ | 87/360 [00:03<00:10, 26.62it/s]
25%|██▌ | 90/360 [00:03<00:10, 26.85it/s]
26%|██▌ | 93/360 [00:03<00:09, 27.46it/s]
27%|██▋ | 96/360 [00:03<00:09, 27.47it/s]
28%|██▊ | 99/360 [00:03<00:09, 27.00it/s]
28%|██▊ | 102/360 [00:03<00:09, 27.66it/s]
29%|██▉ | 105/360 [00:03<00:09, 27.54it/s]
30%|███ | 108/360 [00:04<00:09, 27.60it/s]
31%|███ | 111/360 [00:04<00:08, 27.67it/s]
32%|███▏ | 114/360 [00:04<00:08, 27.35it/s]
32%|███▎ | 117/360 [00:04<00:08, 27.60it/s]
33%|███▎ | 120/360 [00:04<00:08, 27.91it/s]
34%|███▍ | 123/360 [00:04<00:08, 27.86it/s]
35%|███▌ | 126/360 [00:04<00:08, 26.86it/s]
36%|███▌ | 129/360 [00:04<00:09, 25.47it/s]
37%|███▋ | 132/360 [00:04<00:08, 25.44it/s]
38%|███▊ | 135/360 [00:05<00:08, 26.25it/s]
38%|███▊ | 138/360 [00:05<00:08, 26.54it/s]
39%|███▉ | 141/360 [00:05<00:08, 25.09it/s]
40%|████ | 144/360 [00:05<00:08, 24.49it/s]
41%|████ | 147/360 [00:05<00:08, 25.60it/s]
42%|████▏ | 150/360 [00:05<00:08, 25.70it/s]
42%|████▎ | 153/360 [00:05<00:07, 26.60it/s]
43%|████▎ | 156/360 [00:05<00:07, 27.04it/s]
44%|████▍ | 159/360 [00:05<00:07, 27.06it/s]
45%|████▌ | 162/360 [00:06<00:07, 27.07it/s]
46%|████▌ | 165/360 [00:06<00:07, 27.60it/s]
47%|████▋ | 168/360 [00:06<00:06, 27.57it/s]
48%|████▊ | 171/360 [00:06<00:06, 27.85it/s]
48%|████▊ | 174/360 [00:06<00:06, 27.87it/s]
49%|████▉ | 177/360 [00:06<00:06, 27.76it/s]
50%|█████ | 180/360 [00:06<00:06, 27.73it/s]
51%|█████ | 183/360 [00:06<00:06, 27.71it/s]
52%|█████▏ | 186/360 [00:06<00:06, 27.73it/s]
52%|█████▎ | 189/360 [00:07<00:06, 28.05it/s]
53%|█████▎ | 192/360 [00:07<00:06, 27.98it/s]
54%|█████▍ | 195/360 [00:07<00:06, 26.73it/s]
55%|█████▌ | 198/360 [00:07<00:06, 26.44it/s]
56%|█████▌ | 201/360 [00:07<00:05, 26.54it/s]
57%|█████▋ | 204/360 [00:07<00:05, 26.26it/s]
57%|█████▊ | 207/360 [00:07<00:05, 27.19it/s]
58%|█████▊ | 210/360 [00:07<00:05, 27.38it/s]
59%|█████▉ | 213/360 [00:07<00:05, 27.22it/s]
60%|██████ | 216/360 [00:08<00:05, 27.11it/s]
61%|██████ | 219/360 [00:08<00:05, 26.98it/s]
62%|██████▏ | 222/360 [00:08<00:05, 26.67it/s]
62%|██████▎ | 225/360 [00:08<00:05, 26.97it/s]
63%|██████▎ | 228/360 [00:08<00:07, 18.28it/s]
64%|██████▍ | 231/360 [00:08<00:06, 20.28it/s]
65%|██████▌ | 234/360 [00:08<00:05, 22.07it/s]
66%|██████▌ | 237/360 [00:08<00:05, 23.72it/s]
67%|██████▋ | 240/360 [00:09<00:04, 25.23it/s]
68%|██████▊ | 243/360 [00:09<00:04, 24.85it/s]
68%|██████▊ | 246/360 [00:09<00:04, 23.50it/s]
69%|██████▉ | 249/360 [00:09<00:04, 23.93it/s]
70%|███████ | 252/360 [00:09<00:04, 25.37it/s]
71%|███████ | 255/360 [00:09<00:03, 26.43it/s]
72%|███████▏ | 258/360 [00:09<00:03, 26.61it/s]
72%|███████▎ | 261/360 [00:09<00:03, 26.02it/s]
73%|███████▎ | 264/360 [00:10<00:03, 26.77it/s]
74%|███████▍ | 267/360 [00:10<00:03, 27.52it/s]
75%|███████▌ | 271/360 [00:10<00:03, 28.25it/s]
76%|███████▌ | 274/360 [00:10<00:03, 28.41it/s]
77%|███████▋ | 277/360 [00:10<00:02, 27.74it/s]
78%|███████▊ | 280/360 [00:10<00:02, 27.90it/s]
79%|███████▊ | 283/360 [00:10<00:02, 27.93it/s]
80%|███████▉ | 287/360 [00:10<00:02, 28.72it/s]
81%|████████ | 291/360 [00:10<00:02, 29.45it/s]
82%|████████▏ | 294/360 [00:11<00:02, 28.98it/s]
82%|████████▎ | 297/360 [00:11<00:02, 28.78it/s]
84%|████████▎ | 301/360 [00:11<00:02, 29.31it/s]
84%|████████▍ | 304/360 [00:11<00:01, 29.33it/s]
85%|████████▌ | 307/360 [00:11<00:01, 29.29it/s]
86%|████████▌ | 310/360 [00:11<00:01, 29.12it/s]
87%|████████▋ | 313/360 [00:11<00:01, 28.85it/s]
88%|████████▊ | 317/360 [00:11<00:01, 29.20it/s]
89%|████████▉ | 320/360 [00:11<00:01, 29.36it/s]
90%|████████▉ | 323/360 [00:12<00:01, 29.45it/s]
91%|█████████ | 326/360 [00:12<00:01, 29.50it/s]
91%|█████████▏| 329/360 [00:12<00:01, 29.44it/s]
92%|█████████▏| 332/360 [00:12<00:00, 29.04it/s]
93%|█████████▎| 335/360 [00:12<00:00, 29.23it/s]
94%|█████████▍| 339/360 [00:12<00:00, 29.72it/s]
95%|█████████▌| 343/360 [00:12<00:00, 30.18it/s]
96%|█████████▋| 347/360 [00:12<00:00, 30.22it/s]
98%|█████████▊| 351/360 [00:12<00:00, 29.81it/s]
99%|█████████▊| 355/360 [00:13<00:00, 29.99it/s]
100%|█████████▉| 359/360 [00:13<00:00, 30.14it/s]
100%|██████████| 360/360 [00:13<00:00, 27.14it/s]

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()
0%| | 0/360 [00:00<?, ?it/s]
24%|██▍ | 86/360 [00:00<00:00, 859.05it/s]
59%|█████▉ | 213/360 [00:00<00:00, 950.94it/s]
95%|█████████▌| 343/360 [00:00<00:00, 1033.30it/s]
100%|██████████| 360/360 [00:00<00:00, 1140.16it/s]

不同estimator得到的结果并不一样