-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtwo_relations_anti.py
65 lines (55 loc) · 2.04 KB
/
two_relations_anti.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
# Copyright 2021 MIT Probabilistic Computing Project
# Apache License, Version 2.0, refer to LICENSE.txt
import os
import random
import matplotlib.pyplot as plt
import numpy as np
NOISY=1
prng = random.Random(1)
def flip(p):
return prng.random() < p if NOISY else p > 0.5
P_LO = .1
P_HI = .65
# ===== Synthetic data generation.
items_D1 = [
list(range(0, 300)),
list(range(300, 400)),
]
data_r1 \
= [((i, j), flip(P_LO)) for i in items_D1[0] for j in items_D1[0]] \
+ [((i, j), flip(P_HI)) for i in items_D1[0] for j in items_D1[1]] \
+ [((i, j), flip(P_HI)) for i in items_D1[1] for j in items_D1[0]] \
+ [((i, j), flip(P_LO)) for i in items_D1[1] for j in items_D1[1]] \
data_r2 \
= [((i, j), flip(P_HI)) for i in items_D1[0] for j in items_D1[0]] \
+ [((i, j), flip(P_LO)) for i in items_D1[0] for j in items_D1[1]] \
+ [((i, j), flip(P_LO)) for i in items_D1[1] for j in items_D1[0]] \
+ [((i, j), flip(P_HI)) for i in items_D1[1] for j in items_D1[1]] \
xlabels = {'R1': '$D_1$', 'R2': '$D_1$'}
ylabels = {'R1': '$D_1$', 'R2': ''}
# Plot the synthetic data.
fig, axes = plt.subplots(ncols=2)
for relation, data, ax in [('R1', data_r1, axes[0]), ('R2', data_r2, axes[1])]:
n = max(max(z) for z in items_D1)
X = np.zeros((n+1, n+1))
for (i, j), v in data:
X[i,j] = v
ax.imshow(X, cmap='Greys')
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
ax.set_xlabel(xlabels[relation])
ax.set_ylabel(ylabels[relation], rotation=0, labelpad=10)
ax.set_xticks([])
ax.set_yticks([])
ax.text(.05, .95, '$%s_%s$' % (relation[0], relation[1]),
ha='left', va='top',
transform=ax.transAxes,
bbox={'facecolor': 'red', 'alpha': 1, 'edgecolor':'k'})
figname = os.path.join('assets', 'two_relations_anti.data.png')
fig.set_size_inches((3,1.5))
fig.set_tight_layout(True)
fig.savefig(figname)
print(figname)
# TODO: Compare output for clustering R1 and R2 using:
# - IRM, with a higher-order encoding R': D1 x D1 X R -> {0, 1}
# - HIRM, with a direct encoding of R1 and R2.