-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprobmonad.py
304 lines (236 loc) · 9.87 KB
/
probmonad.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
import sys
import operator
from pymonad import *
from util import *
import numpy as np
#import multiprocessing
#from multiprocessing import Pool
#p = Pool(multiprocessing.cpu_count())
#print "cpus=" + str(multiprocessing.cpu_count())
class Dist(Monad):
def __init__(self, newdist):
self.dist = newdist
self.hash = None
def __hash__(self):
if self.hash == None:
self.hash = hash(tuple(sorted(self)))
return self.hash
def __iter__(self): return self.dist.iteritems()
def map(self, f): return map(f,self)
def __repr__(self): return str(self)
def __str__(self):
ent = self.entropy()
vul = self.vulnerability()
temp = "Dist (ent=%0.6f, vul=%0.6f)\n" % (ent,vul)
items = list(self)
for (value,prob) in items[:10]:
temp += " %0.10f" % (prob) + ": " + str(value) + "\n"
if (len(items)) > 10:
temp += "... and " + str(len(items) - 10) + " more item(s)\n"
return temp
@staticmethod
def singleton(value):
return Dist({value: 1.0})
@staticmethod
def flip(prob):
return Dist({True: prob, False: 1 - prob})
@staticmethod
def uniform(items):
prob = 1.0 / len(items)
return Dist({item: prob for item in items})
@staticmethod
def lift(items):
count = 0
counts = dict()
for item in items:
count += 1
insert_with(counts, item, 1, operator.add)
prob = 1.0 / count
return Dist({item: counts[item] * prob for item in counts})
def scale(self, prob1):
return Dist({value: prob1 * prob2 for (value, prob2) in self})
def bind(self, f):
newdist = dict()
for (value,prob) in self:
for (value2,prob2) in f(value):
insert_with(newdist, value2, prob2 * prob, operator.add)
return Dist(newdist)
def project(self, f):
newdist = dict()
for (value,prob) in self:
insert_with(newdist, f(value), prob, operator.add)
return Dist(newdist)
def __getitem__(self, key):
return self.project(lambda v: v.__getitem__(key))
def __getattr__(self, name):
def _missing(*args, **kwargs):
return self.project(lambda v: v.__getattr__(name)(*args,**kwargs))
return _missing
def reflect(self):
return Dist({(value,prob): prob for (value,prob) in self})
def conditional(self,f):
subdists = dict()
for (value,prob) in self:
cond = f(value)
if not cond in subdists:
subdists[cond] = [prob,{value:prob}]
else:
subdists[cond][0] += prob
subdists[cond][1][value] = prob
return Dist({Dist(subdist).scale(1/totalprob): totalprob
for (totalprob,subdist) in subdists.values()})
def conditional_bins(self,f):
subdists = dict()
for (value,prob) in self:
cond = f(value)
if not cond in subdists:
subdists[cond] = [prob,{value:prob}]
else:
subdists[cond][0] += prob
subdists[cond][1][value] = prob
return {cond: (Dist(subdist).scale(1/totalprob), totalprob)
for (cond, (totalprob,subdist)) in subdists.iteritems()
}
def expectation(self, f=None):
if f == None:
return sum(self.map(lambda (value,prob): prob * value))
else:
return self.expectation_projected(f)
def expectation_projected(self, f):
return sum(self.map(lambda (value,prob): prob * f(value)))
def max(self):
mv = None
mp = 0.0
for (v,p) in self:
if p > mp:
mv = v
mp = p
if p > 0.5:
return (mv, mp)
return (mv, mp)
def argmax (self): return first (self.max())
def probmax(self): return second(self.max())
def entropy(self):
if len(self.dist) == 1: # floating point issues
return 0.0
return self.reflect().expectation(lambda (value,prob): - lg(prob))
def vulnerability(self):
if len(self.dist) == 1: # floating point issues
return 1.0
return self.probmax()
def conditional_entropy(self,f):
#return self.conditional(f).expectation(Dist.entropy)
# That form is too expensive given distribution hashing is pointlessly performed.
return sum(map(lambda (d,p): d.entropy() * p, self.conditional_bins(f).values()))
def conditional_vulnerability(self,fb):
# CV(A,B) := E_b V(A | B = b)
#distAB = self.project(lambda x: (fa(x), fb(x)))
return sum(map(lambda (d,p): d.vulnerability() * p, self.conditional_bins(fb).values()))
def mutual_information(self,fa,fb):
if self.project(fa).entropy() == 0.0: return 0.0
if self.project(fb).entropy() == 0.0: return 0.0
distAB = self.project(lambda x: (fa(x), fb(x)))
entA = distAB.project(first).entropy()
entAgB = distAB.conditional_entropy(second)
return entA - entAgB
def normalized_mutual_information(self,fa,fb):
# NMI(A,B) := I(A;B) / H(A,B)
if self.project(fa).entropy() == 0.0: return 0.0
if self.project(fb).entropy() == 0.0: return 0.0
# floating point issues
distAB = self.project(lambda x: (fa(x), fb(x)))
entA = distAB.project(first).entropy()
#entB = distAB.project(second).entropy()
entAgB = distAB.conditional_entropy(second)
entAB = distAB.entropy()
# I think I fixed the floating point issues with the check earlier
#if entAB <= 0.00000000001: return 0.0
#if abs(entA - entAgB) < 0.00000000001: return 0.0
return (entA - entAgB) / entAB
#return (entA - entAgB) / math.sqrt(entA * entB)
def normalized_mutual_information_skl(self,fa,fb,bins=10):
# NMI(A,B) := I(A;B) / H(A,B)
if self.project(fa).entropy() == 0.0: return 0.0
if self.project(fb).entropy() == 0.0: return 0.0
# floating point issues
distAB = self.project(lambda x: (fa(x), fb(x)))
dbA = [val[0] for (val, prob) in distAB]
dbB = [val[1] for (val, prob) in distAB]
prob = [prob for (val, prob) in distAB]
from sklearn.metrics import mutual_info_score
c_xy = np.histogram2d(dbA, dbB, bins, weights = prob)[0]
mi = mutual_info_score(None, None, contingency=c_xy)
return mi
def dual_sided_conditional_vulnerability(self,fa,fb):
# DCV(A,B) := 1/2( V(A|B) + V(B|A) )
distAB = self.project(lambda x: (fa(x), fb(x)))
vulAgB = distAB.conditional_vulnerability(second)
vulBgA = distAB.conditional_vulnerability(first)
return 0.5 * (vulAgB + vulBgA)
def vulnerability_flow(self,fa,fb):
# VF(A,B) := V(A|B) - V(A)
distAB = self.project(lambda x: (fa(x), fb(x)))
vulAgB = distAB.conditional_vulnerability(second)
vulA = distAB.project(first).vulnerability()
# if vulA == 1.0:
# return 1.0
# else:
# return (vulAgB - vulA)/(1.0 - vulA)
return vulAgB - vulA
def dual_sided_vulnerability_flow(self,fa,fb):
# DVF(A,B) := 1/2 (VF(A,B) + VB(B,A))
distAB = self.project(lambda x: (fa(x), fb(x)))
vulAgB = distAB.conditional_vulnerability(second)
vulBgA = distAB.conditional_vulnerability(first)
vulA = distAB.project(first) .vulnerability()
vulB = distAB.project(second).vulnerability()
# if vulA == 1.0:
# sideA = 1.0
# else:
# sideA = (vulAgB - vulA) / (1.0 - vulA)
#
# if vulB == 1.0:
# sideB = 1.0
# else:
# sideB = (vulBgA - vulB) / (1.0 - vulB)
sideA = vulAgB - vulA
sideB = vulBgA - vulB
return 0.5 * (sideA + sideB)
def association_metrics(self, fo, fs):
# fo - project to output of a sub-expression
# fs - project to sensitive attribute
# distOS = self.project(lambda x: (fo(x), fs(x)))
#
# entO = distOS.project(first).entropy()
# entOgS = distOS.conditional_entropy(second)
# entOS = distOS.entropy()
#
# vulOgS = distOS.conditional_vulnerability(second)
# vulSgO = distOS.conditional_vulnerability(first)
# vulO = distOS.project(first).vulnerability()
# vulS = distOS.project(second).vulnerability()
#if self.project(fa).entropy() == 0.0: return 0.0
#if self.project(fb).entropy() == 0.0: return 0.0
distOS = self.project(lambda x: (fo(x), fs(x)))
return {'mi' : self.mutual_information(fo,fs),
'nmi' : self.normalized_mutual_information(fo,fs),
'nmi-skl' : self.normalized_mutual_information_skl(fo,fs),
'cv-OgS' : distOS.conditional_vulnerability(second),
'cv-SgO' : distOS.conditional_vulnerability(first),
'dcv' : self.dual_sided_conditional_vulnerability(first,second),
'vf-OtoS' : self.vulnerability_flow(first,second),
'vf-StoO' : self.vulnerability_flow(second,first),
'dvf' : self.dual_sided_vulnerability_flow(first,second)}
# return {'mi' : entO - entOgS,
# 'nmi' : (entO - entOgS) / entOS,
# 'cv-OgS' : vulOgS,
# 'cv-SgO' : vulSgO,
# 'dcv' : 0.5 * (vulOgS + vulSgO),
# 'vf-StoO' : (vulOgS - vulO) / (1.0 - vulO),
# 'vf-OtoS' : (vulSgO - vulS) / (1.0 - vulO),
# 'dvf' : 0.5 * ((vulOgS - vulO)/(1.0 - vulO) + (vulSgO - vulS)/(1.0 - vulS))}
return_ = Dist.singleton
singleton = Dist.singleton
flip = Dist.flip
uniform = Dist.uniform
lift = Dist.lift