-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_fit_bp.py
1936 lines (1777 loc) · 86.9 KB
/
utils_fit_bp.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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""MULCH fit and log-likelihood functions on block pair level
@author: Hadeel Soliman
"""
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import minimize, approx_fprime
from bisect import bisect_left
import time
import utils_generate_model as generate_bp
#%% 6-alpha diagonal block pair log-likelihood and fit functions
def cal_R_6_alpha_dia_bp(events_dict, betas):
"""
calculate recursive term in log-likelihood of a diagonal block pair (a, a) - 6 excitation types
:param dict events_dict: dictionary of events within block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param betas: (Q,) array of decays
:return: list of recursive function values for each (u, v) in events_dict
"""
Q = len(betas)
Ris = []
for (u, v) in events_dict:
# array of events of node pair (u,v)
uv_events = events_dict[(u, v)]
num_events_uv = len(uv_events)
if num_events_uv == 0:
Ris.append(np.array([0])) # R=0 if node_pair (u,v) has no events
else:
# 6*Q columns (alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr)*Q
Ri = np.zeros((num_events_uv, 6 * Q))
uv_intertimes = (uv_events[1:] - uv_events[:-1])
# (#_uv_events-1, Q) array
e_intertimes_Q = np.zeros((len(uv_intertimes), Q))
for q in range(Q):
e_intertimes_Q[:, q] = np.exp(-betas[q] * uv_intertimes)
for (x, y) in events_dict:
# same node_pair events (alpha_s)
if (u, v) == (x, y):
for k in range(1, num_events_uv):
for q in range(Q):
Ri[k, 0 + q * 6] = e_intertimes_Q[k - 1, q] * (1 + Ri[k - 1, 0 + q * 6])
# reciprocal node_pair events (alpha_r)
elif (v, u) == (x, y):
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 1 + q * 6] = Ri_temp[:, q]
# br node_pairs events (alpha_tc)
elif u == x and v != y:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 2 + q * 6] += Ri_temp[:, q]
# gr node_pairs events (alpha_gr)
elif u == y and v != x:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 3 + q * 6] += Ri_temp[:, q]
# alliance np (alpha_al)
elif v == y and u != x:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 4 + q * 6] += Ri_temp[:, q]
# alliance reciprocal np (alpha_alr)
elif v == x and u != y:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 5 + q * 6] += Ri_temp[:, q]
Ris.append(Ri)
# return list of arrays - list size = #node_pairs_events_in block_pair
return Ris
def LL_6_alpha_dia_bp(params, events_dict, end_time, n_a, m, T_diff_sums=None, Ris=None):
"""
calculate log-likelihood of one diagonal block pair - 6 excitation types
:param tuple params: MULCH block pair parameters (mu, alpha_1, .. alpha_n, C, betas)
:param dict dict events_dict: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param float end_time: duration of the network
:param int n_a: number of nodes in block a
:param int m: number of node pairs in the diagonal block pair
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair log-likelihood
:rtype: float
"""
# C: scaling parameters - same length as betas
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr, C, betas = params
Q = len(betas)
# first term
first = -m * mu * end_time
# block pair has no events (empty)
if len(events_dict) == 0:
return first
# second term
if T_diff_sums is None:
T_diff_sums = cal_diff_sums_Q(events_dict, end_time, betas)
second = -(alpha_s + alpha_r + (alpha_tc + alpha_gr + alpha_al + alpha_alr) * (n_a - 2)) * C @ T_diff_sums
# third term
if Ris is None:
Ris = cal_R_6_alpha_dia_bp(events_dict, betas)
third = 0
for i in range(len(Ris)):
col_sum = np.zeros(Ris[i].shape[0])
for q in range(Q):
col_sum[:] += C[q]*betas[q] * (alpha_s * Ris[i][:, 0 + q * 6] + alpha_r * Ris[i][:, 1 + q * 6] +
alpha_tc * Ris[i][:, 2 + q * 6] + alpha_gr * Ris[i][:,3 + q * 6] +
alpha_al * Ris[i][:, 4 + q * 6] + alpha_alr * Ris[i][:,5 + q * 6])
col_sum += mu
third += np.sum(np.log(col_sum))
log_likelihood_value = first + second + third
# print("ll: ", first, second, third)
return log_likelihood_value
def NLL_6_alpha_dia_bp(p, betas, events_dict, end_time, n_a, m, T_diff_sums, Ris):
"""
negative log-likelihood of one diagonal block pair- 6 excitation type
called by scipy.minimize() function for parameters estimation
:param tuple p: MULCH block pair raveled parameters (mu, alpha_1, .., alpha_6, C_1, .., C_Q )
:param betas: (Q,) array of decays
:param dict events_dict: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param float end_time: duration of the network
:param int n_a: number of nodes in block a
:param int m: number of node pairs in the diagonal block pair
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair negative log-likelihood
:rtype: float
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr = p[:7]
C = np.array(p[7:])
# scaling step - constaint C sums to 1
C_sum = np.sum(C)
if (C_sum != 0):
C = C / C_sum
alpha_s = alpha_s * C_sum
alpha_r = alpha_r * C_sum
alpha_tc = alpha_tc * C_sum
alpha_gr = alpha_gr * C_sum
alpha_al = alpha_al * C_sum
alpha_alr = alpha_alr * C_sum
params = mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr, C, betas
return -LL_6_alpha_dia_bp(params, events_dict, end_time, n_a, m, T_diff_sums, Ris)
def NLL_6_alpha_dia_bp_jac(p, betas, events_dict, end_time, n_a, m, T_diff_sums, Ris):
"""
jacobian of negative log-likelihood of one diagonal block pair- 6 excitations
called by scipy.minimize() function for parameters estimation
:param tuple p: MULCH block pair parameters (mu, alpha_1, .., alpha_6, C_1, .., C_Q )
:param betas: (Q,) array of decays
:param dict events_dict: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param float end_time: duration of the network
:param int n_a: number of nodes in block a
:param int m: number of node pairs in the diagonal block pair
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: jacobian array of negative log-likelihood function with respect to MULCH parameters
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr = p[:7]
C = np.array(p[7:])
Q = len(C)
# derivatives of second term
d_mu = m * end_time
d_alphas = np.zeros(6)
d_alphas[:2] = C @ T_diff_sums
d_alphas[2:] = (n_a - 2) * C @ T_diff_sums
d_C = (alpha_s + alpha_r + (n_a - 2) * (alpha_tc + alpha_gr + alpha_al + alpha_alr)) * T_diff_sums
# derivatives of third term
for i in range(len(Ris)):
denominator = np.zeros(Ris[i].shape[0])
# one column for each alpha_j
numerator_alphas = np.zeros((Ris[i].shape[0], 6))
numerator_C = []
for q in range(Q):
numerator_C.append(betas[q]* (alpha_s * Ris[i][:, 0 + q * 6] + alpha_r * Ris[i][:, 1 + q * 6] +
alpha_tc * Ris[i][:, 2 + q * 6] + alpha_gr * Ris[i][:,3 + q * 6] +
alpha_al * Ris[i][:, 4 + q * 6] + alpha_alr * Ris[i][:,5 + q * 6]))
denominator += C[q] * numerator_C[q]
for j in range(6):
numerator_alphas[:, j] += C[q] * betas[q] * Ris[i][:, j + q * 6]
denominator += mu
d_mu -= np.sum(1 / denominator)
for a in range(6):
d_alphas[a] -= np.sum(numerator_alphas[:, a] / denominator)
for q in range(Q):
d_C[q] -= np.sum(numerator_C[q] / denominator)
return np.hstack((d_mu, d_alphas, d_C))
def fit_6_alpha_dia_bp(events_dict, end_time, n_a, m, betas):
"""
fit mulch one diagonal block pair (a, b) - 6 excitations
:param dict events_dict: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param float end_time: duration of the network
:param int n_a: number of nodes in block a
:param int m: number of node pairs in the diagonal block pair
:param betas: (Q,) array of decays
:return: estimated parameters (mu, alpha_1,.. , alpha_n, C, betas)
:rtype: tuple
"""
Q = len(betas)
# events_dict : (u,v):array_of_events
if len(events_dict) == 0: # handling empty block pair with no events
# (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr, C, betas)
C = np.zeros(Q)
return (1e-10, 0, 0, 0, 0, 0, 0, C, betas)
# calculate fixed terms in log-likelihood
Ris = cal_R_6_alpha_dia_bp(events_dict, betas)
T_diff_sums = cal_diff_sums_Q(events_dict, end_time, betas)
# initialize parameters (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, c1, ..., cQ)
mu_i = np.random.uniform(1e-6, 1e-2)
alpha_s_i, alpha_r_i = np.random.uniform(0.1, 0.5, 2)
alpha_tc_i, alpha_gr_i, alpha_al_i, alpha_alr_i = np.random.uniform(1e-5, 0.1, 4)
mu_alpha_init = [mu_i, alpha_s_i, alpha_r_i, alpha_tc_i, alpha_gr_i, alpha_al_i, alpha_alr_i] # <-- random initialization
# mu_alpha_init = [1e-2, 2e-2, 2e-2, 1e-2, 1e-2, 1e-2, 1e-2] # <-- fixed initialization
C = [1 / Q] * Q # <-- fixed initialization
init_param = tuple(mu_alpha_init + C)
# define bounds
mu_alpha_bo = [(1e-7, None)] * 7
C_bo = [(0, 1)] * Q
bounds = tuple(mu_alpha_bo + C_bo)
# minimize function
# to print optimization details , options={'disp': True}
res = minimize(NLL_6_alpha_dia_bp, init_param, method='L-BFGS-B', bounds=bounds, jac=NLL_6_alpha_dia_bp_jac,
args=(betas, events_dict, end_time, n_a, m, T_diff_sums, Ris), tol=1e-12)
results = res.x
# print("success ", res.success, ", status ", res.status, ", fun value ", res.fun)
# print("message ", res.message)
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr = results[:7]
C = np.array(results[7:])
# scaling step
C_sum = np.sum(C)
if C_sum != 0:
C = C / C_sum
alpha_s = alpha_s * C_sum
alpha_r = alpha_r * C_sum
alpha_tc = alpha_tc * C_sum
alpha_gr = alpha_gr * C_sum
alpha_al = alpha_al * C_sum
alpha_alr = alpha_alr * C_sum
return (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr, C, betas)
#%% 6-alpha off-diagonal block pair log-likelihood and fit functions
def cal_R_6_alpha_off_bp(events_dict, events_dict_r, betas):
"""
calculate recursive term in log-likelihood of one off-diagonal block pair (a, b) - 6 excitations
:param events_dict: dictionary of events within block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param events_dict_r: dictionary of events within reciprocal block pair (b, a)
:param betas: (Q,) array of decays
:return: list of recursive function values for each (u, v) in events_dict
"""
Q = len(betas)
Ris = []
for (u, v) in events_dict:
# array of events of node pair (u,v)
uv_events = events_dict[(u, v)]
num_events_uv = len(uv_events)
if num_events_uv == 0:
Ris.append(np.array([0])) # R=0 if node_pair (u,v) has no events
else:
# 6*Q columns (alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr)*Q
Ri = np.zeros((num_events_uv, 6 * Q))
uv_intertimes = (uv_events[1:] - uv_events[:-1])
# (#_uv_events-1, Q) array
e_intertimes_Q = np.zeros((len(uv_intertimes), Q))
for q in range(Q):
e_intertimes_Q[:, q] = np.exp(-betas[q] * uv_intertimes)
# loop through node pairs in block pair ab
for (x, y) in events_dict:
# same node_pair events (alpha_s)
if (u, v) == (x, y):
for k in range(1, num_events_uv):
for q in range(Q):
Ri[k, 0 + q * 6] = e_intertimes_Q[k - 1, q] * (1 + Ri[k - 1, 0 + q * 6])
# br node_pairs events (alpha_tc)
elif u == x and v != y:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 2 + q * 6] += Ri_temp[:, q]
# alliance np (alpha_al)
elif v == y and u != x:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 4 + q * 6] += Ri_temp[:, q]
# loop through node pairs in reciprocal block pair ba
for (x, y) in events_dict_r:
# reciprocal node_pair events (alpha_r)
if (v, u) == (x, y):
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict_r[(x, y)], betas)
for q in range(Q):
Ri[:, 1 + q * 6] = Ri_temp[:, q]
# gr node_pairs events (alpha_gr)
elif u == y and v != x:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict_r[(x, y)], betas)
for q in range(Q):
Ri[:, 3 + q * 6] += Ri_temp[:, q]
# alliance reciprocal np (alpha_alr)
elif v == x and u != y:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict_r[(x, y)], betas)
for q in range(Q):
Ri[:, 5 + q * 6] += Ri_temp[:, q]
Ris.append(Ri)
# return list of arrays - list size = #node_pairs_events_in block_pair
return Ris
def LL_6_alpha_off_bp(params, ed, ed_r, end_time, n_b, m_ab, T_diff_sums=None, T_diff_sums_r=None, Ris=None):
"""
calculate log-likelihood of one off-diagonal block pair - 6 excitation types
:param tuple params: MULCH block pair parameters (mu, alpha_1, .. alpha_n, C, betas)
:param dict ed: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param dict ed_r: dictionary of events within reciprocal block pair (b, a)
:param float end_time: duration of the network
:param int n_b: number of nodes in block b
:param int m_ab: number of node pairs in off-diagonal block pair (a, b)
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair log-likelihood
:rtype: float
"""
# C: scaling parameters - same length as betas
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr, C, betas = params
Q = len(betas)
N_a = m_ab // n_b
##first term
first = -m_ab * mu * end_time
##second term
if T_diff_sums is None:
T_diff_sums = cal_diff_sums_Q(ed, end_time, betas)
T_diff_sums_r = cal_diff_sums_Q(ed_r, end_time, betas)
second = -(alpha_s + alpha_tc * (n_b - 1) + alpha_al * (N_a - 1)) * C @ T_diff_sums
second -= (alpha_r + alpha_gr * (n_b - 1) + alpha_alr * (N_a - 1)) * C @ T_diff_sums_r
##third term
if Ris is None:
Ris = cal_R_6_alpha_off_bp(ed, ed_r, betas)
third = 0
for i in range(len(Ris)):
col_sum = np.zeros(Ris[i].shape[0])
for q in range(Q):
col_sum[:] += C[q] * betas[q] * (alpha_s * Ris[i][:, 0 + q * 6] + alpha_r * Ris[i][:, 1 + q * 6] +
alpha_tc * Ris[i][:, 2 + q * 6] + alpha_gr * Ris[i][:, 3 + q * 6] +
alpha_al * Ris[i][:, 4 + q * 6] + alpha_alr * Ris[i][:,5 + q * 6])
col_sum += mu
third += np.sum(np.log(col_sum))
log_likelihood_value = first + second + third
return log_likelihood_value
def NLL_6_alpha_off_bp(p, betas, ed, ed_r, end_time, n_b, m_ab, T_diff_sums, T_diff_sums_r, Ris):
"""
negative log-likelihood of an off-diagonal block pair (a,b) - 6 excitation type
called by scipy.minimize() function for parameters estimation
:param tuple p: MULCH block pair raveled parameters (mu, alpha_1, .., alpha_6, C_1, .., C_Q )
:param betas: (Q,) array of decays
:param dict ed: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param dict ed_r: dictionary of events within reciprocal block pair (b, a)
:param float end_time: duration of the network
:param int n_b: number of nodes in block b
:param int m_ab: number of node pairs in off-diagonal block pair (a, b)
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair negative log-likelihood
:rtype: float
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr = p[:7]
C = np.array(p[7:])
# scaling step - constaint C sums to 1
C_sum = np.sum(C)
if (C_sum != 0):
C = C / C_sum
alpha_s = alpha_s * C_sum
alpha_r = alpha_r * C_sum
alpha_tc = alpha_tc * C_sum
alpha_gr = alpha_gr * C_sum
alpha_al = alpha_al * C_sum
alpha_alr = alpha_alr * C_sum
params = mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr, C, betas
return -LL_6_alpha_off_bp(params, ed, ed_r, end_time, n_b, m_ab, T_diff_sums, T_diff_sums_r, Ris)
def NLL_6_alpha_off_bp_jac(p, betas, ed, ed_r, end_time, n_b, m_ab, T_diff_sums, T_diff_sums_r, Ris):
"""
jacobian of negative log-likelihood of an off-diagonal block pair- 6 excitations
called by scipy.minimize() function for parameters estimation
:param tuple p: MULCH block pair raveled parameters (mu, alpha_1, .., alpha_6, C_1, .., C_Q )
:param betas: (Q,) array of decays
:param dict ed: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param dict ed_r: dictionary of events within reciprocal block pair (b, a)
:param float end_time: duration of the network
:param int n_b: number of nodes in block b
:param int m_ab: number of node pairs in off-diagonal block pair (a, b)
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair negative log-likelihood
:rtype: float
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr = p[:7]
C = np.array(p[7:])
Q = len(C)
N_a = m_ab // n_b
# derivatives of second term
d_mu = m_ab * end_time
d_alphas = np.zeros(6)
d_alphas[0] = C @ T_diff_sums
d_alphas[1] = C @ T_diff_sums_r
d_alphas[2] = (n_b - 1) * C @ T_diff_sums
d_alphas[3] = (n_b - 1) * C @ T_diff_sums_r
d_alphas[4] = (N_a - 1) * C @ T_diff_sums
d_alphas[5] = (N_a - 1) * C @ T_diff_sums_r
d_C = (alpha_s + alpha_tc * (n_b - 1) + alpha_al * (N_a - 1)) * T_diff_sums \
+ (alpha_r + alpha_gr * (n_b - 1) + alpha_alr * (N_a - 1)) * T_diff_sums_r
# derivatives of third term
for i in range(len(Ris)):
denominator = np.zeros(Ris[i].shape[0])
# one column for each alpha_j
numerator_alphas = np.zeros((Ris[i].shape[0], 6))
numerator_C = []
for q in range(Q):
numerator_C.append(betas[q]*( alpha_s * Ris[i][:, 0 + q * 6] + alpha_r * Ris[i][:, 1 + q * 6] +
alpha_tc * Ris[i][:, 2 + q * 6] + alpha_gr * Ris[i][:,3 + q * 6] +
alpha_al * Ris[i][:, 4 + q * 6] + alpha_alr * Ris[i][:,5 + q * 6]))
denominator += C[q] * numerator_C[q]
for j in range(6):
numerator_alphas[:, j] += C[q] * betas[q] * Ris[i][:, j + q * 6]
denominator += mu
d_mu -= np.sum(1 / denominator)
for a in range(6):
d_alphas[a] -= np.sum(numerator_alphas[:, a] / denominator)
for q in range(Q):
d_C[q] -= np.sum(numerator_C[q] / denominator)
return np.hstack((d_mu, d_alphas, d_C))
def fit_6_alpha_off_bp(ed, ed_r, end_time, n_b, m_ab, betas):
"""
fit mulch one off-diagonal block pair (a, b) - 6 excitations
:param dict ed: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param dict ed_r: dictionary of events within reciprocal block pair (b, a)
:param float end_time: duration of the network
:param int n_b: number of nodes in block b
:param int m_ab: number of node pairs in off-diagonal block pair (a, b)
:param betas: (Q,) array of decays
:return: estimated parameters (mu, alpha_1,.. , alpha_n, C, betas)
:rtype: tuple
"""
Q = len(betas)
# events_dict : (u,v):array_of_events
if len(ed) == 0: # handling empty block pair with no events
# (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr, C, betas)
C = np.zeros(Q)
return (1e-10, 0, 0, 0, 0, 0, 0, C, betas)
# calculate fixed terms in log-likelihood
Ris = cal_R_6_alpha_off_bp(ed, ed_r, betas)
T_diff_sums = cal_diff_sums_Q(ed, end_time, betas)
T_diff_sums_r = cal_diff_sums_Q(ed_r, end_time, betas)
# initialize parameters
# mu_alpha_init = [1e-2, 2e-2, 2e-2, 1e-2, 1e-2] # <-- fixed initialization
mu_i = np.random.uniform(1e-6, 1e-2)
alpha_s_i, alpha_r_i = np.random.uniform(0.1, 0.5, 2)
alpha_tc_i, alpha_gr_i, alpha_al_i, alpha_alr_i = np.random.uniform(1e-5, 0.1, 4)
mu_alpha_init = [mu_i, alpha_s_i, alpha_r_i, alpha_tc_i, alpha_gr_i, alpha_al_i, alpha_alr_i] # <-- random initialization
C = [1 / Q] * Q # <-- fixed initialization
init_param = tuple(mu_alpha_init + C)
# define bounds
mu_alpha_bo = [(1e-7, None)] * 7
C_bo = [(0, 1)] * Q
bounds = tuple(mu_alpha_bo + C_bo)
# minimize function
# options = {'disp': True}
res = minimize(NLL_6_alpha_off_bp, init_param, method='L-BFGS-B', bounds=bounds, jac=NLL_6_alpha_off_bp_jac,
args=(betas, ed, ed_r, end_time, n_b, m_ab, T_diff_sums, T_diff_sums_r, Ris), tol=1e-12)
results = res.x
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr = results[:7]
C = np.array(results[7:])
# scaling step -
C_sum = np.sum(C)
if C_sum != 0:
C = C / C_sum
alpha_s = alpha_s * C_sum
alpha_r = alpha_r * C_sum
alpha_tc = alpha_tc * C_sum
alpha_gr = alpha_gr * C_sum
alpha_al = alpha_al * C_sum
alpha_alr = alpha_alr * C_sum
return (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, alpha_al, alpha_alr, C, betas)
#%% 4-alpha diagonal block pair log-likelihood and fit functions
def cal_R_4_alpha_dia_bp(events_dict, betas):
"""
calculate recursive term in log-likelihood of a diagonal block pair (a, a) - 4 excitation types
:param dict events_dict: dictionary of events in a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param betas: (Q,) array of decays
:return: list of recursive function values for each (u, v) in events_dict
"""
Q = len(betas)
Ris = []
for (u, v) in events_dict:
# array of events of node pair (u,v)
uv_events = events_dict[(u, v)]
num_events_uv = len(uv_events)
if num_events_uv == 0:
Ris.append(np.array([0])) # R=0 if node_pair (u,v) has no events
else:
# 4*Q columns (alpha_s, alpha_r, alpha_tc, alpha_gr)*Q
Ri = np.zeros((num_events_uv, 4 * Q))
uv_intertimes = (uv_events[1:] - uv_events[:-1])
# (#_uv_events-1, Q) array
e_intertimes_Q = np.zeros((len(uv_intertimes), Q))
for q in range(Q):
e_intertimes_Q[:, q] = np.exp(-betas[q] * uv_intertimes)
for (x, y) in events_dict:
if x == u or y == u:
prev_index = 0
# same node_pair events (alpha_s)
if (u, v) == (x, y):
for k in range(1, num_events_uv):
for q in range(Q):
Ri[k, 0 + q * 4] = e_intertimes_Q[k - 1, q] * (1 + Ri[k - 1, 0 + q * 4])
# reciprocal node_pair events (alpha_r)
elif (v, u) == (x, y):
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 1 + q * 4] = Ri_temp[:, q]
# br node_pairs events (alpha_tc)
elif u == x and v != y:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 2 + q * 4] += Ri_temp[:, q]
# gr node_pairs events (alpha_gr)
elif u == y and v != x:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 3 + q * 4] += Ri_temp[:, q]
Ris.append(Ri)
# return list of arrays - list size = #node_pairs_events_in block_pair
return Ris
def LL_4_alpha_dia_bp(params, events_dict, end_time, n_a, m, T_diff_sums=None, Ris=None):
"""
calculate log-likelihood of one diagonal block pair - 4 excitation types
:param tuple params: MULCH block pair parameters (mu, alpha_1, .. alpha_n, C, betas)
:param dict dict events_dict: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param float end_time: duration of the network
:param int n_a: number of nodes in block a
:param int m: number of node pairs in the diagonal block pair
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair log-likelihood
:rtype: float
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, C, betas = params
Q = len(betas)
##first term
first = -m * mu * end_time
##second term
if T_diff_sums is None:
events_array = list(events_dict.values())
# if block pair has no events
if len(events_array) == 0:
return first
T_diff = end_time - np.concatenate(events_array)
T_diff_sums = np.zeros(Q, )
for q in range(Q):
T_diff_sums[q] = np.sum(1 - np.exp(-betas[q] * T_diff))
second = -(alpha_s + alpha_r + (alpha_tc + alpha_gr) * (n_a - 2)) * C @ T_diff_sums
##third term
if Ris is None:
Ris = cal_R_4_alpha_dia_bp(events_dict, betas)
third = 0
for i in range(len(Ris)):
col_sum = np.zeros(Ris[i].shape[0])
for q in range(Q):
col_sum[:] += C[q]*betas[q] * (alpha_s * Ris[i][:, 0 + q * 4] + alpha_r * Ris[i][:, 1 + q * 4] +
alpha_tc * Ris[i][:, 2 + q * 4] + alpha_gr * Ris[i][:,3 + q * 4])
col_sum += mu
third += np.sum(np.log(col_sum))
log_likelihood_value = first + second + third
return log_likelihood_value
def NLL_4_alpha_dia_bp(p, betas, events_dict, end_time, n_a, m, T_diff_sums, Ris):
"""
negative log-likelihood of one diagonal block pair- 4 excitation type
called by scipy.minimize() function for parameters estimation
:param tuple p: MULCH block pair raveled parameters (mu, alpha_1, .., alpha_6, C_1, .., C_Q )
:param betas: (Q,) array of decays
:param dict events_dict: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param float end_time: duration of the network
:param int n_a: number of nodes in block a
:param int m: number of node pairs in the diagonal block pair
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair negative log-likelihood
:rtype: float
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr = p[:5]
C = np.array(p[5:])
# scaling step - constaint C sums to 1
C_sum = np.sum(C)
if (C_sum != 0):
C = C / C_sum
alpha_s = alpha_s * C_sum
alpha_r = alpha_r * C_sum
alpha_tc = alpha_tc * C_sum
alpha_gr = alpha_gr * C_sum
params = mu, alpha_s, alpha_r, alpha_tc, alpha_gr, C, betas
return -LL_4_alpha_dia_bp(params, events_dict, end_time, n_a, m, T_diff_sums, Ris)
def NLL_4_alpha_dia_bp_jac(p, betas, events_dict, end_time, n_a, m, T_diff_sums, Ris):
"""
jacobian of negative log-likelihood of one diagonal block pair- 4 excitations
called by scipy.minimize() function for parameters estimation
:param tuple p: MULCH block pair parameters (mu, alpha_1, .., alpha_6, C_1, .., C_Q )
:param betas: (Q,) array of decays
:param dict events_dict: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param float end_time: duration of the network
:param int n_a: number of nodes in block a
:param int m: number of node pairs in the diagonal block pair
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: jacobian array of negative log-likelihood function with respect to MULCH parameters
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr = p[:5]
C = np.array(p[5:])
Q = len(C)
# derivatives of second term
d_mu = m * end_time
d_alpha_s = C @ T_diff_sums
d_alpha_r = C @ T_diff_sums
d_alpha_tc = (n_a - 2) * C @ T_diff_sums
d_alpha_gr = (n_a - 2) * C @ T_diff_sums
d_C = (alpha_s + alpha_r + (n_a - 2) * (alpha_tc + alpha_gr)) * T_diff_sums
# derivatives of third term
for i in range(len(Ris)):
denominator = np.zeros(Ris[i].shape[0])
# one column for each alpha_j
numerator_alphas = np.zeros((Ris[i].shape[0], 4))
numerator_C = []
for q in range(Q):
numerator_C.append(betas[q]* (alpha_s * Ris[i][:, 0 + q * 4] + alpha_r * Ris[i][:, 1 + q * 4] +
alpha_tc * Ris[i][:, 2 + q * 4] + alpha_gr * Ris[i][:,3 + q * 4]))
denominator += C[q] * numerator_C[q]
for j in range(4):
numerator_alphas[:, j] += C[q] * betas[q] * Ris[i][:, j + q * 4]
denominator += mu
d_mu -= np.sum(1 / denominator)
d_alpha_s -= np.sum(numerator_alphas[:, 0] / denominator)
d_alpha_r -= np.sum(numerator_alphas[:, 1] / denominator)
d_alpha_tc -= np.sum(numerator_alphas[:, 2] / denominator)
d_alpha_gr -= np.sum(numerator_alphas[:, 3] / denominator)
for q in range(Q):
d_C[q] -= np.sum(numerator_C[q] / denominator)
return np.hstack((d_mu, d_alpha_s, d_alpha_r, d_alpha_tc, d_alpha_gr, d_C))
def fit_4_alpha_dia_bp(events_dict, end_time, n_a, m, betas):
"""
fit mulch one diagonal block pair (a, b) - 4 excitations
:param dict events_dict: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param float end_time: duration of the network
:param int n_a: number of nodes in block a
:param int m: number of node pairs in the diagonal block pair
:param betas: (Q,) array of decays
:return: estimated parameters (mu, alpha_1,.. , alpha_n, C, betas)
:rtype: tuple
"""
Q = len(betas)
# events_dict : (u,v):array_of_events
if len(events_dict) == 0: # handling empty block pair with no events
# (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, C, betas)
C = np.zeros(Q)
return (1e-10, 0, 0, 0, 0, C, betas)
# calculate fixed terms in log-likelihood
Ris = cal_R_4_alpha_dia_bp(events_dict, betas)
events_array = list(events_dict.values())
T_diff = end_time - np.concatenate(events_array)
T_diff_sums = np.zeros(Q, )
for q in range(Q):
T_diff_sums[q] = np.sum(1 - np.exp(-betas[q] * T_diff))
# initialize parameters (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, c1, ..., cQ)
mu_i = np.random.uniform(1e-6, 1e-2)
alpha_s_i, alpha_r_i = np.random.uniform(0.1, 0.5, 2)
alpha_tc_i, alpha_gr_i = np.random.uniform(1e-5, 0.1, 2)
mu_alpha_init = [mu_i, alpha_s_i, alpha_r_i, alpha_tc_i, alpha_gr_i] # <-- random initialization
# mu_alpha_init = [1e-2, 2e-2, 2e-2, 1e-2, 1e-2] # <-- fixed initialization
C = [1 / Q] * Q # <-- fixed initialization
init_param = tuple(mu_alpha_init + C)
# define bounds
mu_alpha_bo = [(1e-7, None)] * 5
C_bo = [(0, 1)] * Q
bounds = tuple(mu_alpha_bo + C_bo)
# minimize function
res = minimize(NLL_4_alpha_dia_bp, init_param, method='L-BFGS-B', bounds=bounds, jac=NLL_4_alpha_dia_bp_jac,
args=(betas, events_dict, end_time, n_a, m, T_diff_sums, Ris), tol=1e-12)
results = res.x
mu, alpha_s, alpha_r, alpha_tc, alpha_gr = results[:5]
C = np.array(results[5:])
# scaling step
C_sum = np.sum(C)
if C_sum != 0:
C = C / C_sum
alpha_s = alpha_s * C_sum
alpha_r = alpha_r * C_sum
alpha_tc = alpha_tc * C_sum
alpha_gr = alpha_gr * C_sum
return (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, C, betas)
#%% 4-alpha off-diagonal block pair log-likelihood and fit functions
def cal_R_4_alpha_off_bp(events_dict, events_dict_r, betas):
"""
calculate recursive term in log-likelihood of one off-diagonal block pair (a, b) - 4 excitations
:param events_dict: dictionary of events within block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param events_dict_r: dictionary of events within reciprocal block pair (b, a)
:param betas: (Q,) array of decays
:return: list of recursive function values for each (u, v) in events_dict
"""
Q = len(betas)
Ris = []
for (u, v) in events_dict:
# array of events of node pair (u,v)
uv_events = events_dict[(u, v)]
num_events_uv = len(uv_events)
if num_events_uv == 0:
Ris.append(np.array([0])) # R=0 if node_pair (u,v) has no events
else:
# 4*Q columns (alpha_s, alpha_r, alpha_tc, alpha_gr)*Q
Ri = np.zeros((num_events_uv, 4 * Q))
uv_intertimes = (uv_events[1:] - uv_events[:-1])
# (#_uv_events-1, Q) array
e_intertimes_Q = np.zeros((len(uv_intertimes), Q))
for q in range(Q):
e_intertimes_Q[:, q] = np.exp(-betas[q] * uv_intertimes)
# loop through node pairs in block pair ab
for (x, y) in events_dict:
if x == u:
prev_index = 0
# same node_pair events (alpha_s)
if (u, v) == (x, y):
for k in range(1, num_events_uv):
for q in range(Q):
Ri[k, 0 + q * 4] = e_intertimes_Q[k - 1, q] * (1 + Ri[k - 1, 0 + q * 4])
# br node_pairs events (alpha_tc)
else:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict[(x, y)], betas)
for q in range(Q):
Ri[:, 2 + q * 4] += Ri_temp[:, q]
# loop through node pairs in reciprocal block pair ba
for (x, y) in events_dict_r:
if y == u:
# reciprocal node_pair events (alpha_r)
if (v, u) == (x, y):
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict_r[(x, y)], betas)
for q in range(Q):
Ri[:, 1 + q * 4] = Ri_temp[:, q]
# gr node_pairs events (alpha_gr)
else:
Ri_temp = get_Ri_temp_Q(uv_events, e_intertimes_Q, events_dict_r[(x, y)], betas)
for q in range(Q):
Ri[:, 3 + q * 4] += Ri_temp[:, q]
Ris.append(Ri)
# return list of arrays - list size = #node_pairs_events_in block_pair
return Ris
def LL_4_alpha_off_bp(params, ed, ed_r, end_time, n_b, m_ab, T_diff_sums=None, T_diff_sums_r=None, Ris=None):
"""
calculate log-likelihood of one off-diagonal block pair - 4 excitation types
:param tuple params: MULCH block pair parameters (mu, alpha_1, .. alpha_n, C, betas)
:param dict ed: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param dict ed_r: dictionary of events within reciprocal block pair (b, a)
:param float end_time: duration of the network
:param int n_b: number of nodes in block b
:param int m_ab: number of node pairs in off-diagonal block pair (a, b)
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair log-likelihood
:rtype: float
"""
# C: scaling parameters - same length as betas
mu, alpha_s, alpha_r, alpha_tc, alpha_gr, C, betas = params
Q = len(betas)
##first term
first = -m_ab * mu * end_time
##second term
if T_diff_sums is None:
events_array = list(ed.values())
events_array_r = list(ed_r.values())
T_diff_sums = np.zeros(Q, )
T_diff_sums_r = np.zeros(Q, )
if len(events_array) != 0:
T_diff = end_time - np.concatenate(events_array)
for q in range(Q):
T_diff_sums[q] = np.sum(1 - np.exp(-betas[q] * T_diff))
if len(events_array_r) != 0:
T_diff = end_time - np.concatenate(events_array_r)
for q in range(Q):
T_diff_sums_r[q] = np.sum(1 - np.exp(-betas[q] * T_diff))
second = -(alpha_s + alpha_tc * (n_b - 1)) * C @ T_diff_sums
second -= (alpha_r + alpha_gr * (n_b - 1)) * C @ T_diff_sums_r
##third term
if Ris is None:
Ris = cal_R_4_alpha_off_bp(ed, ed_r, betas)
third = 0
for i in range(len(Ris)):
col_sum = np.zeros(Ris[i].shape[0])
for q in range(Q):
col_sum[:] += C[q] * betas[q] * (alpha_s * Ris[i][:, 0 + q * 4] + alpha_r * Ris[i][:, 1 + q * 4] +
alpha_tc * Ris[i][:, 2 + q * 4] + alpha_gr * Ris[i][:, 3 + q * 4])
col_sum += mu
third += np.sum(np.log(col_sum))
log_likelihood_value = first + second + third
return log_likelihood_value
def NLL_4_alpha_off_bp(p, betas, ed, ed_r, end_time, n_b, m_ab, T_diff_sums, T_diff_sums_r, Ris):
"""
negative log-likelihood of an off-diagonal block pair (a,b) - 4 excitation type
called by scipy.minimize() function for parameters estimation
:param tuple p: MULCH block pair raveled parameters (mu, alpha_1, .., alpha_6, C_1, .., C_Q )
:param betas: (Q,) array of decays
:param dict ed: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param dict ed_r: dictionary of events within reciprocal block pair (b, a)
:param float end_time: duration of the network
:param int n_b: number of nodes in block b
:param int m_ab: number of node pairs in off-diagonal block pair (a, b)
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair negative log-likelihood
:rtype: float
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr = p[:5]
C = np.array(p[5:])
# scaling step - constaint C sums to 1
C_sum = np.sum(C)
if (C_sum != 0):
C = C / C_sum
alpha_s = alpha_s * C_sum
alpha_r = alpha_r * C_sum
alpha_tc = alpha_tc * C_sum
alpha_gr = alpha_gr * C_sum
params = mu, alpha_s, alpha_r, alpha_tc, alpha_gr, C, betas
return -LL_4_alpha_off_bp(params, ed, ed_r, end_time, n_b, m_ab, T_diff_sums, T_diff_sums_r, Ris)
def NLL_4_alpha_off_bp_jac(p, betas, ed, ed_r, end_time, n_b, m_ab, T_diff_sums, T_diff_sums_r, Ris):
"""
jacobian of negative log-likelihood of an off-diagonal block pair- 4 excitations
called by scipy.minimize() function for parameters estimation
:param tuple p: MULCH block pair raveled parameters (mu, alpha_1, .., alpha_6, C_1, .., C_Q )
:param betas: (Q,) array of decays
:param dict ed: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param dict ed_r: dictionary of events within reciprocal block pair (b, a)
:param float end_time: duration of the network
:param int n_b: number of nodes in block b
:param int m_ab: number of node pairs in off-diagonal block pair (a, b)
:param T_diff_sums: Optional (Q,) array(float)
:param Ris: Optional list(size of events_dict)
:return: block pair negative log-likelihood
:rtype: float
"""
mu, alpha_s, alpha_r, alpha_tc, alpha_gr = p[:5]
C = np.array(p[5:])
Q = len(C)
# derivatives of second term
d_mu = m_ab * end_time
d_alpha_s = C @ T_diff_sums
d_alpha_r = C @ T_diff_sums_r
d_alpha_tc = (n_b - 1) * C @ T_diff_sums
d_alpha_gr = (n_b - 1) * C @ T_diff_sums_r
d_C = (alpha_s + (n_b - 1) * alpha_tc) * T_diff_sums + (alpha_r + (n_b - 1) * alpha_gr) * T_diff_sums_r
# derivatives of third term
for i in range(len(Ris)):
denominator = np.zeros(Ris[i].shape[0])
# one column for each alpha_j
numerator_alphas = np.zeros((Ris[i].shape[0], 4))
numerator_C = []
for q in range(Q):
numerator_C.append(betas[q]*( alpha_s * Ris[i][:, 0 + q * 4] + alpha_r * Ris[i][:, 1 + q * 4] +
alpha_tc * Ris[i][:, 2 + q * 4] + alpha_gr * Ris[i][:,3 + q * 4]))
denominator += C[q] * numerator_C[q]
for j in range(4):
numerator_alphas[:, j] += C[q] * betas[q] * Ris[i][:, j + q * 4]
denominator += mu
d_mu -= np.sum(1 / denominator)
d_alpha_s -= np.sum(numerator_alphas[:, 0] / denominator)
d_alpha_r -= np.sum(numerator_alphas[:, 1] / denominator)
d_alpha_tc -= np.sum(numerator_alphas[:, 2] / denominator)
d_alpha_gr -= np.sum(numerator_alphas[:, 3] / denominator)
for q in range(Q):
d_C[q] -= np.sum(numerator_C[q] / denominator)
return np.hstack((d_mu, d_alpha_s, d_alpha_r, d_alpha_tc, d_alpha_gr, d_C))
def fit_4_alpha_off_bp(ed, ed_r, end_time, n_b, m_ab, betas):
"""
fit mulch one off-diagonal block pair (a, b) - 4 excitations
:param dict ed: dictionary of events within a block pair (a, b),
where {(u, v) node pair in (a, b) : [t1, t2, ..] array of events between (u, v)}
:param dict ed_r: dictionary of events within reciprocal block pair (b, a)
:param float end_time: duration of the network
:param int n_b: number of nodes in block b
:param int m_ab: number of node pairs in off-diagonal block pair (a, b)
:param betas: (Q,) array of decays
:return: estimated parameters (mu, alpha_1,.. , alpha_n, C, betas)
:rtype: tuple
"""
Q = len(betas)
# events_dict : (u,v):array_of_events
if len(ed) == 0: # handling empty block pair with no events
# (mu, alpha_s, alpha_r, alpha_tc, alpha_gr, C, betas)
C = np.zeros(Q)
return (1e-10, 0, 0, 0, 0, C, betas)
# calculate fixed terms in log-likelihood
Ris = cal_R_4_alpha_off_bp(ed, ed_r, betas)
events_array = list(ed.values())
events_array_r = list(ed_r.values())
T_diff_sums = np.zeros(Q, )
T_diff_sums_r = np.zeros(Q, )
if len(events_array) != 0:
T_diff = end_time - np.concatenate(events_array)
for q in range(Q):
T_diff_sums[q] = np.sum(1 - np.exp(-betas[q] * T_diff))
if len(events_array_r) != 0:
T_diff = end_time - np.concatenate(events_array_r)
for q in range(Q):
T_diff_sums_r[q] = np.sum(1 - np.exp(-betas[q] * T_diff))
# initialize parameters
# mu_alpha_init = [1e-2, 2e-2, 2e-2, 1e-2, 1e-2] # <-- fixed initialization
mu_i = np.random.uniform(1e-6, 1e-2)
alpha_s_i, alpha_r_i = np.random.uniform(0.1, 0.5, 2)
alpha_tc_i, alpha_gr_i = np.random.uniform(1e-5, 0.1, 2)
mu_alpha_init = [mu_i, alpha_s_i, alpha_r_i, alpha_tc_i, alpha_gr_i] # <-- random initialization
C = [1 / Q] * Q # <-- fixed initialization
init_param = tuple(mu_alpha_init + C)
# define bounds
mu_alpha_bo = [(1e-7, None)] * 5
C_bo = [(0, 1)] * Q
bounds = tuple(mu_alpha_bo + C_bo)
# minimize function
res = minimize(NLL_4_alpha_off_bp, init_param, method='L-BFGS-B', bounds=bounds, jac=NLL_4_alpha_off_bp_jac,
args=(betas, ed, ed_r, end_time, n_b, m_ab, T_diff_sums, T_diff_sums_r, Ris), tol=1e-12)
results = res.x
mu, alpha_s, alpha_r, alpha_tc, alpha_gr = results[:5]
C = np.array(results[5:])