Repository navigation
Expand file tree
/
Copy pathBOMP.py
More file actions
617 lines (480 loc) · 21.4 KB
/
Copy pathBOMP.py
File metadata and controls
617 lines (480 loc) · 21.4 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
__author__ = 'yannis'
import pandas
import numpy
import numpy.random
import scipy
import graph_tool as gt
import my_graph_tool_add_ons as mygt
###### PARSERS
def read_incidence_matrix_sequence(Bs):
#expects: ordered list of numpy arrays
N = Bs[0].shape[0]
As = []
Ds = []
TLs = dict()
for B in Bs:
A = numpy.dot(B,B.T)
# sparse?
A = A - numpy.diag(A.diagonal())
As.append(A)
Ds.append(A.sum(0))
for i in range(0,N-1):
for j in range(i+1,N):
ij = tuple([i,j])
coocurrences_ij = [A[i,j] for A in As]
occurrences_i = [d[i] for d in Ds]
occurrences_j = [d[j] for d in Ds]
TLs[ij] = TemporalLink(coocurrences_ij,occurrences_i,occurrences_j)
TLs[ij].learn_all_steps()
return TLs, As
def copycat_predictor(OUTPUT):
aijs_pred = pandas.TimeSeries([0] + [OUTPUT.aijs.iloc[t] for t in range(0,len(OUTPUT.index)-1)],index = OUTPUT.index)
pijs_pred = pandas.TimeSeries([0]+[OUTPUT.pijs.iloc[t] for t in range(0,len(OUTPUT.index)-1)],index = OUTPUT.index)
return pandas.DataFrame({'aijs_pred': aijs_pred,
'aijs_sqr_err': (aijs_pred - OUTPUT.aijs)**2,
'pijs_pred': pijs_pred,
'pijs_sqr_err': (pijs_pred - OUTPUT.pijs)**2},
index = OUTPUT.index)
###### MODEL
def p_exp(k,l):
return (1.*k)/(k+l)
def p_var(k,l):
return (1.*k*l)/ ((k+l)**2 * (k+l+1))
def a_exp(k,l,x):
return x*p_exp(k,l)
def a_var(k,l,x):
return (1.*x*k*l*(k+l+x)) / ((k+l)**2 * (k+l+1))
def a_std(k,l,x):
return numpy.sqrt(a_var(k,l,x))
def a_exp_from_observed_opportunities(k,l,x):
return a_exp(k,l,x)
def a_exp_from_predicted_opportunities(k,l,xi,xj = None):
Ep = p_exp(k,l)
if xj is not None:
#return (xi+xj)*(Ep/(1+Ep)) ##### OLD OPPORTUNITIES
return min(xi,xj)*Ep
else:
return xi*Ep
def get_opportunities(aij,xi,xj):
#return xi + xj - aij ##### OLD OPPORTUNITIES
return min(xi,xj)
def sigmoid(x):
return 1 / (1 + numpy.exp(-x))
def watch_and_learn(a_new,xij_new,xi_new,xj_new,kappa,lambd,gamma):
# try a prediction given new xi,xj and kappa, lambda from previous step
a_pred = a_exp_from_predicted_opportunities(kappa,lambd,xi_new,xj_new)
# also do a prediction for pijs
p_pred_mean = p_exp(kappa,lambd)
p_pred_var = p_var(kappa,lambd)
# get prediction variance (expected squared error from predictive distribution)
exp_error = a_var(kappa,lambd,xij_new)
# calculate deviation from prediction
err = error_function(a_new,a_pred) #square error
# calculate gain
gain = gain_function(err,exp_error)
# calculate new gamma
gamma = fusion_function(gain,gamma)
#update hyperparameters
kappa = gamma*kappa + (1-gamma)*a_new
lambd = gamma*lambd + (1-gamma)*(xij_new - a_new)
return kappa,lambd,gamma,a_pred,exp_error,err,gain,p_pred_mean,p_pred_var
def error_function(new_val,predicted_val):
return (1.*new_val - predicted_val)**2
def gain_function(err,exp_error):
return err / exp_error
def fusion_function(gain,gamma): #it cal also take old gamma
return 1/(1+gain) #logistic between 0 and .5
class TemporalLink:
def __init__(self,coocurrences_ij,occurrences_i,occurrences_j,timestamps = None, use_degree_as_opportunities = False):
self.T = len(coocurrences_ij)
if timestamps is None:
self.timestamps = range(0,self.T)
else:
self.timestamps = timestamps
self.DATA = pandas.DataFrame({'aijs': coocurrences_ij,
'xis': occurrences_i,
'xjs': occurrences_j},
index=self.timestamps)
self.PARAMS = pandas.DataFrame({'kappas': [numpy.nan]*self.T,
'lambdas': [numpy.nan]*self.T,
'gammas': [numpy.nan]*self.T},
index=self.timestamps)
self.AUX = pandas.DataFrame({'aijs_pred': [numpy.nan]*self.T,
'aijs_var': [numpy.nan]*self.T,
'sqr_err': [numpy.nan]*self.T,
'gain': [numpy.nan]*self.T,
'pijs_pred':[numpy.nan]*self.T,
'pijs_pred_var': [numpy.nan]*self.T},
index=self.timestamps)
self.use_degree_as_opportunities = use_degree_as_opportunities
self.k0 = .01
self.l0 = 10
self.g0 = .5
self.current_step = -1
self.current_timestamp = -1
def export_black_box(self):
black_box = dict()
black_box['DATA'] = self.DATA
black_box['PARAMS'] = self.PARAMS
black_box['AUX'] = self.AUX
black_box['T'] = self.T
black_box['timestamps'] = self.timestamps
black_box['use_degree_as_opportunities'] = self.use_degree_as_opportunities
black_box['current_step'] = self.current_step
black_box['current_timestamp'] = self.current_timestamp
return black_box
def import_black_box(self,black_box):
self.DATA = black_box['DATA']
self.PARAMS = black_box['PARAMS']
self.AUX = black_box['AUX']
self.T = black_box['T']
self.timestamps = black_box['timestamps']
self.use_degree_as_opportunities = black_box['use_degree_as_opportunities']
self.current_step = black_box['current_step']
self.current_timestamp = black_box['current_timestamp']
#### TIME FUNCTIONS
def increment_time(self):
self.current_step +=1
self.current_timestamp = self.timestamps[self.current_step]
def get_step_from_timestamp(self,timestamp):
return self.timestamps.index(timestamp)
def next_timestamp(self):
if self.current_step!=self.T-1:
return self.timestamps[self.current_step+1]
else:
return numpy.nan
def next_timestamp_of_given(self,timestamp):
if timestamp!=self.timestamps[-1]:
return self.timestamps[self.get_step_from_timestamp(timestamp) +1]
else:
return numpy.nan
def previous_timestamp(self):
if self.current_step!=0:
return self.timestamps[self.current_step-1]
else:
return numpy.nan
def previous_timestamp_of_given(self,timestamp):
if timestamp!=self.timestamps[0]:
return self.timestamps[self.get_step_from_timestamp(timestamp) -1]
else:
return numpy.nan
#### MODEL LEARNING
def learn_one_step(self):
self.increment_time()
# TAKE THE PRIORS -- KAPPA, LAMBDA, GAMMA FROM PREVIOUS STEP
if self.current_step != 0:
kappa = self.PARAMS.kappas[self.previous_timestamp()]
lambd = self.PARAMS.lambdas[self.previous_timestamp()]
gamma = self.PARAMS.gammas[self.previous_timestamp()]
else:
kappa = self.k0
lambd = self.l0
gamma = self.g0
#print '{0}: {1}, kappa_prior: {2}, lambda_prior:{3}, p_pred:{4}'.format(self.current_step,self.current_timestamp,kappa,lambd,p_exp(kappa,lambd))
# TAKE THE NEW OBSERVATIONS -- xi xj from current step
xi_new = self.DATA.xis[self.current_timestamp]
xj_new = self.DATA.xjs[self.current_timestamp]
# IF NOT NAN, POSTERIOR UPDATE
if (not numpy.isnan(xi_new)) and (not numpy.isnan(xj_new)):
a_new = self.DATA.aijs[self.current_timestamp]
if not self.use_degree_as_opportunities:
xij_new = get_opportunities(a_new,xi_new,xj_new)
kappa,lambd,gamma,a_pred,exp_error,err,gain,p_pred_mean,p_pred_var = watch_and_learn(a_new,xij_new,xi_new,xj_new,kappa,lambd,gamma)
else:
kappa,lambd,gamma,a_pred,exp_error,err,gain,p_pred_mean,p_pred_var = watch_and_learn(a_new,xi_new,xi_new,None,kappa,lambd,gamma)
self.AUX.aijs_pred[self.current_timestamp] = a_pred
self.AUX.aijs_var[self.current_timestamp] = exp_error
self.AUX.sqr_err[self.current_timestamp] = err
self.AUX.gain[self.current_timestamp] = gain
self.AUX.pijs_pred[self.current_timestamp] = p_pred_mean
self.AUX.pijs_pred_var[self.current_timestamp] = p_pred_var
self.PARAMS.kappas[self.current_timestamp] = kappa
self.PARAMS.lambdas[self.current_timestamp] = lambd
self.PARAMS.gammas[self.current_timestamp] = gamma
def learn_all_steps(self):
for t in range(self.current_step,self.T-1):
self.learn_one_step()
return self.export_results()
def export_results(self):
return pandas.DataFrame({'aijs':self.DATA.aijs,
'xis':self.DATA.xis,
'xjs':self.DATA.xjs,
'xijs':self.get_all_opportunities(),
'pijs':self.get_all_simple_ratios(),
'pijs_pred':self.AUX.pijs_pred,
'pijs_pred_var':self.AUX.pijs_pred_var,
'aijs_pred': self.AUX.aijs_pred,
'aijs_var': self.AUX.aijs_var,
'sqr_err': self.AUX.sqr_err,
#'gain': self.AUX.gain,
'kappas': self.PARAMS.kappas,
'lambdas':self.PARAMS.lambdas,
'gammas':self.PARAMS.gammas
#'SRs':self.get_all_simple_ratios()
},
index=self.timestamps)
### MODEL PREDICTIONS
def predict_exp_cooccurrences(self):
return self.predict_exp_coocurrences_at_timestamp(self.current_timestamp)
def predict_exp_coocurrences_at_timestamp(self,timestamp):
if timestamp!=self.timestamps[0]:
xi = self.DATA.xis[timestamp]
xj = self.DATA.xjs[timestamp]
kappa = self.PARAMS.kappas[self.previous_timestamp_of_given(timestamp)]
lambd = self.PARAMS.lambdas[self.previous_timestamp_of_given(timestamp)]
if not self.use_degree_as_opportunities:
return a_exp_from_predicted_opportunities(kappa,lambd,xi,xj)
else:
return a_exp_from_predicted_opportunities(kappa,lambd,xi)
else:
return numpy.nan
# def predict_var_cooccurrences(self):
def predict_exp_opportunities(self):
return self.predict_exp_opportunities_at_timestamp(self.current_timestamp)
def predict_exp_opportunities_at_timestamp(self,timestamp):
if timestamp!=self.timestamps[0]:
xi = self.DATA.xis[timestamp]
xj = self.DATA.xjs[timestamp]
if not self.use_degree_as_opportunities:
#kappa = self.PARAMS.kappas[self.previous_timestamp()]
#lambd = self.PARAMS.lambdas[self.previous_timestamp()]
#return xi + xj - a_exp_from_predicted_opportunities(kappa,lambd,xi,xj) ##### OLD OPPORTUNITIES
return get_opportunities(None,xi,xj)
else:
return xi
else:
return numpy.nan
### RETURN PARAMS AND STATISTICS
def get_opportunities_at_step(self,step):
timestamp = self.timestamps[step]
return self.get_opportunities_at_timestamp(timestamp)
def get_opportunities_at_timestamp(self,timestamp):
#aij = self.DATA.aijs[timestamp] OLD OPPORTUNITIES
xi = self.DATA.xis[timestamp]
xj = self.DATA.xjs[timestamp]
return min(xi,xj)
def get_a_pred_std_at_step(self,step):
timestamp = self.timestamps[step]
return self.get_a_pred_std_at_timestamp(timestamp)
def get_a_pred_std_at_timestamp(self,timestamp):
timestamp_prev = self.previous_timestamp_of_given(timestamp)
if numpy.isnan(timestamp_prev):
return 0
else:
return a_std(self.PARAMS.kappas[timestamp_prev],self.PARAMS.lambdas[timestamp_prev],self.PARAMS.xijs[timestamp_prev])
def get_p_exp_at_step(self,step=None):
if step is None:
timestamp = self.current_timestamp
else:
timestamp = self.timestamps[step]
return self.get_p_exp_at_timestamp(timestamp)
def get_p_exp_at_timestamp(self,timestamp=None):
if timestamp is None:
timestamp = self.current_timestamp
return (1.*self.PARAMS.kappas[timestamp])/(self.PARAMS.kappas[timestamp] + self.PARAMS.lambdas[timestamp])
def get_all_p_exp(self):
return (1.*self.PARAMS.kappas)/(self.PARAMS.kappas + self.PARAMS.lambdas)
def get_all_p_var(self):
return (1.*self.PARAMS.kappas*self.PARAMS.lambdas)/ ((self.PARAMS.kappas+self.PARAMS.lambdas)**2 * (self.PARAMS.kappas+self.PARAMS.lambdas+1))
def get_all_opportunities(self):
if self.use_degree_as_opportunities:
return self.DATA.xis
else:
#return self.DATA.xis + self.DATA.xjs - self.DATA.aijs OLD OPPORTUNITES
return [min(self.DATA.loc[i,'xjs'],self.DATA.loc[i,'xis']) for i in self.DATA.index]
def get_all_simple_ratios(self):
return (1.*self.DATA.aijs) / self.get_all_opportunities()
def get_all_dependencies_i_to_j(self):
return (1.*self.DATA.aijs) / self.DATA.xis
def get_all_dependencies_j_to_i(self):
return (1.*self.DATA.aijs) / self.DATA.xjs
##### DRAW SAMPLES FROM THE MODEL
#### TO BE RENAMED AS DRAW PREDICTIVE SAMPLES
def draw_sample_coocurrence(self,timestamp=None,xij = None):
if timestamp is None:
timestamp = self.current_timestamp
if xij is None:
xij = min(self.DATA.xis,self.DATA.xjs)
p = self.draw_sample_dependency(timestamp)
a = numpy.random.binomial(xij,p)
return a
def draw_sample_dependency(self,timestamp=None):
if timestamp is None:
timestamp = self.current_timestamp
previous_timestamp = self.previous_timestamp_of_given(timestamp)
if numpy.isnan(previous_timestamp):
kappa = self.k0
lambd = self.l0
else:
kappa = self.PARAMS.kappas[timestamp]
lambd = self.PARAMS.lambdas[timestamp]
return numpy.random.beta(kappa,lambd)
class TemporalNetwork:
def __init__(self,TLs,node_indices = None):
# expects a dictionary with keys a tuple (i,j) where i,j the labels of the node pair
self.TLs = TLs
if node_indices is None:
node_indices = self.build_internal_node_index(TLs)
self.node_indices = node_indices
self.N = len(node_indices)
self.T = TLs.values()[0].T
self.current_step = -1
self.current_timestamp = -1
self.is_dependency_network_directed = TLs.values()[0].use_degree_as_opportunities
def export_black_box(self):
black_box = dict()
black_box['TLs'] = self.TLs
black_box['node_indices'] = self.node_indices
black_box['N'] = self.N
black_box['T'] = self.T
black_box['current_step'] = self.current_step
black_box['current_timestamp'] = self.current_timestamp
black_box['is_dependency_network_directed'] = self.is_dependency_network_directed
return black_box
def import_black_box(self,black_box):
self.TLs = black_box['TLs']
self.node_indices = black_box['node_indices']
self.N = black_box['N']
self.T = black_box['T']
self.current_step = black_box['current_step']
self.current_timestamp = black_box['current_timestamp']
self.is_dependency_network_directed = black_box['is_dependency_network_directed']
#### TIME FUNCTIONS
def increment_time(self):
self.current_step +=1
self.current_timestamp = self.timestamps[self.current_step]
def get_step_from_timestamp(self,timestamp):
return self.timestamps.index(timestamp)
def next_timestamp(self):
if self.current_step!=self.T-1:
return self.timestamps[self.current_step+1]
else:
return numpy.nan
def next_timestamp_of_given(self,timestamp):
if timestamp!=self.timestamps[-1]:
return self.timestamps[self.get_step_from_timestamp(timestamp) +1]
else:
return numpy.nan
def previous_timestamp(self):
if self.current_step!=0:
return self.timestamps[self.current_step-1]
else:
return numpy.nan
def previous_timestamp_of_given(self,timestamp):
if timestamp!=self.timestamps[0]:
return self.timestamps[self.get_step_from_timestamp(timestamp) -1]
else:
return numpy.nan
###### LEARNING
def learn_one_step(self):
for TL in self.TLs.values():
TL.learn_one_step()
self.current_step = TL.current_step
self.current_timestamp = TL.current_timestamp
def learn_all_steps(self):
for TL in self.TLs.values():
TL.learn_all_steps()
self.current_step = TL.current_step
self.current_timestamp = TL.current_timestamp
######
def draw_sample_as_graph_tool_object(self,Xijs = None,coocurrence_label = 'co_oc',dependency_label = 'SR',timestamp = None):
G = gt.Graph(directed=self.is_dependency_network_directed)
G.vertex_properties['label'] = G.new_vertex_property('string')
G.edge_properties[dependency_label] = G.new_edge_property('float')
if Xijs is not None:
G.edge_properties[coocurrence_label] = G.new_edge_property('int')
G.graph_properties['index_of'] = G.new_graph_property('object')
G.graph_properties['index_of'] = dict()
for node_pair in self.TLs.keys():
TL = self.TLs[node_pair]
ilabel = node_pair[0]
jlabel = node_pair[1]
v = mygt.get_vertex_by_label(G,ilabel)
if v is None:
v = G.add_vertex()
G.vertex_properties['label'][v] = ilabel
G.graph_properties['index_of'][ilabel] = int(v)
u = mygt.get_vertex_by_label(G,jlabel)
if u is None:
u = G.add_vertex()
G.vertex_properties['label'][u] = jlabel
G.graph_properties['index_of'][jlabel] = int(u)
e = G.add_edge(v,u)
G.edge_properties[dependency_label][e] = TL.draw_sample_dependency(timestamp)
if Xijs is not None:
G.edge_properties[coocurrence_label][e] = TL.draw_sample_coocurrence(Xijs[node_pair],timestamp)
return G
def draw_sample_cooccurrence_network_as_adjacency_matrix(self,timestamp=None):
Asample = numpy.zeros((self.N,self.N))
for node_pair in self.TLs.keys():
ilabel = node_pair[0]
jlabel = node_pair[1]
i = self.node_indices[ilabel]
j = self.node_indices[jlabel]
TL = self.TLs[node_pair]
Asample[i,j] = TL.draw_sample_coocurrence(timestamp)
return Asample
def draw_sample_dependency_network_as_adjacency_matrix(self,timestamp=None):
Psample = numpy.zeros((self.N,self.N))
for node_pair in self.TLs.keys():
ilabel = node_pair[0]
jlabel = node_pair[1]
i = self.node_indices[ilabel]
j = self.node_indices[jlabel]
TL = self.TLs[node_pair]
if not self.is_dependency_network_directed:
if j>i:
k=j
j=i
i=k
Psample[i,j] = TL.draw_sample_dependency(timestamp)
Psample[j,i] = Psample[i,j]
else:
Psample[i,j] = TL.draw_sample_dependency(timestamp)
return Psample
def export_p_exp_network_as_adjacency_matrix(self,timestamp = None):
Pexp = numpy.zeros((self.N,self.N))
for node_pair in self.TLs.keys():
ilabel = node_pair[0]
jlabel = node_pair[1]
i = self.node_indices[ilabel]
j = self.node_indices[jlabel]
TL = self.TLs[node_pair]
if not self.is_dependency_network_directed:
if j<i:
k=j
j=i
i=k
Pexp[i,j] = TL.AUX.pijs_pred[timestamp]
Pexp[j,i] = Pexp[i,j]
else:
Pexp[i,j] = TL.AUX.pijs_pred[timestamp]
return Pexp
def export_a_exp_network_as_adjacency_matrix(self,timestamp):
Aexp = numpy.zeros((self.N,self.N))
for node_pair in self.TLs.keys():
ilabel = node_pair[0]
jlabel = node_pair[1]
i = self.node_indices[ilabel]
j = self.node_indices[jlabel]
TL = self.TLs[node_pair]
if j<i:
k=j
j=i
i=k
Aexp[i,j] = TL.AUX.aijs_pred[timestamp]
return Aexp
@staticmethod
def build_internal_node_index(TLs):
node_indices = dict()
node_counter = -1
for node_pair in TLs.keys():
ilabel = node_pair[0]
jlabel = node_pair[1]
if not node_indices.has_key(ilabel):
node_counter+=1
node_indices[ilabel] = node_counter
if not node_indices.has_key(jlabel):
node_counter+=1
node_indices[jlabel] = node_counter
return node_indices