/
methods.py
780 lines (546 loc) · 21.4 KB
/
methods.py
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 19 10:17:50 2021
@author: yohanna
"""
import itertools
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import config
p = config.setup()
lgr = p.logger
from data import SynDAG
from evl import DCP
def clime(data):
'''
learn cov_est use CLIME algorithm.
Paper: http://stat.wharton.upenn.edu/~tcai/paper/Precision-Matrix.pdf
R package: https://cran.r-project.org/web/packages/flare/index.html
Arguments:
data : Input test data;
Return:
clime_cov_est : Estimated empirical covariance matrix from testing data
Notes:
CLIME solves the following minimization problem
min ||Ω||_1 s.t. ||SΩ − I||∞ ≤ λ,
'''
import rpy2.robjects as robjects
from rpy2.robjects.packages import STAP
import rpy2.robjects.numpy2ri
from rpy2.robjects import pandas2ri
pandas2ri.activate()
rpy2.robjects.numpy2ri.activate()
robjects.r.source("utils.R")
with open('utils.R', 'r') as f:
string = f.read()
bayesian_network = STAP(string, "bayesian_network")
clime_cov_est = bayesian_network.clime(data)
return clime_cov_est
def tiger(data):
'''
learn cov_est use TIGER algorithm.
Paper: http://stat.wharton.upenn.edu/~tcai/paper/Precision-Matrix.pdf
R package: https://cran.r-project.org/web/packages/flare/index.html
Arguments:
data : Input test data;
Return:
tiger_cov_est : Estimated empirical covariance matrix from testing data
'''
import rpy2.robjects as robjects
from rpy2.robjects.packages import STAP
import rpy2.robjects.numpy2ri
from rpy2.robjects import pandas2ri
pandas2ri.activate()
rpy2.robjects.numpy2ri.activate()
robjects.r.source("utils.R")
with open('utils.R', 'r') as f:
string = f.read()
bayesian_network = STAP(string, "bayesian_network")
tiger_cov = bayesian_network.tiger(data)
return tiger_cov
def glasso_R(data):
'''
learn cov_est use TIGER algorithm.
Paper: http://stat.wharton.upenn.edu/~tcai/paper/Precision-Matrix.pdf
R package: https://cran.r-project.org/web/packages/flare/index.html
Arguments:
data : Input test data;
Return:
tiger_cov_est : Estimated empirical covariance matrix from testing data
'''
import rpy2.robjects as robjects
from rpy2.robjects.packages import STAP
import rpy2.robjects.numpy2ri
from rpy2.robjects import pandas2ri
pandas2ri.activate()
rpy2.robjects.numpy2ri.activate()
robjects.r.source("utils.R")
with open('utils.R', 'r') as f:
string = f.read()
bayesian_network = STAP(string, "bayesian_network")
glasso_cov = bayesian_network.glasso_r(data)
return glasso_cov
def empirical_est(data):
'''
The empirical estimator (like in Appendix C of https://arxiv.org/pdf/1710.05209.pdf);
where cov = 1/m * E(X^T * X)
Arguments:
data : Input test data;
Return:
cov_est : Estimated empirical covariance matrix from testing data
'''
#return np.cov(data.T) #(np.matmul(data.T, data)/p.s)
empir_cov_np = np.cov(data.T, bias=True)
b = data - np.mean(data, axis=0)
empir_cov = np.matmul(np.transpose(b), b)/data.shape[0]
return empir_cov
#@ignore_warnings(category=ConvergenceWarning)
def glasso(data):
from sklearn.covariance import GraphicalLasso
'''
Graphical Lasso algorithm from sklearn package
Arguments:
data : Input data;
Return:
cov_est : Graph with learned coefficients
'''
cov = GraphicalLasso(mode='lars', max_iter=2000).fit(data)
cov_est = np.around(cov.covariance_, decimals=3)
return cov_est
def sigma_estimator(data, A_est):
'''
Algorithm 1: Recovering the varianceσgiven an estimatêA of coefficientsA
'''
n = p.n
Sigma_hat = {}
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_est[:, child]))]
parents = list(itertools.chain(*parents))
''' Calculate a_est'''
index_est = A_est[:, child]
a_est = index_est[index_est != 0]
''' Calculate sigma_y (true)'''
if len(a_est) == 0:
sigma_hat = np.sqrt(np.var(data[:, child]))
elif len(a_est) == 1:
sigma_hat = np.sqrt(np.var(data[:, child] - a_est *
np.transpose(data[:, parents])))
elif len(a_est) > 1:
sigma_hat = np.sqrt(np.var(
data[:, child] - np.matmul(np.array(a_est), np.transpose(data[:, parents]))))
Sigma_hat[child] = sigma_hat
return Sigma_hat
def regression(data, A_bin):
from sklearn.linear_model import LinearRegression
'''
Learn coefficients through linear regression
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
n = p.n
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
if len(parents) > 0:
for pa in parents:
data_ch = data[:, child].reshape(-1, 1)
data_pa = data[:, pa].reshape(-1, 1)
'''
Notes: Linear regression returns 'Coefficient of determination',
which is R-square.
Coefficient of correlation is “R” value which is given
in the summary table in the Regression output.
'''
reg = LinearRegression().fit(data_ch, data_pa)
A_est[pa, child] = float(reg.coef_)
return A_est
def batch_least_square_mean(data, A_bin, mb_size):
'''
Algorithm 6:
Recovery algorithm for general Bayesian networks via Batch Least Squre + x
Learn coefficients through linear regression
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
n = p.n
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
if len(parents) > 0:
a_est_list = []
for i in range(0, data.shape[0], mb_size+len(parents)):
if i + mb_size+len(parents) < data.shape[0]:
X = data[i:i+mb_size+len(parents), parents]
Y = data[i:i+mb_size+len(parents), child]
'''
Notes: A_hat = (X^T*X)^{-1}*X^T*Y
'''
a_est = np.dot(np.dot(np.linalg.inv(np.dot(X.T, X)), X.T), Y)
a_est_list.append(a_est)
else: break
a_est_mean = np.mean(a_est_list, axis=0)
A_est[parents, child] = a_est_mean
return A_est
def batch_least_square_median(data, A_bin, mb_size):
'''
Algorithm 6:
Recovery algorithm for general Bayesian networks via Batch Least Squre + x
Learn coefficients through linear regression
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
n = p.n
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
if len(parents) > 0:
a_est_list = []
for i in range(0, data.shape[0], mb_size+len(parents)):
if i + mb_size+len(parents) < data.shape[0]:
X = data[i:i+mb_size+len(parents), parents]
Y = data[i:i+mb_size+len(parents), child]
'''
Notes: A_hat = (X^T*X)^{-1}*X^T*Y
'''
a_est = np.dot(np.dot(np.linalg.inv(np.dot(X.T, X)), X.T), Y)
a_est_list.append(a_est)
else: break
a_est_median = np.median(a_est_list, axis=0)
A_est[parents, child] = a_est_median
return A_est
def least_square_ill(data, A_bin):
'''
Algorithm 2:
Recovery algorithm for general Bayesian networks via Least Squares estimators
Learn coefficients through linear regression
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
from sklearn.linear_model import Ridge, RidgeCV
n = p.n
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
if len(parents) > 0:
X = data[:, parents]
Y = data[:, child]
Y = np.expand_dims(Y, axis=1)
'''
Notes: A_hat = (X^T*X)^{-1}*X^T*Y
'''
#a_est = Ridge(alpha=1e-20) #1e-7, 1e-5, 1e-3, 1e-1, 1
#a_est.fit(X, Y)
a_est = RidgeCV(alphas=[1e-10, 1e-7, 1e-3, 1e-1, 1]).fit(X, Y)
A_est[parents, child] = a_est.coef_
return A_est
def least_square(data, A_bin):
'''
Algorithm 2:
Recovery algorithm for general Bayesian networks via Least Squares estimators
Learn coefficients through linear regression
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
n = p.n
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
if len(parents) > 0:
X = data[:, parents]
Y = data[:, child]
'''
Notes: A_hat = (X^T*X)^{-1}*X^T*Y
'''
#a_est1 = np.matmul(np.matmul(np.linalg.inv(np.matmul(np.transpose(X), X)), np.transpose(X)), Y)
a_est = np.dot(np.dot(np.linalg.inv(np.dot(X.T, X)), X.T), Y)
#a = np.dot(X.T, X)
#b = np.dot(X.T, Y)
#a_est = np.linalg.solve(a, b)
A_est[parents, child] = a_est
return A_est
def CauchyEst_trimmed(data, A_bin):
'''
Algorithm 3:
Recovery algorithm for tree-skeletoned Bayesian networks
Learn coefficients use median
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
n, d = p.n, p.d
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
if len(parents) > 0:
'''
P: number of parents, 1 <= p <= d
'''
P = int(len(parents))
A_est_s = []
for s in range(data.shape[0] - d):
X = data[s:s+P, parents]
Y = np.expand_dims(data[s:s+P, child], axis=1)
if X.shape[0] == X.shape[1]:
a_est_s = np.matmul(np.linalg.inv(X), Y)
A_est_s = np.append(A_est_s, a_est_s)
else:
break
''' Find the median '''
A_est_s = A_est_s.reshape(-1, P).T
#A_est_median = np.median(A_est_s, axis=1)
A_est_median = []
for i in range(A_est_s.shape[0]):
a_est_median = stats.trim_mean(A_est_s[i,:], 0.38)
A_est_median.append(a_est_median)
for i in range(len(parents)):
A_est[parents[i], child] = A_est_median[i]
return A_est
def CauchyEst_Tree(data, A_bin):
'''
Algorithm 3:
Recovery algorithm for tree-skeletoned Bayesian networks
Learn coefficients use median
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
n, d = p.n, p.d
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
if len(parents) > 0:
'''
P: number of parents, 1 <= p <= d
'''
P = int(len(parents))
A_est_s = []
for s in range(data.shape[0] - P):
X = data[s:s+P, parents]
Y = np.expand_dims(data[s:s+P, child], axis=1)
if X.shape[0] == X.shape[1]:
#a_est_s = np.matmul(np.linalg.inv(X), Y)
a_est_s = np.linalg.solve(X, Y)
# Try use np.solve
A_est_s = np.append(A_est_s, a_est_s)
else:
break
''' Find the median '''
A_est_s = A_est_s.reshape(-1, P).T
A_est_median = np.median(A_est_s, axis=1)
for i in range(len(parents)):
A_est[parents[i], child] = A_est_median[i]
return A_est
def heuristic_extension_trimmed(data, A_bin):
import scipy.linalg
'''
Algorithm 4: Recovery algorithm for general Bayesian networks.
Estimate L_hat using empirical covariance matrix M_hat.
Learn coefficients from median
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
n, d = p.n, p.d
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
''' Calculate M: covariance matrix among parents'''
if len(parents) > 0:
if len(parents) == 1:
M = np.var(data[:, parents].T)
else:
M = np.cov(data[:, parents].T)
''' Compute Cholesky decomposition M_hat = L_hat * L_hat^T'''
L = scipy.linalg.cholesky(M, lower=True)
'''
p: number of parents, 1 <= p <= d
'''
P = len(parents)
A_est_s = []
for s in range(data.shape[0] - d + 1):
X = data[s:s+P, parents]
Y = np.expand_dims(data[s:s+P, child], axis=1)
if X.shape[0] == X.shape[1]:
a_est_s = np.matmul(np.linalg.inv(X), Y)
A_est_s = np.append(A_est_s, a_est_s)
else:
break
''' Find the median '''
A_est_s = A_est_s.reshape(-1, P).T
#MED = np.median(np.matmul(np.transpose(L), A_est_s), axis=1)
temp = np.matmul(np.transpose(L), A_est_s)
MED = []
for i in range(A_est_s.shape[0]):
med = stats.trim_mean(temp[i,:], 0.15)
MED.append(med)
''' Define estimates A_hat '''
A_est_i = np.matmul(np.linalg.inv(np.transpose(L)), np.transpose(MED))
for i in range(len(parents)):
A_est[parents[i], child] = A_est_i[i]
return A_est
def CauchyEst_General(data, A_bin):
import scipy.linalg
'''
Algorithm 4: Recovery algorithm for general Bayesian networks.
Estimate L_hat using empirical covariance matrix M_hat.
Learn coefficients from median
Arguments:
data : Input data;
A_bin : The graph structure
Return:
A_est : Graph with learned coefficients
'''
n, d = p.n, p.d
A_est = np.zeros((n, n))
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
''' Calculate M: covariance matrix among parents'''
if len(parents) > 0:
if len(parents) == 1:
M = np.var(data[:, parents].T)
else:
#print('parents bigger than 1')
M = np.cov(data[:, parents].T)
''' Compute Cholesky decomposition M_hat = L_hat * L_hat^T'''
def cholesky(A):
"""Performs a Cholesky decomposition of A, which must
be a symmetric and positive definite matrix. The function
returns the lower variant triangular matrix, L."""
n = len(A)
# Create zero matrix for L
L = [[0.0] * n for i in range(n)]
# Perform the Cholesky decomposition
for i in range(n):
for k in range(i+1):
tmp_sum = sum(L[i][j] * L[k][j] for j in range(k))
if (i == k): # Diagonal elements
# LaTeX: l_{kk} = \sqrt{ a_{kk} - \sum^{k-1}_{j=1} l^2_{kj}}
L[i][k] = np.sqrt(A[i][i] - tmp_sum)
else:
# LaTeX: l_{ik} = \frac{1}{l_{kk}} \left( a_{ik} - \sum^{k-1}_{j=1} l_{ij} l_{kj} \right)
L[i][k] = (1.0 / L[k][k] * (A[i][k] - tmp_sum))
return L
L = scipy.linalg.cholesky(M, lower=True)
'''
p: number of parents, 1 <= p <= d
'''
P = len(parents)
A_est_s = []
for s in range(data.shape[0] - P):
X = data[s:s+P, parents]
Y = np.expand_dims(data[s:s+P, child], axis=1)
if X.shape[0] == X.shape[1]:
a_est_s = np.matmul(np.linalg.inv(X), Y)
A_est_s = np.append(A_est_s, a_est_s)
else:
break
''' Find the median '''
A_est_s = A_est_s.reshape(-1, P).T
MED = np.median(np.matmul(np.transpose(L), A_est_s), axis=1)
''' Define estimates A_hat '''
A_est_i = np.matmul(np.linalg.inv(np.transpose(L)), np.transpose(MED))
for i in range(len(parents)):
A_est[parents[i], child] = A_est_i[i]
return A_est
def split_data(data):
'''
Split the data into training (eg. 80%) and testing (eg. 20%)
Arguments:
data : Input data;
Returns:
train_data: training data
test_data : testing data
'''
train_prop = p.train
train_index = int(train_prop * data.shape[0])
train_data = data[0:train_index, :]
test_data = data[train_index:, :]
return train_data, test_data
def ground_truth_cov(data, A_bin, M_gt):
'''
Given training data, get the ground truth covariance matrix between each
[child, parents] data, and the covariance matrix over whole training data.
Therefore, if there are n nods, we will return n+1 matrix.
Arguments:
data : Input data;
A_bin : Binary adjacency matrix;
Returns:
dic_cov : a dictionary stores all the ground truth covariance matrix.
'''
n = p.n
dic_cov_idx = {}
dic_cov_val = {}
dic_cov_val_gt = {}
for child in range(n):
parents = [list(pa) for pa in (np.nonzero(A_bin[:, child]))]
parents = list(itertools.chain(*parents))
'''
Notes: For each child node, M is the covairnce matrix between its parents.
'''
if len(parents) == 0:
M = 0
M2 = 0
elif len(parents) == 1:
M = np.var(data[:, parents].T)
M2 = M_gt[np.ix_(parents, parents)]
elif len(parents) > 1:
M = np.cov(data[:, parents].T)
M2 = M_gt[np.ix_(parents, parents)]
else:
raise ValueError('unknown parents size')
dic_cov_idx[child] = parents
dic_cov_val[child] = M
dic_cov_val_gt[child] = M2
gt_cov = np.cov(data.T)
return dic_cov_idx, dic_cov_val, dic_cov_val_gt, gt_cov
def nodes_to_big_value(data):
n, s = p.n, p.s
mu, sigma = 10000, 1
num_noise_node = 2
for i in range(s):
nodes = np.sort(np.random.choice(n, size=num_noise_node, replace=False))
data[i, nodes] = np.random.normal(mu, sigma, len(nodes)) #np.array(10000 * np.ones(s)) #
return data
def list_to_big_value(data):
n, s = p.n, p.s
mu, sigma = 10000, 1
percent_of_sample = 0
num_noise_node = 2
sample_size = int(s * percent_of_sample / 100)
sample = np.sort(np.random.choice(s, size=sample_size, replace=False))
for i in sample:
nodes = np.sort(np.random.choice(n, size=num_noise_node, replace=False))
data[i, nodes] = np.random.normal(mu, sigma, num_noise_node)#np.array(10000 * np.ones(n))
return data