/
pmonad.py
399 lines (336 loc) · 10.5 KB
/
pmonad.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
""" Probability monad """
import random
from collections import Counter, namedtuple
from math import log, exp
import operator
import functools
import itertools
import sympy
import rfutils
from rfutils.compat import *
INF = float('inf')
_SENTINEL = object()
def keep_calling_forever(f):
return iter(f, _SENTINEL)
# safelog : Float -> Float
def safelog(x):
try:
return log(x)
except ValueError:
return -INF
# logaddexp : Float x Float -> Float
def logaddexp(one, two):
return safelog(exp(one) + exp(two))
# logsumexp : [Float] -> Float
def logsumexp(xs):
return safelog(sum(map(exp, xs)))
# reduce_by_key : (a x a -> a) x [(b, a)] -> {b -> a}
def reduce_by_key(f, keys_and_values):
d = {}
for k, v in keys_and_values:
if k in d:
d[k] = f(d[k], v)
else:
d[k] = v
return d
def lazy_product_map(f, xs):
""" equivalent to itertools.product(*map(f, xs)), but does not hold the values
resulting from map(f, xs) in memory. xs must be a sequence. """
if not xs:
yield []
else:
x = xs[0]
for result in f(x):
for rest in lazy_product_map(f, xs[1:]):
yield [result] + rest
class Monad(object):
def __repr__(self):
return self.__class__.__name__ + "(" + str(self.values) + ")"
def __rshift__(self, f):
return self.bind(f)
def __add__(self, bindee_without_arg):
return self.bind(lambda _: bindee_without_arg())
# lift : (a -> b) -> (m a -> m b)
@classmethod
def lift(cls, f):
@functools.wraps(f)
def wrapper(a):
return a.bind(cls.lift_ret(f))
return wrapper
# lift_ret : (a -> b) -> a -> m b
@classmethod
def lift_ret(cls, f):
@functools.wraps(f)
def wrapper(*a, **k):
return cls.ret(f(*a, **k))
return wrapper
@property
def mzero(self):
return type(self)(self.zero)
@classmethod
def guard(cls, truth):
if truth:
return cls.ret(_SENTINEL) # irrelevant value
else:
return cls(cls.zero) # construct mzero
class Amb(Monad):
def __init__(self, values):
self.values = values
zero = []
def sample(self):
return next(iter(self))
def bind(self, f):
return Amb(rfutils.flatmap(f, self.values))
@classmethod
def ret(cls, x):
return cls([x])
def __iter__(self):
return iter(self.values)
# mapM : (a -> Amb b) x [a] -> Amb [b]
@classmethod
def mapM(cls, f, *xss):
return Amb(itertools.product(*map(f, *xss)))
# filterM : (a -> Amb Bool) x [a] -> Amb [a]
@classmethod
def filterM(cls, f, xs):
return cls(itertools.compress(xs, mask) for mask in cls.mapM(f, xs))
# reduceM : (a x a -> Amb a) x [a] -> Amb [a]
@classmethod
def reduceM(cls, f, xs, initial=None):
def do_it(acc, xs):
if not xs:
yield acc
else:
x = xs[0]
xs = xs[1:]
for new_acc in nf(acc, x):
for res in do_it(new_acc, xs):
yield res
xs = tuple(xs)
if initial is None:
return cls(do_it(xs[0], xs[1:]))
else:
return cls(do_it(initial, xs))
def conditional(self, f=None, normalized=True):
if f is None:
f = lambda x: x
class CDict(dict):
def __missing__(d, key):
samples = (y for x, y in map(f, self.values) if x == key)
d[key] = Amb(samples)
return d[key]
return CDict()
def Samples(rf):
return Amb(keep_calling_forever(rf))
Field = namedtuple('Field', ['add', 'sum', 'mul', 'div', 'zero', 'one'])
p_space = Field(operator.add, sum, operator.mul, operator.truediv, 0, 1)
log_space = Field(logaddexp, logsumexp, operator.add, operator.sub, -INF, 0)
class Enumeration(Monad):
def __init__(self,
values,
marginalized=False,
normalized=False):
self.marginalized = marginalized
self.normalized = normalized
self.values = values
if isinstance(values, dict):
self.marginalized = True
self.values = values.items()
self._dict = values
else:
self.values = values
self._dict = None
field = log_space
zero = []
def bind(self, f):
mul = self.field.mul
def gen():
for x, p_x in self.values:
for y, p_y in f(x):
yield y, mul(p_y, p_x)
return type(self)(gen()).marginalize().normalize()
# return : a -> Enum a
@classmethod
def ret(cls, x):
return cls(
[(x, cls.field.one)],
normalized=True,
marginalized=True,
)
def marginalize(self):
if self.marginalized:
return self
else:
# add together probabilities of equal values
result = reduce_by_key(self.field.add, self.values)
# remove zero probability values
zero = self.field.zero
result = {k:v for k, v in result.items() if v != zero}
return type(self)(
result,
marginalized=True,
normalized=self.normalized,
)
def normalize(self):
if self.normalized:
return self
else:
enumeration = list(self)
Z = self.field.sum(p for _, p in enumeration)
div = self.field.div
result = [(thing, div(p, Z)) for thing, p in enumeration]
return type(self)(
result,
marginalized=self.marginalized,
normalized=True,
)
def __iter__(self):
return iter(self.values)
@property
def dict(self):
if self._dict:
return self._dict
else:
self._dict = dict(self.values)
return self._dict
def __getitem__(self, key):
return self.dict[key]
@classmethod
def mapM(cls, ef, *xss):
mul = cls.field.mul
one = cls.field.one
def gen():
for sequence in itertools.product(*map(ef, *xss)):
seq = []
p = one
for thing, p_thing in sequence:
seq.append(thing)
p = mul(p, p_thing)
yield tuple(seq), p
return cls(gen()).marginalize().normalize()
@classmethod
def reduceM(cls, ef, xs, initial=None):
mul = cls.field.mul
one = cls.field.one
def do_it(acc, xs):
if not xs:
yield (acc, one)
else:
the_car = xs[0]
the_cdr = xs[1:]
for new_acc, p in ef(acc, the_car):
for res, p_res in do_it(new_acc, the_cdr):
yield res, mul(p, p_res)
xs = tuple(xs)
if initial is None:
result = do_it(xs[0], xs[1:])
else:
result = do_it(initial, xs)
return cls(result).marginalize().normalize()
def expectation(self, f):
return sum(f(v)*exp(lp) for v, lp in self.values)
def entropy(self):
return -sum(exp(logp)*logp for _, logp in self.normalize()) / log(2)
def conditional(self, f=None, normalized=True):
if f is None:
f = lambda x: x
add = self.field.add
d = {}
for value, p in self.values:
condition, outcome = f(value)
if condition in d:
if outcome in d[condition]:
d[condition][outcome] = add(d[condition][outcome], p)
else:
d[condition][outcome] = p
else:
d[condition] = {outcome: p}
cls = type(self)
if normalized:
return {
k : cls(v).normalize()
for k, v in d.items()
}
else:
return {k: cls(v) for k, v in d.items()}
@classmethod
def flip(cls, p):
def gen():
if p > 0:
yield True, log(p)
if p < 1:
yield False, log(1-p)
return cls(gen(), marginalized=True, normalized=True)
class PSpaceEnumeration(Enumeration):
field = p_space
@classmethod
def flip(cls, p):
def gen():
yield True, p
yield False, 1 - p
return cls(gen(), marginalized=True, normalized=True)
class SymbolicEnumeration(PSpaceEnumeration):
def marginalize(self):
result = super().marginalize()
new_result = {k:sympy.simplify(v) for k, v in result.values}
return type(result)(
new_result,
marginalized=True,
normalized=result.normalized
)
def UniformEnumeration(xs):
xs = list(xs)
N = len(xs)
return Enumeration([(x, -log(N)) for x in xs])
def UniformSamples(xs):
return Samples(lambda: random.choice(xs))
def enumerator(f):
@functools.wraps(f)
def wrapper(*a, **k):
return Enumeration(f(*a, **k))
return wrapper
def pspace_enumerator(f):
@functools.wraps(f)
def wrapper(*a, **k):
return PSpaceEnumeration(f(*a, **k))
return wrapper
def uniform_enumerator(f):
@functools.wraps(f)
def wrapper(*a, **k):
return UniformEnumeration(f(*a, **k))
return wrapper
uniform = uniform_enumerator
deterministic = Enumeration.lift_ret
certainly = Enumeration.ret
def sampler(f):
@functools.wraps(f)
def wrapper(*a, **k):
return Samples(lambda: f(*a, **k))
return wrapper
def enumeration_from_samples(samples, num_samples):
counts = Counter(itertools.islice(samples, None, num_samples))
return Enumeration((k, log(v)) for k, v in counts.items()).normalize()
def enumeration_from_sampling_function(f, num_samples):
samples = iter(f, _SENTINEL)
return enumeration_from_samples(samples, num_samples)
def approx_enumerator(num_samples):
def decorator(f):
@functools.wraps(f)
def wrapper(*a, **k):
sample_f = lambda: f(*a, **k)
return enumeration_from_sampling_function(sample_f, num_samples)
return wrapper
return decorator
# enum_flip :: Float -> Enum Bool
@enumerator
def enum_flip(p):
if p > 0:
yield True, log(p)
if p < 1:
yield False, log(1-p)
@pspace_enumerator
def pspace_flip(p):
if p > 0:
yield True, p
elif p < 1:
yield False, 1 - p