-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributions.py
482 lines (346 loc) · 11.3 KB
/
distributions.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
from __future__ import annotations
# most code here from
# https://github.com/adrianjav/heterogeneous_vaes
from functools import reduce
from typing import Iterable
import torch
import torch.distributions as dist
from torch.distributions import constraints
from torch.distributions.utils import probs_to_logits, logits_to_probs
# from .miscelanea import to_one_hot
def to_one_hot(x, size):
x_one_hot = x.new_zeros(x.size(0), size)
x_one_hot.scatter_(1, x.unsqueeze(-1).long(), 1).float()
return x_one_hot
class Base(object):
def __init__(self):
self._weight = torch.tensor([1.0])
self.arg_constraints = {}
self.size = 1
@property
def weight(self):
return self._weight
@weight.setter
def weight(self, value):
if not isinstance(value, torch.Tensor) and isinstance(value, Iterable):
assert len(value) == 1, value
value = iter(value)
self._weight = value if isinstance(value, torch.Tensor) else torch.tensor([value])
@property
def expanded_weight(self):
return reduce(list.__add__, [[w] * len(self[i].f) for i,w in enumerate(self.weight)])
@property
def parameters(self):
return list(self.dist.arg_constraints.keys())
@property
def real_parameters(self):
return self.real_dist.real_parameters if id(self) != id(self.real_dist) else self.parameters
def __getitem__(self, item):
assert item == 0
return self
def preprocess_data(self, x, mask=None):
return x,
def scale_data(self, x, weight=None):
weight = weight or self.weight
return x * weight
def unscale_data(self, x, weight=None):
weight = weight or self.weight
return x / weight
@property
def f(self):
raise NotImplementedError()
def sample(self, size, etas):
real_params = self.to_real_params(etas)
real_params = dict(zip(self.real_parameters, real_params))
return self.real_dist.dist(**real_params).sample(torch.Size([size]))
def impute(self, etas):
raise NotImplementedError()
# real_params = self.to_real_params(etas)
# real_params = dict(zip(self.real_parameters, real_params))
# return self.real_dist.dist(**real_params).mean
def mean(self, etas):
params = self.to_params(etas)
params = dict(zip(self.parameters, params))
return self.dist(**params).mean
def to_text(self, etas):
params = self.to_real_params(etas)
params = [x.cpu().tolist() for x in params]
params = dict(zip(self.real_parameters, params))
try:
mean = self.mean(etas).item()
except NotImplementedError:
mean = None
return f'{self.real_dist} params={params}' + (f' mean={mean}' if mean is not None else '')
def params_from_data(self, x):
raise NotImplementedError()
def real_params_from_data(self, x):
etas = self.real_dist.params_from_data(x)
return self.real_dist.to_real_params(etas)
@property
def real_dist(self) -> Base:
return self
def to_real_params(self, etas):
return self.to_params(etas)
@property
def num_params(self):
return len(self.arg_constraints)
@property
def size_params(self):
return [1] * self.num_params
@property
def num_suff_stats(self):
return self.num_params
@property
def num_dists(self):
return 1
def log_prob(self, x, etas):
params = self.to_params(etas)
params = dict(zip(self.parameters, params))
return self.dist(**params).log_prob(x)
def real_log_prob(self, x, etas):
real_params = self.to_real_params(etas)
real_params = dict(zip(self.real_parameters, real_params))
return self.real_dist.dist(**real_params).log_prob(x)
@property
def dist(self):
raise NotImplementedError()
def unscale_params(self, etas):
c = torch.ones_like(etas)
for i, f in enumerate(self.f):
c[i].mul_(f(self.expanded_weight[i]).item())
return etas * c
def scale_params(self, etas):
c = torch.ones_like(etas)
for i, f in enumerate(self.f):
c[i].mul_(f(self.expanded_weight[i]).item())
return etas / c
def __str__(self):
raise NotImplementedError()
def to_params(self, etas):
raise NotImplementedError()
def to_naturals(self, params):
raise NotImplementedError()
@property
def is_discrete(self):
raise NotImplementedError()
@property
def is_continuous(self):
return not self.is_discrete
def __rshift__(self, data):
return self.scale_data(data)
def __lshift__(self, etas):
return self.unscale_params(etas)
class Normal(Base):
def __init__(self):
super(Normal, self).__init__()
self.arg_constraints = [
constraints.real, # eta1
constraints.less_than(0) # eta2
]
@property
def is_discrete(self):
return False
@property
def dist(self):
return dist.Normal
@property
def f(self):
return [lambda w: w, lambda w: w**2]
def params_from_data(self, x):
return self.to_naturals([x.mean(), x.std()])
def to_params(self, etas):
eta1, eta2 = etas
return -0.5 * eta1 / eta2, torch.sqrt(-0.5 / eta2)
def to_naturals(self, params):
loc, std = params
eta2 = -0.5 / std ** 2
eta1 = -2 * loc * eta2
return eta1, eta2
def impute(self, etas):
return self.mean(etas)
def __str__(self):
return 'normal'
class LogNormal(Normal):
def scale_data(self, x, weight=None):
weight = self.weight if weight is None else weight
return torch.clamp(torch.pow(x, weight), min=1e-20, max=1e20)
def unscale_data(self, x, weight=None):
weight = self.weight if weight is None else weight
return torch.clamp(torch.pow(x, 1./weight), min=1e-20, max=1e20)
@property
def dist(self):
return dist.LogNormal
def params_from_data(self, x):
return super().params_from_data(torch.log(x))
def sample(self, size, etas):
return torch.clamp(super().sample(size, etas), min=1e-20, max=1e20)
def impute(self, etas):
mu, sigma = self.to_real_params(etas)
return torch.clamp(torch.exp(mu - sigma**2), min=1e-20, max=1e20)
def __str__(self):
return 'lognormal'
class Gamma(Base):
def __init__(self):
super().__init__()
self.arg_constraints = [
constraints.greater_than(-1), # eta1
constraints.less_than(0) # eta2
]
@property
def dist(self):
return dist.Gamma
@property
def f(self):
return [lambda w: torch.ones_like(w), lambda w: w]
@property
def is_discrete(self):
return False
def params_from_data(self, x):
mean, meanlog = x.mean(), x.log().mean()
s = mean.log() - meanlog
shape = (3 - s + ((s-3)**2 + 24*s).sqrt()) / (12 * s)
for _ in range(50):
shape = shape - (shape.log() - torch.digamma(shape) - s) / (1 / shape - torch.polygamma(1, shape))
concentration = shape
rate = shape / mean
eta1 = concentration - 1
eta2 = -rate
return eta1, eta2
def to_params(self, etas):
eta1, eta2 = etas
return eta1 + 1, -eta2
def impute(self, etas):
alpha, beta = self.to_real_params(etas)
return torch.clamp((alpha - 1) / beta, min=0.0)
def __str__(self):
return 'gamma'
class Exponential(Base):
def __init__(self):
super(Exponential, self).__init__()
self.arg_constraints = [
constraints.less_than(0) # eta1
]
@property
def dist(self):
return dist.Exponential
@property
def is_discrete(self):
return False
@property
def f(self):
return [lambda w: w]
def params_from_data(self, x):
mean = x.mean()
return -1 / mean,
def to_params(self, etas):
return -etas[0],
def impute(self, etas):
raise NotImplementedError()
def __str__(self):
return "exponential"
class Bernoulli(Base):
def __init__(self):
super().__init__()
self.size = 2
self.arg_constraints = [
constraints.real
]
@property
def dist(self):
return dist.Bernoulli
@property
def is_discrete(self):
return True
@property
def parameters(self):
return 'logits',
@property
def real_parameters(self):
return 'probs',
def scale_data(self, x, weight=None):
return x
@property
def f(self):
return [lambda w: torch.ones_like(w)]
def params_from_data(self, x):
return probs_to_logits(x.mean(), is_binary=True),
def to_params(self, etas):
return etas[0],
def to_real_params(self, etas):
return logits_to_probs(self.to_params(etas)[0], is_binary=True),
def impute(self, etas):
probs = self.to_real_params(etas)[0]
return (probs >= 0.5).float()
def __str__(self):
return 'bernoulli'
class Poisson(Base):
def __init__(self):
super().__init__()
self.arg_constraints = [
constraints.real
]
@property
def dist(self):
return dist.Poisson
@property
def is_discrete(self):
return True
def scale_data(self, x, weight=None):
return x
@property
def f(self):
return [lambda w: torch.ones_like(w)]
def params_from_data(self, x):
return torch.log(torch.clamp(x.mean(), min=1e-20)),
def to_params(self, etas):
return torch.exp(etas[0]).clamp(min=1e-6, max=1e20), # TODO
def impute(self, etas):
rate = self.to_real_params(etas)[0]
return rate.floor()
def __str__(self):
return 'poisson'
class Categorical(Base):
def __init__(self, size):
super().__init__()
self.arg_constraints = [constraints.real_vector]
self.size = size
@property
def dist(self):
return dist.Categorical
@property
def parameters(self):
return 'logits',
@property
def is_discrete(self):
return True
@property
def real_parameters(self):
return 'probs',
@property
def size_params(self):
return [self.size]
def scale_data(self, x, weight=None):
return x
@property
def f(self):
return [lambda w: torch.ones_like(w)]
def impute(self, etas):
real_params = self.to_real_params(etas)
real_params = dict(zip(self.real_parameters, real_params))
return self.real_dist.dist(**real_params).probs.max(dim=-1)[1]
def params_from_data(self, x):
new_x = to_one_hot(x, self.size)
return probs_to_logits(new_x.sum(dim=0) / x.size(0)),
def mean(self, etas):
raise NotImplementedError()
def to_params(self, etas):
return etas[0],
def to_real_params(self, etas):
return logits_to_probs(self.to_params(etas)[0]),
def __str__(self):
return f'categorical({self.size})'
if __name__ == '__main__':
normal = Normal()
params = (torch.tensor(3), torch.tensor(1))
eta1, eta2 = normal.to_naturals(params)
#print(normal.sample(2, (eta1,eta2)))