-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAXENT3D_PID.py
2259 lines (1698 loc) · 81.4 KB
/
MAXENT3D_PID.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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""MAXENT3D_PID.py -- Python module
MAXENT3D_PID: Trivariate Partial Information Decomposition via Maximum Entropy
https://github.com/Abzinger/MAXENT3D_PID
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify with proper attribution (Apache License version 2.0)
Information about the algorithm and examples are here:
@Article{?????????????,
author = {Makkeh, Abdullah and Theis, Dirk Oliver and Vicente, Raul and Chicharro, Daniel},
title = {????????},
journal = {????????},
year = ????,
volume = {??},
number = {?},
pages = {???}
}
Please cite this paper when you use this software (cf. README.md)
"""
import TRIVARIATE_SYN
import TRIVARIATE_UNQ
import TRIVARIATE_QP
import multiprocessing as mp
from multiprocessing import Pool
import ecos
from scipy import sparse
import numpy as np
from numpy import linalg as LA
import math
from collections import defaultdict
import time
log = math.log2
ln = math.log
def r_vidx(i):
return 3*i
def p_vidx(i):
return 3*i+1
def q_vidx(i):
return 3*i+2
class MAXENT3D_PID_Exception(Exception):
"""Prints exception when MAXENT3D_PID doesn't return a solution
"""
pass
class Solve_w_ECOS():
"""Solve_w_Ecos.
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify under Apache License version 2.0
Implements the ecos initialization and functions needed to be used by the children classes that computes PID terms
Methods:
condentropy__orig(pdf, output)
Computes H(T|X,Y,Z) w.r.t the original distribution P
entropy_V(V,pdf,output)
Computes H(T), H(X), H(Y), or H(Z) w.r.t the original distribution P
"""
def __init__(self, marg_tx, marg_ty, marg_tz, marg_xy, marg_xz, marg_yz):
"""
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify under Apache License version 2.0
Args:
marg_tx: dict() P(T,X)
marg_ty: dict() P(T,Y)
marg_tz: dict() P(T,Z)
marg_xy: dict() P(X,Y)
marg_xz: dict() P(X,Z)
marg_yz: dict() P(Y,Z)
"""
# ECOS parameters
self.ecos_kwargs = dict()
self.verbose = False
# Probability density funciton data
self.b_tx = dict(marg_tx)
self.b_ty = dict(marg_ty)
self.b_tz = dict(marg_tz)
self.b_xy = dict(marg_xy)
self.b_xz = dict(marg_xz)
self.b_yz = dict(marg_yz)
self.T =set([ t for t,x in self.b_tx.keys() ]
+ [ t for t,y in self.b_ty.keys() ]
+ [ t for t,z in self.b_tz.keys() ])
self.X = set( [ x for t,x in self.b_tx.keys() ] )
self.Y = set( [ y for t,y in self.b_ty.keys() ] )
self.Z = set( [ z for t,z in self.b_tz.keys() ] )
self.idx_of_quad = dict()
self.quad_of_idx = []
# Do stuff:
for t in self.T:
for x in self.X:
if (t,x) in self.b_tx.keys():
for y in self.Y:
if (t,y) in self.b_ty.keys():
for z in self.Z:
if (t,z) in self.b_tz.keys():
self.idx_of_quad[ (t,x,y,z) ] = len( self.quad_of_idx )
self.quad_of_idx.append( (t,x,y,z) )
#^ if
#^ for z
#^ if
#^ for y
#^ if
#^ for x
#^ for t
#^ init()
def condentropy__orig(self,pdf,output):
"""Computes H(T|X,Y,Z) w.r.t. the original distribution P of (T,X,Y,Z)
Args:
pdf: dictionary - the original distribution of (T,X,Y,Z)
Keys: (t,x,y,z)
values: P(t,x,y,z)
output: int - print different outputs based on (int) to console
Returns:
mysum: float - H(T|X,Y,Z)
"""
itic = time.process_time()
mysum = 0.
marg_xyz = defaultdict(lambda: 0.)
for txyz, i in pdf.items():
t,x,y,z = txyz
marg_xyz[x,y,z] += pdf[(t,x,y,z)]
#^ for
for txyz, i in pdf.items():
t,x,y,z = txyz
p = pdf[(t,x,y,z)]
mysum -= p*log(p/marg_xyz[x,y,z])
#^ for
itoc = time.process_time()
if output == 2: print("MAXENT3D_PID.condentropy__orig(): Time to compute H(T|XYZ) of the input pdf:", itoc - itic, "secs")
return mysum
#^ condentropy__orig()
# Compute H(T), H(X), H(Y), or H(Z)
def entropy_V(self, V, pdf, output):
"""Computes H(T), H(X), H(Y), or H(Z) w.r.t. the original
distribution P of (T,X,Y,Z)
Args:
V: int -
if 1 computes H(T)
if 2 computes H(X)
if 3 computes H(Y)
if 4 computes H(Z)
pdf: dictionary - the input distribution of (T,X,Y,Z)
Keys: (t,x,y,z)
values: P(t,x,y,z)
output: int - print different outputs based on (int) to console
Returns:
(if 1: V=T | if 2: V=X | if 3: V=Y | if 4: V=Z)
mysum: float - H(V)
"""
marg_V = defaultdict(lambda: 0.)
num_V = defaultdict(lambda: 0.)
mysum = 0.
if V == 1:
# H(S)
itic = time.process_time()
for txyz,r in pdf.items():
t,x,y,z = txyz
if r > 0:
marg_V[t] += r
num_V[t] = 1
#^ if
#^ for
for t in num_V.keys():
if marg_V[t] > 0: mysum -= marg_V[t]*log( marg_V[t] )
#^ for
itoc = time.process_time()
if output == 2: print("MAXENT3D_PID.entopry_V(): Time to compute H(T):", itoc - itic, "secs")
return mysum
elif V == 2:
# H(X)
itic = time.process_time()
for txyz,r in pdf.items():
t,x,y,z = txyz
if r > 0:
marg_V[x] += r
num_V[x] = 1
#^ if
#^ for
for x in num_V.keys():
mysum -= marg_V[x]*log( marg_V[x] )
#^ for
itoc = time.process_time()
if output == 2: print("MAXENT3D_PID.entropy_V(): Time to compute H(X):", itoc - itic, "secs")
return mysum
elif V == 3:
# H(Y)
itic = time.process_time()
for txyz,r in pdf.items():
t,x,y,z = txyz
if r > 0:
marg_V[y] += r
num_V[y] = 1
#^ if
#^ for
for y in num_V.keys():
mysum -= marg_V[y]*log( marg_V[y] )
#^ for
itoc = time.process_time()
if output == 2: print("MAXENT3D_PID.entropy_V(): Time to compute H(Y):", itoc - itic, "secs")
return mysum
elif V == 4:
# H(Z)
itic = time.process_time()
for txyz,r in pdf.items():
t,x,y,z = txyz
if r > 0:
marg_V[z] += r
num_V[z] = 1
#^ if
#^ for
for z in num_V.keys():
mysum -= marg_V[z]*log( marg_V[z] )
#^ for
itoc = time.process_time()
if output == 2: print("MAXENT3D_PID.entropy_V(): Time to compute H(Z):", itoc - itic, "secs")
return mysum
else:
print("MAXENT3D_PID.entropy_V(): The argument V takes the values: 1, 2, 3, or 4")
exit(1)
#^ entropy()
#^ class Solve_w_ECOS
class Opt_I(Solve_w_ECOS):
"""Implements the functions to compute Synergistic information ( CI(T;X,Y,Z )
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify under Apache License version 2.0
Methods:
create_model(output)
Creates the exponential Cone Program min_{Delta_p}H(T|X,Y,Z)
solve(c,G,h,dims,A,b,output)
Solves the exponential Cone Program min_{Delta_p}H(T|X,Y,Z)
dual_value(sol_lambda,b)
evaluates the dual value of H(T|X,Y,Z)
check_feasibility(sol_rpq,sol_lambda,output)
Checks the KKT conditions of the exponential Cone Program min_{Delta_p}H(T|X,Y,Z)
condentropy(sol_rpq,output)
evalutes the value of H(T|X,Y,Z) at the optimal distribution
"""
def create_model(self, output):
"""Creates the exponential Cone Program min_{q in Delta_d}H(T|X,Y,Z) of the form
min. c'x
s.t.
Ax = b
Gx <=_K h
where
x = (r,p,q)
K represents a vector representing cones (K_1, K_2) such that K_1 is a vector repesenting exponential cones and K_2 is a vector repesenting nonnegative cones
Args:
output: int - print different outputs based on (int) to console
Returns:
c: numpy.array - objective function weights
G: scipy.sparse.csc_matrix - matrix of exponential and nonnegative inequalities
h: numpy.array - L.H.S. of inequalities
dims: dictionary - cones to be used
keys: string - cone type (exponential or nonegative)
values: int - number of cones
A: scipy.sparse.csc_matrix - Matrix of marginal, q-w coupling, and q-p coupling equations
b: numpy.array - L.H.S. of equalities
"""
return TRIVARIATE_SYN.create_model(self, output)
def solve(self, c, G, h, dims, A, b, output):
"""Solves the exponential Cone Program min_{Delta_p}H(T|X,Y,Z)
Args:
c: numpy.array - objective function weights
G: scipy.sparse.csc_matrix - matrix of exponential and nonnegative inequalities
h: numpy.array - L.H.S. of inequalities
dims: dictionary - cones to be used
keys: string - cone type (exponential or nonegative)
values: int - number of cones
A: scipy.sparse.csc_matrix - Matrix of marginal, q-w coupling, and q-p coupling equations
b: numpy.array - L.H.S. of equalities
output: int - print different outputs based on (int) to console
Returns:
sol_rpq: numpy.array - primal optimal solution
sol_slack: numpy.array - slack of primal optimal solution (G*sol_rpq - h)
sol_lambda: numpy.array - equalities dual optimal solution
sol_mu: numpy.array - inequalities dual optimal solution
sol_info: dictionary - Brief stats of the optimization from ECOS
"""
return TRIVARIATE_SYN.solve(self, c, G, h, dims, A, b, output)
def dual_value(self, sol_lambda, b):
"""Evaluates the dual value of H(T|X,Y,Z)
Args:
sol_lambda: numpy.array - equalities dual optimal solution
b: numpy.array - L.H.S. of equalities
Returns:
float
"""
return TRIVARIATE_SYN.dual_value(self, sol_lambda, b)
def check_feasibility(self, sol_rpq, sol_lambda, output):
"""Checks the KKT conditions of the exponential Cone Program min_{Delta_p}H(T|X,Y,Z)
Args:
sol_rpq: numpy.array - primal optimal solution
sol_slack: numpy.array - slack of primal optimal solution (G*sol_rpq - h)
sol_lambda: numpy.array - equalities dual optimal solution
output: int - print different outputs based on (int) to console
Returns:
primal_infeasability: float - maximum violation of the optimal primal solution for primal equalities and inequalities
dual_infeasability: float - maximum violation of the optimal dual solution for dual equalities and inequalities
"""
return TRIVARIATE_SYN.check_feasibility(self, sol_rpq, sol_lambda, output)
def condentropy(self, sol_rpq, output):
"""Evalutes the value of H(T|X,Y,Z) at the optimal distribution
Args:
sol_rpq: numpy.array - primal optimal solution
output: int - print different outputs based on (int) to console
Returns:
mysum: float - H(T|X,Y,Z)
"""
return TRIVARIATE_SYN.condentropy(self, sol_rpq, output)
#^ subclass Opt_I
# Subclass to compute Unique Information
class Opt_II(Solve_w_ECOS):
"""Implements the functions to compute Unique information
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify under Apache License version 2.0
Methods:
initialization(which_sources)
Initialize the data for the triplets (T,U,V) where U,V in {X,Y,Z}
sq_vidx(i,which_sources)
Computes the index of the optimal distribution (q_vars) in sol_rpq
marginals(which_sources,sol_rpq,output)
Computes all the marginal distributions of the optimal distribution
create_model(which_sources,output)
Creates the exponential Cone Program min_{Delta_p}H(T|U,V) where U,V in {X,Y,Z}
solve(c,G,h,dims,A,b,output)
Solves the exponential Cone Program min_{Delta_p}H(T|U,V) where U,V in {X,Y,Z}
dual_value(sol_lambda,b)
Evaluates the dual value of H(T|U,V) where U,V in {X,Y,Z}
check_feasibility(which_sources, sol_rpq, sol_slack, sol_lambda, sol_mu, output)
Checks the KKT conditions of the exponential Cone Program min_{Delta_p}H(T|U,V)
where U,V in {X,Y,Z}
condentropy_2vars(which_sources,sol_rpq,output,marg_XY,marg_XZ,marg_YZ,marg_SXY,marg_SXZ,marg_SYZ)
Evalutes the value of H(T|U,V) w.r.t. the optimal distribution where U,V in {X,Y,Z}
"""
def initialization(self, which_sources):
"""Initialize the data for the triplets (T,V,W) where V,W in {X,Y,Z}
Args:
which_sources: list(int) -
[1,2] if sources are X and Y
[1,3] if sources are X and Z
[2,3] if sources are Y and Z
Returns:
(if [1,2] v,w=x,y|if [1,3] v,w=x,z|if [2,3] v,w=y,z|)
dictionary
keys: (t,v,w)
values: their indices
list of (t,v,w)
"""
return TRIVARIATE_UNQ.initialization(self, which_sources)
def sq_vidx(self, i, which_sources):
"""Computes the index of the optimal distribution (q_vars) in the optimal solution of the Exponential Cone Programming
Args:
i: int
which_sources: list(int) -
[1,2] if sources are X and Y
[1,3] if sources are X and Z
[2,3] if sources are Y and Z
Returns:
int
"""
return TRIVARIATE_UNQ.sq_vidx(self, i, which_sources)
def marginals(self, which_sources, sol_rpq, output):
"""Computes all the marginal distributions of the optimal distribution
Args:
which_sources: list(int) -
[1,2] if sources are X and Y and Q is the optimal distribution of min_{Delta_P} H(T|X,Y)
[1,3] if sources are X and Z and Q is the optimal distribution of min_{Delta_P} H(T|X,Z)
[2,3] if sources are Y and Z and Q is the optimal distribution of min_{Delta_P} H(T|Y,Z)
sol_rpq: numpy.array - array of triplets (r,p,q) of Exponential cone where q is the optimal distribution
output: int - print different outputs based on (int) to console
Returns:
dictionary - optimal marginal distribution of T
keys: t
values: Q(t)
dictionary - optimal marginal distribution of X
keys: x
values: Q(x)
dictionary - optimal marginal distribution of Y
keys: y
values: Q(y)
dictionary - optimal marginal distribution of Z
keys: z
values: Q(z)
dictionary - optimal marginal distribution of (T,X)
keys: t,x
values: Q(t,x)
dictionary - optimal marginal distribution of (T,Y)
keys: t,y
values: Q(t,y)
dictionary - optimal marginal distribution of (T,Z)
keys: t,z
values: Q(t,z)
dictionary - optimal marginal distribution of (X,Y)
keys: x,y
values: Q(x,y)
dictionary - optimal marginal distribution of (X,Z)
keys: x,z
values: Q(x,z)
dictionary - optimal marginal distribution of (Y,Z)
keys: y,z
values: Q(y,z)
dictionary - optimal marginal distribution of (T,X,Y)
keys: t,x,y
values: Q(t,x,y)
dictionary - optimal marginal distribution of (T,X,Z)
keys: t,x,z
values: Q(t,x,z)
dictionary - optimal marginal distribution of (T,Y,Z)
keys: t,y,z
values: Q(t,y,z)
"""
return TRIVARIATE_UNQ.marginals(self, which_sources,sol_rpq, output)
def create_model(self, which_sources, output):
"""Creates the exponential Cone Program min_{q in Delta_d}H(T|U,V) of the form
min. c'x
s.t.
Ax = b
Gx <=_K h
where
x = (r,p,q)
K represents a vector representing cones (K_1, K_2) such that K_1 is a vector repesenting exponential cones and K_2 is a vector repesenting nonnegative cones
Args:
which_sources: list(int) -
[1,2] if sources are X and Y
[1,3] if sources are X and Z
[2,3] if sources are Y and Z
Returns:
numpy.array - objective function weights
scipy.sparse.csc_matrix - matrix of exponential and nonnegative inequalities
numpy.array - L.H.S. of inequalities
dictionary - cones to be used
keys: string - cone type (exponential or nonegative)
values: int - number of cones
scipy.sparse.csc_matrix - Matrix of marginal, q-w coupling, and q-p coupling equations
numpy.array - L.H.S. of equalities
"""
return TRIVARIATE_UNQ.create_model(self, which_sources, output)
def solve(self, c, G, h, dims, A, b, output):
"""Solves the exponential Cone Program min_{Delta_p}H(T|U,V) where U,V in {X,Y,Z}
Args:
c: numpy.array - objective function weights
G: scipy.sparse.csc_matrix - matrix of exponential and nonnegative inequalities
h: numpy.array - L.H.S. of inequalities
dims: dictionary - cones to be used
keys: string - cone type (exponential or nonegative)
values: int - number of cones
A: scipy.sparse.csc_matrix - Matrix of marginal, q-w coupling, and q-p coupling equations
b: numpy.array - L.H.S. of equalities
output: int - print different outputs based on (int) to console
Returns:
sol_rpq: numpy.array - primal optimal solution
sol_slack: numpy.array - slack of primal optimal solution (G*sol_rpq - h)
sol_lambda: numpy.array - equalities dual optimal solution
sol_mu: numpy.array - inequalities dual optimal solution
sol_info: dictionary - Brief stats of the optimization from ECOS
"""
return TRIVARIATE_UNQ.solve(self, c, G, h, dims, A, b, output)
def dual_value(self, sol_lambda, b):
"""Evaluates the dual value of H(T|U,V) where U,V in {X,Y,Z}
Args:
sol_lambda: numpy.array - equalities dual optimal solution
b: numpy.array - L.H.S. of equalities
Returns:
float
"""
return TRIVARIATE_UNQ.dual_value(self, sol_lambda, b)
def check_feasibility(self, which_sources, sol_rpq, sol_slack, sol_lambda, sol_mu, output):
"""Checks the KKT conditions of the exponential Cone Program min_{Delta_p}H(T|U,V) where U,V in {X,Y,Z}
Args:
which_sources: list(int) -
[1,2] if sources are X and Y
[1,3] if sources are X and Z
[2,3] if sources are Y and Z
sol_rpq: numpy.array - primal optimal solution
sol_slack: numpy.array - slack of primal optimal solution (G*sol_rpq - h)
sol_lambda: numpy.array - equalities dual optimal solution
sol_mu: numpy.array - inequalities dual optimal solution
output: int - print different outputs based on (int) to console
Returns:
primal_infeasability: float - maximum violation of the optimal primal solution for primal equalities and inequalities
dual_infeasability: float - maximum violation of the optimal dual solution for dual equalities and inequalities
"""
return TRIVARIATE_UNQ.check_feasibility(self, which_sources, sol_rpq, sol_slack, sol_lambda, sol_mu, output)
def condentropy_2vars(self, which_sources,sol_rpq,output,marg_XY,marg_XZ,marg_YZ,marg_TXY,marg_TXZ,marg_TYZ):
"""Evalutes the value of H(T|U,V) w.r.t. the optimal distribution where U,V in {X,Y,Z}
Args:
which_sources: list(int) -
[1,2] if sources are X and Y
[1,3] if sources are X and Z
[2,3] if sources are Y and Z
sol_rpq: numpy.array - primal optimal solution
output: int - print different outputs based on (int) to console
marg_XY: dictionary - optimal marginal distribution of (X,Y)
keys: x,y
values: Q(x,y)
marg_XZ: dictionary - optimal marginal distribution of (X,Z)
keys: x,z
values: Q(x,z)
marg_YZ: dictionary - optimal marginal distribution of (Y,Z)
keys: y,z
values: Q(y,z)
marg_TXY: dictionary - optimal marginal distribution of (T,X,Y)
keys: t,x,y
values: Q(t,x,y)
marg_TXZ: dictionary - optimal marginal distribution of (T,X,Z)
keys: t,x,z
values: Q(t,x,z)
marg_TYZ: dictionary - optimal marginal distribution of (T,Y,Z)
keys: t,y,z
values: Q(t,y,z)
Returns:
(if [1,2]: U,V=X,Y | if [1,3]: U,V=X,Z | if [2,3]: U,V=Y,Z)
mysum: float - H(T|U,V)
"""
return TRIVARIATE_UNQ.condentropy_2vars(self, which_sources,sol_rpq,output,marg_XY,marg_XZ,marg_YZ,marg_TXY,marg_TXZ,marg_TYZ)
#^ subclass Opt_II
class QP():
"""Implements the functions that recover the solution (if needed) using Quadratic Programming
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify under Apache License version 2.0
Methods:
create_model(which_sources,output)
Creates the second-order cone program min_{Pi_x} (x - y)^2
solve(c,G,h,dims,A,b,output)
Solves the second-order cone program min_{Pi_x} (x - y)^2
"""
def __init__(self, CI, SI, UIX, UIY, UIZ, UIXY, UIXZ, UIYZ, MI, MIX, MIY, MIZ):
"""
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify under Apache License version 2.0
Args:
CI:(float,float) - returned synergy and confidence of the returned synergy
SI:(float,float) - returned shared and confidence of the returned shared
UIX:(float,float) - returned unique X and confidence of the returned unique X
UIY:(float,float) - returned unique Y and confidence of the returned unique Y
UIZ:(float,float) - returned unique Z and confidence of the returned unique Z
UIXY:(float,float) - returned unique X Y and confidence of the returned unique X Y
UIXZ:(float,float) - returned unique X Z and confidence of the returned unique X Z
UIYZ:(float,float) - returned unique Y Z and confidence of the returned unique Y Z
MI: float - MI(T;X,Y,Z)
MIX:float - MI(T;X)
MIY:float - MI(T;Y)
MIZ:float - MI(T;Z)
where the confidence is computed based on the duality gaps of the failed optimization problems
"""
# ECOS parameters
self.ecos_kwargs = dict()
self.verbose = False
# Probability density funciton data
self.CI_e, self.CI_c = CI
self.SI_e, self.SI_c = SI
self.UIX_e, self.UIX_c = UIX
self.UIY_e, self.UIY_c = UIY
self.UIZ_e, self.UIZ_c = UIZ
self.UIXY_e, self.UIXY_c = UIXY
self.UIXZ_e, self.UIXZ_c = UIXZ
self.UIYZ_e, self.UIYZ_c = UIYZ
self.MI = MI
self.MIX = MIX
self.MIY = MIY
self.MIZ = MIZ
self.dims = dict()
#^ init()
def create_model(self, which_probs):
"""Creates the second-order cone program min_{Pi_x}1/2 x^TWx + f^T x of the form
min 1/2 x^TWx + f^T x
subject to
Ax = b
x >= 0
The model can be written as SOCP:
min 1/2 t + f^T x
subject to
Ax = b
[W^(1/2), t, 1] in SOC
x >= 0
In ECOS
min c^T[t,x]
subject to
Ax = b
[-I, W^(1/2)]*[t,x]^T <_{K} [0, 0, 0]
where K := R_+ . Q, c^T := [1/2,f^T]
Args:
which_probs: list(int) -
[1] if min_{Delta_p}H(T|X,Y,Z) failed
[12] if min_{Delta_p}H(T|X,Y) failed
[13] if min_{Delta_p}H(T|X,Z) failed
[23] if min_{Delta_p}H(T|Y,Z) failed
[1,12] if min_{Delta_p}H(T|X,Y,Z) and min_{Delta_p}H(T|X,Y) failed
[1,13] if min_{Delta_p}H(T|X,Y,Z) and min_{Delta_p}H(T|X,Z) failed
[1,23] if min_{Delta_p}H(T|X,Y,Z) and min_{Delta_p}H(T|Y,Z) failed
[12,13] if min_{Delta_p}H(T|X,Y) and min_{Delta_p}H(T|X,Z) failed
[12,23] if min_{Delta_p}H(T|X,Y) and min_{Delta_p}H(T|Y,Z) failed
[13,23] if min_{Delta_p}H(T|X,Z) and min_{Delta_p}H(T|Y,Z) failed
[1,12,13] if min_{Delta_p}H(T|X,Y,Z), min_{Delta_p}H(T|X,Y), and min_{Delta_p}H(T|X,Z) failed
[1,12,23] if min_{Delta_p}H(T|X,Y,Z), min_{Delta_p}H(T|X,Y), and min_{Delta_p}H(T|Y,Z) failed
[1,13,23] if min_{Delta_p}H(T|X,Y,Z), min_{Delta_p}H(T|X,Z), and min_{Delta_p}H(T|Y,Z) failed
[12,13,23] if min_{Delta_p}H(T|X,Y), min_{Delta_p}H(T|X,Z), and min_{Delta_p}H(T|Y,Z) failed
Returns:
numpy.array - objective function weights
scipy.sparse.csc_matrix - matrix of soc and nonnegative inequalities
numpy.array - L.H.S. of inequalities
dictionary - cones to be used
keys: string - cone type (soc or nonegative)
values: int - number of cones
scipy.sparse.csc_matrix - Matrix of identity equations
numpy.array - L.H.S. of equalities
"""
return TRIVARIATE_QP.create_model(self, which_probs)
def solve(self, c, G, h, dims, A, b, output):
"""Solves the second-order cone program min_{Pi_x}1/2 x^TWx + f^T x
Args:
c: numpy.array - objective function weights
G: scipy.sparse.csc_matrix - matrix of soc and nonnegative inequalities
h: numpy.array - L.H.S. of inequalities
dims: dictionary - cones to be used
keys: string - cone type (soc or nonegative)
values: int - number of cones
A: scipy.sparse.csc_matrix - Matrix of identity equations
b: numpy.array - L.H.S. of equalities
output: int - print different outputs based on (int) to console
Returns:
sol_tx: numpy.array - primal optimal solution
sol_slack: numpy.array - slack of primal optimal solution (G*sol_rpq - h)
sol_lambda: numpy.array - equalities dual optimal solution
sol_mu: numpy.array - inequalities dual optimal solution
sol_info: dictionary - Brief stats of the optimization from ECOS
"""
return TRIVARIATE_QP.solve(self, c, G, h, dims, A, b, output)
#^ class QP
# Compute Marginals
def marginal_tx(p):
"""Computes the original marginal distribution of T and X
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify under Apache License version 2.0
Args:
p: dictionary - original distribution of (T,X,Y,Z)
keys: (t,x,y,z)
values: P(t,x,y,z)
Returns:
dictionary - original marginal distribution of T and X
keys: (t,x)
values: P(t,x)
"""
marg = dict()
for txyz,r in p.items():
t,x,y,z = txyz
if (t,x) in marg.keys(): marg[(t,x)] += r
else: marg[(t,x)] = r
return marg
#^ marginal_tx()
def marginal_ty(p):
"""Computes the original marginal distribution of T and Y
(c) Abdullah Makkeh, Dirk Oliver Theis
Permission to use and modify under Apache License version 2.0
Args:
p: dictionary - original distribution of (T,X,Y,Z)
keys: (t,x,y,z)
values: P(t,x,y,z)
Returns:
dictionary - original marginal distribution of T and Y
keys: (t,y)
values: P(t,y)
"""
marg = dict()
for txyz,r in p.items():
t,x,y,z = txyz
if (t,y) in marg.keys(): marg[(t,y)] += r
else: marg[(t,y)] = r
return marg
#^ marginal_ty()
def marginal_tz(p):
"""Computes the original marginal distribution of T and Z
(c) Abdullah Makkeh, Dirk Oliver Theis