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

tigramite_basic.py 7.0 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # # Tigramite基本使用教程
  4. # ## 线性测试
  5. # In[1]:
  6. import numpy as np
  7. import matplotlib
  8. from matplotlib import pyplot as plt
  9. get_ipython().run_line_magic('matplotlib', 'inline')
  10. ## use `%matplotlib notebook` for interactive figures
  11. # plt.style.use('ggplot')
  12. import sklearn
  13. import tigramite
  14. from tigramite import data_processing as pp
  15. from tigramite import plotting as tp
  16. from tigramite.pcmci import PCMCI
  17. from tigramite.independence_tests import ParCorr, GPDC, CMIknn, CMIsymb
  18. # 数据生成函数:
  19. #
  20. # \begin{aligned}
  21. # X^0_t &= 0.7 X^0_{t-1} - 0.8 X^1_{t-1} + \eta^0_t\\
  22. # X^1_t &= 0.8 X^1_{t-1} + 0.8 X^3_{t-1} + \eta^1_t\\
  23. # X^2_t &= 0.5 X^2_{t-1} + 0.5 X^1_{t-2} + 0.6 X^3_{t-3} + \eta^2_t\\
  24. # X^3_t &= 0.7 X^3_{t-1} + \eta^3_t\\
  25. # \end{aligned}
  26. #
  27. # 其中$\eta$满足均值为0方差为1的高斯分布,是添加的随机噪音。
  28. #
  29. # 目标:
  30. #
  31. # 重构出每一个变量的`drivers`。
  32. #
  33. # In[2]:
  34. np.random.seed(42)
  35. links_coeffs = {0: [((0, -1), 0.7), ((1, -1), -0.8)],
  36. 1: [((1, -1), 0.8), ((3, -1), 0.8)],
  37. 2: [((2, -1), 0.5), ((1, -2), 0.5), ((3, -3), 0.6)],
  38. 3: [((3, -1), 0.4)],
  39. }
  40. T = 1000
  41. data, true_parents_neighbors = pp.var_process(links_coeffs, T=T)
  42. # In[3]:
  43. T, N = data.shape
  44. var_names = [r'$X^0$', r'$X^1$', r'$X^2$', r'$X^3$']
  45. dataframe = pp.DataFrame(data,
  46. datatime = np.arange(len(data)),
  47. var_names=var_names)
  48. # In[4]:
  49. data.shape
  50. # In[5]:
  51. tp.plot_timeseries(dataframe)
  52. plt.show()
  53. # In[6]:
  54. parcorr = ParCorr(significance='analytic')
  55. pcmci = PCMCI(
  56. dataframe=dataframe,
  57. cond_ind_test=parcorr,
  58. verbosity=1)
  59. # In[7]:
  60. correlations = pcmci.get_lagged_dependencies(tau_max=20, val_only=True)['val_matrix']
  61. lag_func_matrix = tp.plot_lagfuncs(val_matrix=correlations,
  62. setup_args={'figsize': (6,6),'var_names':var_names,
  63. 'x_base':10, 'y_base':.5})
  64. # In[8]:
  65. pcmci.verbosity = 0
  66. results = pcmci.run_pcmci(tau_max=8, pc_alpha=None)
  67. # In[9]:
  68. print("p-values")
  69. print (results['p_matrix'].round(3))
  70. print("MCI partial correlations")
  71. print (results['val_matrix'].round(2))
  72. # In[10]:
  73. q_matrix = pcmci.get_corrected_pvalues(p_matrix=results['p_matrix'], fdr_method='fdr_bh')
  74. pcmci.print_significant_links(
  75. p_matrix = results['p_matrix'],
  76. q_matrix = q_matrix,
  77. val_matrix = results['val_matrix'],
  78. alpha_level = 0.01)
  79. # In[11]:
  80. link_matrix = pcmci.return_significant_links(pq_matrix=q_matrix,
  81. val_matrix=results['val_matrix'], alpha_level=0.01)['link_matrix']
  82. # In[12]:
  83. link_matrix.shape
  84. # In[13]:
  85. tp.plot_graph(
  86. figsize=(5,5),
  87. val_matrix=results['val_matrix'],
  88. link_matrix=link_matrix,
  89. var_names=var_names,
  90. link_colorbar_label='cross-MCI',
  91. node_colorbar_label='auto-MCI',
  92. ); plt.show()
  93. # In[14]:
  94. # Plot time series graph
  95. tp.plot_time_series_graph(
  96. val_matrix=results['val_matrix'],
  97. link_matrix=link_matrix,
  98. var_names=var_names,
  99. link_colorbar_label='MCI',
  100. ); plt.show()
  101. # ## 非线性测试
  102. # In[15]:
  103. import numpy as np
  104. import matplotlib
  105. from matplotlib import pyplot as plt
  106. get_ipython().run_line_magic('matplotlib', 'inline')
  107. ## use `%matplotlib notebook` for interactive figures
  108. # plt.style.use('ggplot')
  109. import sklearn
  110. import tigramite
  111. from tigramite import data_processing as pp
  112. from tigramite import plotting as tp
  113. from tigramite.pcmci import PCMCI
  114. from tigramite.independence_tests import ParCorr, GPDC, CMIknn, CMIsymb
  115. #
  116. # \begin{align*}
  117. # X^0_t &= 0.2 (X^1_{t-1})^2 + \eta^0_t\\
  118. # X^1_t &= \eta^1_t \\
  119. # X^2_t &= 0.3 (X^1_{t-2})^2 + \eta^2_t
  120. # \end{align*}
  121. # In[16]:
  122. var_names = [r'$X^0$', r'$X^1$', r'$X^2$']
  123. # In[17]:
  124. np.random.seed(1)
  125. data = np.random.randn(500, 3)
  126. for t in range(1, 500):
  127. data[t, 0] += 0.4*data[t-1, 1]**2
  128. data[t, 2] += 0.3*data[t-2, 1]**2
  129. dataframe = pp.DataFrame(data, var_names=var_names)
  130. tp.plot_timeseries(dataframe)
  131. plt.show()
  132. # ### corr
  133. # In[18]:
  134. parcorr = ParCorr(significance='analytic')
  135. pcmci_parcorr = PCMCI(
  136. dataframe=dataframe,
  137. cond_ind_test=parcorr,
  138. verbosity=0)
  139. # In[19]:
  140. correlations = pcmci_parcorr.get_lagged_dependencies(tau_max=20, val_only=True)['val_matrix']
  141. # In[20]:
  142. lag_func_matrix = tp.plot_lagfuncs(val_matrix=correlations,
  143. setup_args={'var_names':var_names, 'figsize': (5,5),
  144. 'x_base':5, 'y_base':.5})
  145. # In[21]:
  146. results = pcmci_parcorr.run_pcmci(tau_max=2, pc_alpha=0.2)
  147. pcmci_parcorr.print_significant_links(
  148. p_matrix = results['p_matrix'],
  149. val_matrix = results['val_matrix'],
  150. alpha_level = 0.01)
  151. # In[22]:
  152. q_matrix = pcmci_parcorr.get_corrected_pvalues(p_matrix=results['p_matrix'], fdr_method='fdr_bh')
  153. link_matrix = pcmci_parcorr.return_significant_links(pq_matrix=q_matrix,
  154. val_matrix=results['val_matrix'], alpha_level=0.01)['link_matrix']
  155. # In[23]:
  156. tp.plot_graph(
  157. val_matrix=results['val_matrix'],
  158. link_matrix=link_matrix,
  159. var_names=var_names,
  160. link_colorbar_label='cross-MCI',
  161. node_colorbar_label='auto-MCI',
  162. ); plt.show()
  163. # ### gpdc
  164. # In[24]:
  165. gpdc = GPDC(significance='analytic', gp_params=None)
  166. pcmci_gpdc = PCMCI(
  167. dataframe=dataframe,
  168. cond_ind_test=gpdc,
  169. verbosity=0)
  170. # In[25]:
  171. results = pcmci_gpdc.run_pcmci(tau_max=2, pc_alpha=0.1)
  172. pcmci_gpdc.print_significant_links(
  173. p_matrix = results['p_matrix'],
  174. val_matrix = results['val_matrix'],
  175. alpha_level = 0.01)
  176. # In[26]:
  177. q_matrix = pcmci_parcorr.get_corrected_pvalues(p_matrix=results['p_matrix'], fdr_method='fdr_bh')
  178. link_matrix = pcmci_parcorr.return_significant_links(pq_matrix=q_matrix,
  179. val_matrix=results['val_matrix'], alpha_level=0.01)['link_matrix']
  180. # In[27]:
  181. tp.plot_graph(
  182. val_matrix=results['val_matrix'],
  183. link_matrix=link_matrix,
  184. var_names=var_names,
  185. link_colorbar_label='cross-MCI',
  186. node_colorbar_label='auto-MCI',
  187. ); plt.show()
  188. # ### cmiknn
  189. # In[28]:
  190. cmi_knn = CMIknn(significance='shuffle_test', knn=0.1, shuffle_neighbors=5, transform='ranks')
  191. pcmci_cmi_knn = PCMCI(
  192. dataframe=dataframe,
  193. cond_ind_test=cmi_knn,
  194. verbosity=2)
  195. results = pcmci_cmi_knn.run_pcmci(tau_max=2, pc_alpha=0.05)
  196. pcmci_cmi_knn.print_significant_links(
  197. p_matrix = results['p_matrix'],
  198. val_matrix = results['val_matrix'],
  199. alpha_level = 0.01)
  200. # In[29]:
  201. q_matrix = pcmci_parcorr.get_corrected_pvalues(p_matrix=results['p_matrix'], fdr_method='fdr_bh')
  202. link_matrix = pcmci_parcorr.return_significant_links(pq_matrix=q_matrix,
  203. val_matrix=results['val_matrix'], alpha_level=0.01)['link_matrix']
  204. # In[30]:
  205. tp.plot_graph(
  206. val_matrix=results['val_matrix'],
  207. link_matrix=link_matrix,
  208. var_names=var_names,
  209. link_colorbar_label='cross-MCI',
  210. node_colorbar_label='auto-MCI',
  211. ); plt.show()
  212. # In[ ]:
  213. # In[ ]:
Tip!

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

Comments

Loading...