-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_th.py
293 lines (242 loc) · 11.1 KB
/
logistic_th.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
from base import *
from utils import *
from dataloader import *
from hypergraph import *
class HyperNSM:
def __init__(self, alpha=10, order_min=2, order_max=2, xi = lambda k: 1 / k):
self.alpha = alpha
self.order_min = order_min
self.order_max = order_max
self.xi = xi
def sample(self, n):
G = Hypergraph()
ranks = n - (1 + np.arange(n))
for order in range(self.order_min, self.order_max + 1):
for edge in itertools.combinations(range(n), order):
edge = np.array(edge)
p_edge = sigmoid(self.xi(order) * generalized_mean(ranks[edge], self.alpha))
if np.random.uniform() <= p_edge:
G.add_simplex_from_nodes(nodes=edge.tolist(), simplex_data = {})
return G, ranks
@staticmethod
def graph_log_likelihood(edge_set, n, M_neg, order_min, order_max, ranks, alpha, xi, negative_samples):
log_likelihood_pos = 0
log_likelihood_neg = 0
for edge in edge_set:
edge_index = np.array(edge)
log_likelihood_pos += np.log(sigmoid(xi(len(edge_index)) * generalized_mean(ranks[edge_index], alpha) / n))
neg_edge_set = set([])
for _ in range(negative_samples):
while True:
order = np.random.choice(np.arange(order_min, order_max + 1), p=M_neg/M_neg.sum())
neg_edge = tuple(sorted(sample_combination(n=n, k=order)))
if neg_edge not in edge_set and neg_edge not in neg_edge_set:
neg_edge_index = np.array(neg_edge)
log_likelihood_neg += np.log(1 - sigmoid(xi(order) * generalized_mean(ranks[neg_edge_index], alpha) / n))
neg_edge_set.add(neg_edge)
break
return log_likelihood_pos + (M_neg.sum() / negative_samples) * log_likelihood_neg
def display_p(self):
if np.isinf(self.alpha):
return '\\infty'
elif np.isinf(-self.alpha):
return '- \\infty'
else:
return self.alpha
def plot_sample(self, n):
G, ranks = self.sample(n)
G = G.clique_decomposition()
A = nx.to_numpy_array(G)
plt.figure(figsize=(10, 10))
plt.imshow(A)
plt.title('Adjacency Matrix for G ~ logstic-TH($p={}$) (Clique-decomposition)'.format(self.display_p()))
plt.xlabel('Ranked Nodes by $\\pi(u)$')
plt.ylabel('Ranked Nodes by $\\pi(u)$')
plt.figure(figsize=(10, 10))
log_rank = np.log(1 + np.arange(A.shape[0]))
log_degree = np.log(1 + A.sum(0))
plt.plot(log_rank, log_degree)
plt.title('Degree Plot')
plt.xlabel('Node Rank by $\\pi(u)$ (log)')
plt.ylabel('Node Degree (log)')
plt.legend()
plt.figure(figsize=(10, 10))
degree_ranks = np.argsort(-log_degree)
for i in range(A.shape[0]):
A[i, :] = A[i, degree_ranks]
for i in range(A.shape[1]):
A[:, i] = A[degree_ranks, i]
log_degree = log_degree[degree_ranks]
plt.imshow(A)
plt.title('Adjacency Matrix for G ~ logstic-TH($p={}$) (Clique-decomposition)'.format(self.display_p()))
plt.xlabel('Ranked Nodes by degree')
plt.ylabel('Ranked Nodes by degree')
plt.figure(figsize=(10, 10))
plt.plot(log_rank, log_degree)
p = np.polyfit(log_rank, log_degree, deg=1)
r2 = np.corrcoef(log_rank, log_degree)[0, 1]
plt.plot(log_rank, log_degree, linewidth=1, label='Realized Degree $R^2 = {}$'.format(round(r2, 2)))
plt.plot(log_rank, p[1] + p[0] * log_rank, linewidth=2, label='Linear Regression')
plt.title('Degree Plot')
plt.xlabel('Node Rank by degree (log)')
plt.ylabel('Node Degree (log)')
plt.legend()
def fit(self, G, p, negative_samples, max_iters=10000, eval_log_likelihood=True):
n = len(G)
assert(p > max(1, self.alpha))
q = p / (p - 1)
x = np.random.uniform(size=n)
edges = G.to_index()
M = G.num_simplices(separate=True)
def fixed_point(x, edges, M, order_min, order_max, alpha, xi):
z = np.zeros((edges.shape[0], edges.shape[1]))
w = np.zeros_like(x)
for i, k in enumerate(range(order_min, order_max + 1)):
for m in range(M[i]):
for j in edges[i, m, :k]:
z[i, m] += x[j]**alpha
z = z**(1 / alpha - 1)
for i, k in enumerate(range(order_min, order_max + 1)):
for m in range(M[i]):
for j in edges[i, m, :k]:
w[j] += xi(k) * z[i, m]
y = (x**(alpha - 1)) * w
return y
x_prev = x
pbar = tqdm(range(max_iters))
for _ in range(max_iters):
y = fixed_point(x, edges, M, self.order_min, self.order_max, self.alpha, self.xi)
x = (y / np.linalg.norm(y, q))**(1 / (p - 1))
pbar.set_description('Error: {}'.format(np.linalg.norm(x - x_prev) / (1e-5 + np.linalg.norm(x_prev))))
pbar.update()
if np.allclose(x, x_prev, rtol=1e-3):
break
else:
x_prev = x
pbar.close()
ranks = np.argsort(-x)
self.ranks = ranks
x = normalize(x)
if eval_log_likelihood:
ll = HyperNSM.graph_log_likelihood(G.to_index(set), len(G), G.num_simplices(separate=True, negate=True), self.order_min, self.order_max, ranks, 10, self.xi, negative_samples=negative_samples)
else:
ll = None
return ll, x, ranks
class LogisticTH:
def __init__(self, alpha=10, order_min=2, order_max=2):
assert(order_min == 2 and order_max == 2)
self.alpha = alpha
self.order_min = order_min
self.order_max = order_max
def sample(self, n):
G = Hypergraph()
ranks = n - (1 + np.arange(n))
for order in range(self.order_min, self.order_max + 1):
for edge in itertools.combinations(range(n), order):
edge = np.array(edge)
p_edge = sigmoid(generalized_mean(ranks[edge], self.alpha) / n)
if np.random.unifomr() <= p_edge:
G.add_simplex_from_nodes(nodes=edge.tolist(), simplex_data = {})
return G, ranks
@staticmethod
def graph_log_likelihood(edge_set, n, M_neg, order_min, order_max, ranks, alpha, negative_samples):
log_likelihood_pos = 0
log_likelihood_neg = 0
for edge in edge_set:
edge_index = np.array(edge)
log_likelihood_pos += np.log(sigmoid(generalized_mean(ranks[edge_index], alpha) / n))
neg_edge_set = set([])
for _ in range(negative_samples):
while True:
order = np.random.choice(np.arange(order_min, order_max + 1), p=M_neg/M_neg.sum())
neg_edge = tuple(sorted(sample_combination(n=n, k=order)))
if neg_edge not in edge_set and neg_edge not in neg_edge_set:
neg_edge_index = np.array(neg_edge)
log_likelihood_neg += np.log(1 - sigmoid(generalized_mean(ranks[neg_edge_index], alpha) / n))
neg_edge_set.add(neg_edge)
break
return log_likelihood_pos + (M_neg.sum() / negative_samples) * log_likelihood_neg
def display_p(self):
if np.isinf(self.alpha):
return '\\infty'
elif np.isinf(-self.alpha):
return '- \\infty'
else:
return self.alpha
def plot_sample(self, n):
G, ranks = self.sample(n)
G = G.clique_decomposition()
A = nx.to_numpy_array(G)
plt.figure(figsize=(10, 10))
plt.imshow(A)
plt.title('Adjacency Matrix for G ~ logstic-TH($p={}$) (Clique-decomposition)'.format(self.display_p()))
plt.xlabel('Ranked Nodes by $\\pi(u)$')
plt.ylabel('Ranked Nodes by $\\pi(u)$')
plt.figure(figsize=(10, 10))
log_rank = np.log(1 + np.arange(A.shape[0]))
log_degree = np.log(1 + A.sum(0))
plt.plot(log_rank, log_degree)
plt.title('Degree Plot')
plt.xlabel('Node Rank by $\\pi(u)$ (log)')
plt.ylabel('Node Degree (log)')
plt.legend()
plt.figure(figsize=(10, 10))
degree_ranks = np.argsort(-log_degree)
for i in range(A.shape[0]):
A[i, :] = A[i, degree_ranks]
for i in range(A.shape[1]):
A[:, i] = A[degree_ranks, i]
log_degree = log_degree[degree_ranks]
plt.imshow(A)
plt.title('Adjacency Matrix for G ~ logstic-TH($p={}$) (Clique-decomposition)'.format(self.display_p()))
plt.xlabel('Ranked Nodes by degree')
plt.ylabel('Ranked Nodes by degree')
plt.figure(figsize=(10, 10))
plt.plot(log_rank, log_degree)
p = np.polyfit(log_rank, log_degree, deg=1)
r2 = np.corrcoef(log_rank, log_degree)[0, 1]
plt.plot(log_rank, log_degree, linewidth=1, label='Realized Degree $R^2 = {}$'.format(round(r2, 2)))
plt.plot(log_rank, p[1] + p[0] * log_rank, linewidth=2, label='Linear Regression')
plt.title('Degree Plot')
plt.xlabel('Node Rank by degree (log)')
plt.ylabel('Node Degree (log)')
plt.legend()
def fit(self, G, p, negative_samples, max_iters=10000, eval_log_likelihood=True):
n = len(G)
assert(p > max(1, self.alpha))
q = p / (p - 1)
x = np.random.uniform(size=n)
order_factorial = np.zeros(self.order_max + 2)
order_factorial[1] = 1
for i in range(2, self.order_max + 1):
order_factorial[i] = order_factorial[i - 1] * i
edges = G.to_index()
M = G.num_simplices(separate=True)
def fixed_point(x, edges, order_factorial, M, order_min, order_max, alpha):
y = np.zeros_like(x)
for i, k in enumerate(range(order_min, order_max + 1)):
for m in range(M[i]):
generalized_mn = generalized_mean(x[edges[i, m, :k]], alpha)
y[edges] += order_factorial[k] / generalized_mn**(alpha - 1)
y *= np.abs(x)**(alpha - 2) * x
return y
x_prev = x
pbar = tqdm(range(max_iters))
for _ in range(max_iters):
y = fixed_point(x, edges, order_factorial, M, self.order_min, self.order_max, self.alpha)
x = np.linalg.norm(y, q)**(1 - q) * np.abs(y)**(q - 2) * y
pbar.set_description('Error: {}'.format(np.linalg.norm(x - x_prev) / (1e-5 + np.linalg.norm(x_prev))))
pbar.update()
if np.allclose(x, x_prev, rtol=1e-3):
break
else:
x_prev = x
pbar.close()
ranks = np.argsort(-x)
self.ranks = ranks
x = normalize(x)
if eval_log_likelihood:
ll = LogisticTH.graph_log_likelihood(G.to_index(set), len(G), G.num_simplices(separate=True, negate=True), self.order_min, self.order_max, ranks, 10, negative_samples=negative_samples)
else:
ll = None
return ll, x, ranks