-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_est_single_obs.py
1820 lines (1569 loc) · 103 KB
/
param_est_single_obs.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
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import os
#############################
#### POTENTIAL FUNCTIONS ####
#############################
## Gradient of quadratic potential (confinement or interaction)
def grad_quadratic(x, alpha1, alpha2=None):
return alpha1 * x
def grad_theta_grad_quadratic(x, alpha1, alpha2=None):
return x
def grad_x_grad_quadratic(x, alpha1, alpha2=None):
return alpha1
## Gradient of linear potential (null) (confinement or interaction)
def grad_linear(x, alpha1, alpha2=None):
return 0*x
def grad_theta_grad_linear(x, alpha1, alpha2=None):
return 0*x
def grad_x_grad_linear(x, alpha1, alpha2=None):
return 0*x
## Gradient of bi-stable (Landau) potential (confinement or interaction)
def grad_bi_stable(x, alpha1, alpha2=None):
return alpha1 * (x ** 3 - x)
def grad_theta_grad_bi_stable(x, alpha1, alpha2=None):
return x**3 - x
def grad_x_grad_bi_stable(x, alpha1, alpha2=None):
return alpha1 * (3 * x ** 2 - 1)
## Gradient of sine (Kuramoto) potential (interaction)
def grad_kuramoto(x, alpha1, alpha2=None):
return alpha1 * np.sin(x)
def grad_theta_grad_kuramoto(x, alpha1, alpha2=None):
return np.sin(x)
def grad_x_grad_kuramoto(x, alpha1, alpha2=None):
return alpha1 * np.cos(x)
## Fitzhugh-Nagumo potential (confinement)
def grad_fitzhugh(x, y, alpha, alpha2=None):
return alpha * (1 / 3 * x ** 3 - x + y)
def grad_theta_grad_fitzhugh(x, y, alpha, alpha2=None):
return 1 / 3 * x ** 3 - x + y
def grad_x1_grad_fitzhugh(x, y, alpha, alpha2=None):
return alpha * (x ** 2 - 1)
def grad_x2_grad_fitzhugh(x, y, alpha, alpha2=None):
return alpha
## Cucker-Smale potential (interaction)
def grad_cucker_smale(x, v, alpha1, alpha2):
num = alpha1 * v
denom = (1 + x ** 2) ** alpha2
return num / denom
def grad_theta1_grad_cucker_smale(x, v, alpha1, alpha2):
num = v
denom = (1 + x ** 2) ** alpha2
return num / denom
def grad_theta2_grad_cucker_smale(x, v, alpha1, alpha2):
num = - alpha1 * np.log((1 + x ** 2)) * v
denom = (1 + x ** 2) ** alpha2
return num / denom
def grad_x1_grad_cucker_smale(x, v, alpha1, alpha2):
num = - 2 * alpha1 * alpha2 * (x) * v
denom = (1 + x ** 2) ** (alpha2 + 1)
return num / denom
def grad_x2_grad_cucker_smale(x, v, alpha1, alpha2):
num = alpha1
denom = (1 + x ** 2) ** alpha2
return num / denom
## Stochastic volatility potential
def grad_stochastic_volatility(x, alpha1, alpha2):
return x * (alpha1 * x - alpha2)
def grad_theta1_grad_stochastic_volatility(x, alpha1, alpha2):
return x * x
def grad_theta2_grad_stochastic_volatility(x, alpha1, alpha2):
return - x
def grad_x_grad_stochastic_volatility(x, alpha1, alpha2):
return 2 * alpha1 * x - alpha2
## Stochastic opinion dynamics
EPS = .1
def opinion_dynamics_kernel(r, alpha):
if -alpha < r < alpha:
return np.exp(EPS - EPS / (1 - (r / alpha) ** 2))
else:
return 0
def grad_opinion_dynamics(x, alpha1, alpha2):
r = abs(x)
if -alpha2 < r < alpha2:
return alpha1 * opinion_dynamics_kernel(r, alpha2) * x
else:
return 0
def grad_theta1_grad_opinion_dynamics(x, alpha1, alpha2):
r = abs(x)
if -alpha2 < r < alpha2:
return opinion_dynamics_kernel(r, alpha2) * x
else:
return 0
def grad_theta2_grad_opinion_dynamics(x, alpha1, alpha2):
r = abs(x)
if -alpha2 < r < alpha2:
return 2 * EPS * alpha1 / (alpha2 ** 3) * r ** 2 * 1 / ((1 - r ** 2 / (alpha2 ** 2)) ** 2) * opinion_dynamics_kernel(r, alpha2) * x
else:
return 0
## gradient of double sine potential
def grad_double_kuramoto(x, alpha1, alpha2):
return alpha1 * np.sin(x) + alpha2 * np.sin(2*x)
def grad_theta1_grad_double_kuramoto(x, alpha1, alpha2):
return np.sin(x)
def grad_theta2_grad_double_kuramoto(x, alpha1, alpha2):
return np.sin(2*x)
#############################
#######################
#### IPS SIMULATOR ####
#######################
## Simulate the IPS with N particles, and output a single observation
## We use this as an approximation to the MVSDE, given that in most cases we cannot simulate
## the MVSDE directly (other than the linear case; see linear_mvsde.py)
## Inputs:
## -> N (number of particles)
## -> T (length of simulation)
## -> grad_v (gradient of confinement potential)
## -> alpha (parameter for confinement potential)
## -> grad_w (gradient of interaction potential, optional)
## -> beta (paramater for interaction potential, optional)
## -> Aij (interaction matrix, optional)
## -> Lij (laplacian matrix), optional)
## -> sigma (noise magnitude)
## -> x0 (initial value)
## -> dt (time step)
## -> seed (random seed)
## Outputs:
## -> x_t^{i} = x_t^{1} (a single particle from the IPS)
def sde_sim_func(N=20, T=100, grad_v=grad_quadratic, alpha=1, grad_w=grad_quadratic, beta=0.1, Aij=None, Lij=None,
sigma=1, x0=1, dt=0.1, seed=1, kuramoto=False, fitzhugh=False, y0=None, gamma=None, cucker_smale=False,
v0=None, beta2=None, stochastic_volatility=False, alpha2=None, all_particles=False,
opinion_dynamics=False, double_kuramoto=False):
# check inputs
if fitzhugh:
assert y0 is not None
assert gamma is not None
assert grad_v == grad_fitzhugh
assert grad_w == grad_quadratic
if cucker_smale:
assert v0 is not None
assert beta2 is not None
assert grad_v == grad_quadratic
assert grad_w == grad_cucker_smale
if kuramoto:
assert grad_w == grad_kuramoto
if stochastic_volatility:
assert alpha2 is not None
assert grad_v == grad_stochastic_volatility
assert grad_w == grad_quadratic
if opinion_dynamics:
assert beta2 is not None
assert grad_v == grad_linear
assert grad_w == grad_opinion_dynamics
if double_kuramoto:
assert beta2 is not None
assert grad_w == grad_double_kuramoto
# set random seed
np.random.seed(seed)
# number of time steps
nt = int(np.round(T / dt))
# parameters
if type(alpha) is float or type(alpha) is int:
alpha = [alpha] * (nt + 1)
if type(beta) is float or type(beta) is int:
beta = [beta] * (nt + 1)
if fitzhugh:
if type(gamma) is int or type(gamma) is float:
gamma = [gamma] * (nt+1)
if cucker_smale or opinion_dynamics or double_kuramoto:
if type(beta2) is int or type(beta2) is float:
beta2 = [beta2] * (nt+1)
if stochastic_volatility:
if type(alpha2) is int or type(alpha2) is float:
alpha2 = [alpha2] * (nt+1)
if stochastic_volatility:
if type(sigma) is int or type(sigma) is float:
sigma = [sigma] * (nt+1)
# initialise xt
xt = np.zeros((nt + 1, N))
xt[0, :] = x0
dxt = np.zeros((nt, N))
# intialise yt
if fitzhugh:
yt = np.zeros((nt+1, N))
yt[0, :] = y0
dyt = np.zeros((nt, N))
# initialise vt
if cucker_smale:
vt = np.zeros((nt+1, N))
vt[0, :] = v0
dvt = np.zeros((nt, N))
# brownian motion
dwt = np.sqrt(dt) * np.random.randn(nt + 1, N)
# simulate
# if interaction parameter provided
if beta is not None:
if fitzhugh:
for i in tqdm(range(0, nt)):
xt[i + 1, :] = xt[i, :] \
- grad_v(xt[i, :], yt[i, :], alpha[i]) * dt \
- grad_w(xt[i] - np.mean(xt[i, :]), beta[i]) * dt \
+ sigma * dwt[i, :]
yt[i + 1, :] = yt[i, :] + (gamma[i] + xt[i, :]) * dt
dxt = xt[i+1, :] - xt[i, :]
dyt = yt[i+1, :] - yt[i, :]
elif cucker_smale:
for i in tqdm(range(0, nt)):
xt[i + 1, :] = xt[i, :] + vt[i, :] * dt
for j in range(N):
vt[i + 1, j] = vt[i, j] \
- grad_v(xt[i, j], alpha[i]) * dt \
- 1 / N * np.sum(grad_w(xt[i, j] - xt[i, :], vt[i, j] - vt[i, :], beta[i], beta2[i])) * dt \
+ sigma * dwt[i, j]
dxt = xt[i + 1, :] - xt[i, :]
dvt = vt[i + 1, :] - vt[i, :]
elif kuramoto:
for i in tqdm(range(0, nt)):
for j in range(N):
xt[i + 1, j] = xt[i, j] \
- grad_v(xt[i, j], alpha[i]) * dt \
- 1 / N * np.sum(grad_w(xt[i, j] - xt[i, :], beta[i])) * dt \
+ sigma * dwt[i, j]
dxt = xt[i + 1, :] - xt[i, :]
while np.any(xt[i + 1, :] > + np.pi) or np.any(xt[i + 1, :] < - np.pi):
xt[i + 1, np.where(xt[i + 1, :] > +np.pi)] -= 2. * np.pi
xt[i + 1, np.where(xt[i + 1, :] < -np.pi)] += 2. * np.pi
elif double_kuramoto:
for i in tqdm(range(0, nt)):
for j in range(N):
xt[i + 1, j] = xt[i, j] \
- grad_v(xt[i, j], alpha[i]) * dt \
- 1 / N * np.sum(grad_w(xt[i, j] - xt[i, :], beta[i], beta2[i])) * dt \
+ sigma * dwt[i, j]
dxt = xt[i + 1, :] - xt[i, :]
while np.any(xt[i + 1, :] > + np.pi) or np.any(xt[i + 1, :] < - np.pi):
xt[i + 1, np.where(xt[i + 1, :] > +np.pi)] -= 2. * np.pi
xt[i + 1, np.where(xt[i + 1, :] < -np.pi)] += 2. * np.pi
elif stochastic_volatility:
for i in tqdm(range(0, nt)):
xt[i + 1, :] = xt[i, :] \
- grad_v(xt[i, :], alpha[i], alpha2[i]) * dt \
- grad_w(xt[i, :] - np.mean(xt[i, :]), beta[i]) * dt \
+ sigma[i] * xt[i, :] ** 1.5 * dwt[i, :]
dxt = xt[i + 1, :] - xt[i, :]
elif opinion_dynamics:
for i in tqdm(range(0, nt)):
for j in range(N):
xt[i + 1, j] = xt[i, j] \
- grad_v(xt[i, j], alpha[i]) * dt \
- 1 / N * np.sum(np.vectorize(grad_w, otypes=[np.float64])(xt[i, j] - xt[i, :], beta[i], beta2[i])) * dt \
+ sigma * dwt[i, j]
dxt = xt[i + 1, :] - xt[i, :]
else:
if grad_w == grad_quadratic:
for i in tqdm(range(0, nt)):
xt[i + 1, :] = xt[i, :] \
- grad_v(xt[i, :], alpha[i]) * dt \
- grad_w(xt[i, :] - np.mean(xt[i, :]), beta[i]) * dt \
+ sigma * dwt[i, :]
dxt = xt[i + 1, :] - xt[i, :]
if grad_w != grad_quadratic:
for i in tqdm(range(0, nt)):
for j in range(N):
xt[i + 1, j] = xt[i, j] \
- grad_v(xt[i, j], alpha[i]) * dt \
- 1 / N * np.sum(grad_w(xt[i, j] - xt[i, :], beta[i])) * dt \
+ sigma * dwt[i, j]
dxt = xt[i + 1, :] - xt[i, :]
# if in Aij form
if np.any(Aij):
for i in tqdm(range(0, nt)):
for j in range(0, N):
xt[i + 1, j] = xt[i, j] - grad_v(xt[i, j], alpha[i]) * dt \
- np.dot(Aij[j, :],xt[i, j] - xt[i, :]) * dt + sigma * dwt[i, j]
dxt = xt[i + 1, :] - xt[i, :]
# if in Lij form
if np.any(Lij):
for i in tqdm(range(0, nt)):
xt[i + 1, :] = xt[i, :] - grad_v(xt[i, :], alpha[i]) * dt - np.dot(Lij, xt[i, :]) * dt + sigma * dwt[i, :]
dxt = xt[i + 1, :] - xt[i, :]
if all_particles:
if fitzhugh:
return xt, yt
elif cucker_smale:
return xt, vt
else:
return xt
else:
if fitzhugh:
return xt[:, 0], yt[:, 0]
elif cucker_smale:
return xt[:, 0], vt[:, 0]
else:
return xt[:, 0]
#######################
##############################################
#### ONLINE ESTIMATORS (MVSDE LIKELIHOOD) ####
##############################################
## Recursive MLE
## Inputs:
## -> xt (observed path of MVSDE)
## -> grad_v (gradient of confinement potential)
## -> grad_theta_grad_v (gradient of gradient of confinement potential w.r.t theta)
## -> alpha0 (initial parameter for confinement potential)
## -> alpha_true (true parameter for confinement potential)
## -> est_alpha (whether to estimate parameter for confinement potential)
## -> grad_w (gradient of interaction potential, optional)
## -> grad_theta_grad_w (gradient of gradient of interaction potential w.r.t theta)
## -> beta0 (initial paramater for interaction potential)
## -> beta_true (true parameter for interaction potential)
## -> est_beta (whether to estimate parameter for interaction potential)
## -> sigma (noise magnitude)
## -> gamma (learning rate)
## -> N (numnber of synthetic particles to use in the estimator)
## -> seed (random seed)
## -> whether to compute average over particles (estimator 1) or not (estimator 2)
## Outputs:
## -> theta_t (online parameter estimate)
def online_est(xt, dt, grad_v, grad_theta_grad_v, grad_x_grad_v, alpha0, alpha_true, est_alpha, grad_w,
grad_theta_grad_w, grad_x_grad_w, beta0, beta_true, est_beta, sigma, gamma, N=2, seed=1, average=True,
fitzhugh=False, yt=None, grad_x2_grad_v=None, gamma0=None, gamma_true=None, est_gamma=False,
kuramoto=False, cucker_smale=False, vt=None, grad_theta2_grad_w=None, grad_x2_grad_w=None, beta20=None,
beta2_true=None, est_beta2=False, stochastic_volatility=False, grad_theta2_grad_v=None, alpha20=None,
alpha2_true=None, est_alpha2=False, sigma0=None, sigma_true=None, est_sigma=False):
# check inputs
if fitzhugh:
assert yt is not None
assert gamma0 is not None
assert gamma_true is not None
assert grad_v == grad_fitzhugh
assert grad_theta_grad_v == grad_theta_grad_fitzhugh
assert grad_x_grad_v == grad_x1_grad_fitzhugh
assert grad_x2_grad_v == grad_x2_grad_fitzhugh
assert grad_w == grad_quadratic
assert grad_theta_grad_w == grad_theta_grad_quadratic
assert grad_x_grad_w == grad_x_grad_quadratic
if cucker_smale:
assert vt is not None
assert beta20 is not None
assert beta2_true is not None
assert grad_v == grad_quadratic
assert grad_theta_grad_v == grad_theta_grad_quadratic
assert grad_x_grad_v == grad_x_grad_quadratic
assert grad_w == grad_cucker_smale
assert grad_theta_grad_w == grad_theta1_grad_cucker_smale
assert grad_theta2_grad_w == grad_theta2_grad_cucker_smale
assert grad_x_grad_w == grad_x1_grad_cucker_smale
assert grad_x2_grad_w == grad_x2_grad_cucker_smale
if kuramoto:
assert grad_w == grad_kuramoto
assert grad_theta_grad_w == grad_theta_grad_kuramoto
assert grad_x_grad_w == grad_x_grad_kuramoto
if stochastic_volatility:
assert alpha20 is not None
assert alpha2_true is not None
assert sigma0 is not None
assert sigma_true is not None
assert grad_v == grad_stochastic_volatility
assert grad_theta_grad_v == grad_theta1_grad_stochastic_volatility
assert grad_theta2_grad_v == grad_theta2_grad_stochastic_volatility
assert grad_x_grad_v == grad_x_grad_stochastic_volatility
assert grad_w == grad_quadratic
assert grad_theta_grad_w == grad_theta_grad_quadratic
assert grad_x_grad_w == grad_x_grad_quadratic
# averaging func
if average:
def averaging_func(x):
return np.mean(x)
if not average:
def averaging_func(x):
return x[1]
# set random seed
np.random.seed(seed)
# number of time steps
nt = xt.shape[0] - 1
# learning rate
if type(gamma) is float or type(gamma) is int:
all_gamma = [gamma] * (nt + 1)
else:
all_gamma = gamma
# parameters
if type(alpha_true) is float or type(alpha_true) is int:
alpha_true = [alpha_true] * (nt + 1)
if type(beta_true) is float or type(beta_true) is int:
beta_true = [beta_true] * (nt + 1)
if fitzhugh:
if type(gamma_true) is float or type(gamma_true) is int:
gamma_true = [gamma_true] * (nt + 1)
if cucker_smale:
if type(beta2_true) is float or type(beta2_true) is int:
beta2_true = [beta2_true] * (nt + 1)
if stochastic_volatility:
if type(alpha2_true) is float or type(alpha2_true) is int:
alpha2_true = [alpha2_true] * (nt + 1)
if type(sigma_true) is float or type(sigma_true) is int:
sigma_true = [sigma_true] * (nt + 1)
# initialise
alpha_t = np.zeros(nt + 1)
if est_alpha:
alpha_t[0] = alpha0
else:
alpha_t = alpha_true
beta_t = np.zeros(nt + 1)
if est_beta:
beta_t[0] = beta0
else:
beta_t = beta_true
gamma_t = np.zeros(nt + 1)
if fitzhugh:
if est_gamma:
gamma_t[0] = gamma0
else:
gamma_t = gamma_true
beta2_t = np.zeros(nt + 1)
if cucker_smale:
if est_beta2:
beta2_t[0] = beta20
else:
beta2_t = beta2_true
alpha2_t = np.zeros(nt + 1)
if stochastic_volatility:
if est_alpha2:
alpha2_t[0] = alpha20
else:
alpha2_t = alpha2_true
sigma_t = np.zeros(nt + 1)
if est_sigma:
sigma_t[0] = sigma0
else:
sigma_t = sigma_true
tildext1_N = np.zeros((nt + 1, N))
tildext2_N = np.zeros((nt + 1, N))
tildext1_N[0, :] = np.random.normal(0,1,N) # xt[0] * np.ones(N)
tildext2_N[0, :] = np.random.normal(0,1,N) # xt[0] * np.ones(N)
tildeyt1_N = np.zeros((nt + 1, 2, N))
tildeyt1_N[0, :] = np.zeros((2, N))
if fitzhugh:
tildext1_N_y = np.zeros((nt + 1, N))
tildext2_N_y = np.zeros((nt + 1, N))
tildext1_N_y[0, :] = yt[0] * np.ones(N)
tildext2_N_y[0, :] = yt[0] * np.ones(N)
tildeyt1_N = np.zeros((nt + 1, 3, N)) # tangent of x process; now have 3 parameters
tildeyt1_N[0, :] = np.zeros((3, N))
tildeyt1_N_y = np.zeros((nt + 1, 3, N)) # tangent of y process; now have 3 parameters
tildeyt1_N_y[0, :] = np.zeros((3, N))
if cucker_smale:
tildext1_N_v = np.zeros((nt + 1, N))
tildext2_N_v = np.zeros((nt + 1, N))
tildext1_N_v[0, :] = np.random.normal(0,1,N)
tildext2_N_v[0, :] = np.random.normal(0,1,N)
tildeyt1_N = np.zeros((nt + 1, 3, N)) # tangent of x process, three parameters
tildeyt1_N[0, :] = np.zeros((3, N))
tildeyt1_N_v = np.zeros((nt + 1, 3, N)) # tangent of v process, three parameters
tildeyt1_N_v[0, :] = np.zeros((3, N))
if stochastic_volatility:
tildeyt1_N = np.zeros((nt + 1, 3, N)) # tangent of x process w.r.t. theta, three parameters
tildeyt1_N[0, :] = np.zeros((3, N)) # tangent of x process w.r.t. theta, three parameters
dwt1 = np.sqrt(dt) * np.random.randn(nt + 1, N)
dwt2 = np.sqrt(dt) * np.random.randn(nt + 1, N)
# integrate parameter update equations
for i in tqdm(range(0, nt)):
t = i * dt
dxt = xt[i + 1] - xt[i]
if fitzhugh:
dyt = yt[i+1] - yt[i]
if cucker_smale:
dvt = vt[i+1] - vt[i]
# IPS integrated with parameters
if fitzhugh:
tildext1_N[i + 1, :] = tildext1_N[i, :] \
- grad_v(tildext1_N[i, :], tildext1_N_y[i, :], alpha_t[i]) * dt \
- grad_w(tildext1_N[i] - np.mean(tildext1_N[i, :]), beta_t[i]) * dt \
+ sigma * dwt1[i, :]
tildext1_N_y[i + 1, :] = tildext1_N_y[i, :] + (gamma_t[i] + tildext1_N[i, :]) * dt
tildext2_N[i + 1, :] = tildext2_N[i, :] \
- grad_v(tildext2_N[i, :], tildext2_N_y[i, :], alpha_t[i]) * dt \
- grad_w(tildext2_N[i] - np.mean(tildext2_N[i, :]), beta_t[i]) * dt \
+ sigma * dwt2[i, :]
tildext2_N_y[i + 1, :] = tildext2_N_y[i, :] + (gamma_t[i] + tildext2_N[i, :]) * dt
elif cucker_smale:
tildext1_N[i + 1, :] = tildext1_N[i, :] + tildext1_N_v[i, :] * dt
for j in range(N):
tildext1_N_v[i + 1, j] = tildext1_N_v[i, j] \
- grad_v(tildext1_N[i, j], alpha_t[i]) * dt \
- 1 / N * np.sum(grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :], beta_t[i], beta2_t[i])) * dt \
+ sigma * dwt1[i, j]
tildext2_N[i + 1, :] = tildext2_N[i, :] + tildext2_N_v[i, :] * dt
for j in range(N):
tildext2_N_v[i + 1, j] = tildext2_N_v[i, j] \
- grad_v(tildext2_N[i, j], alpha_t[i]) * dt \
- 1 / N * np.sum(grad_w(tildext2_N[i, j] - tildext2_N[i, :], tildext2_N_v[i, j] - tildext2_N_v[i, :], beta_t[i], beta2_t[i])) * dt \
+ sigma * dwt2[i, j]
elif kuramoto:
for j in range(N):
tildext1_N[i + 1, j] = tildext1_N[i, j] \
- grad_v(tildext1_N[i, j], alpha_t[i]) * dt \
- 1 / N * np.sum(grad_w(tildext1_N[i, j] - tildext1_N[i, :], beta_t[i])) * dt \
+ sigma * dwt1[i, j]
while np.any(tildext1_N[i + 1, :] > + np.pi) or np.any(tildext1_N[i + 1, :] < - np.pi):
tildext1_N[i + 1, np.where(tildext1_N[i + 1, :] > +np.pi)] -= 2. * np.pi
tildext1_N[i + 1, np.where(tildext1_N[i + 1, :] < -np.pi)] += 2. * np.pi
tildext2_N[i + 1, j] = tildext2_N[i, j] \
- grad_v(tildext2_N[i, j], alpha_t[i]) * dt \
- 1 / N * np.sum(grad_w(tildext2_N[i, j] - tildext2_N[i, :], beta_t[i])) * dt \
+ sigma * dwt2[i, j]
while np.any(tildext2_N[i + 1, :] > + np.pi) or np.any(tildext2_N[i + 1, :] < - np.pi):
tildext2_N[i + 1, np.where(tildext2_N[i + 1, :] > +np.pi)] -= 2. * np.pi
tildext2_N[i + 1, np.where(tildext2_N[i + 1, :] < -np.pi)] += 2. * np.pi
elif stochastic_volatility:
tildext1_N[i + 1, :] = tildext1_N[i, :] \
- grad_v(tildext1_N[i, :], alpha_t[i], alpha2_t[i]) * dt \
- grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * dt \
+ sigma_t[i] * tildext1_N[i, :] ** 1.5 * dwt1[i, :]
tildext2_N[i + 1, :] = tildext2_N[i, :] \
- grad_v(tildext2_N[i, :], alpha_t[i], alpha2_t[i]) * dt \
- grad_w(tildext2_N[i, :] - np.mean(tildext2_N[i, :]), beta_t[i]) * dt \
+ sigma_t[i] * tildext1_N[i, :] ** 1.5 * dwt2[i, :]
else:
if grad_w == grad_quadratic:
tildext1_N[i + 1, :] = tildext1_N[i, :] \
- grad_v(tildext1_N[i, :], alpha_t[i]) * dt \
- beta_t[i] * (tildext1_N[i, :] - np.mean(tildext1_N[i, :])) * dt \
+ sigma * dwt1[i, :]
tildext2_N[i + 1, :] = tildext2_N[i, :] - grad_v(tildext2_N[i, :], alpha_t[i]) * dt \
- beta_t[i] * (tildext2_N[i, :] - np.mean(tildext2_N[i, :])) * dt \
+ sigma * dwt2[i, :]
if grad_w != grad_quadratic:
for j in range(N):
tildext1_N[i + 1, j] = tildext1_N[i, j] \
- grad_v(tildext1_N[i, j], alpha_t[i]) * dt \
- 1 / N * np.sum(grad_w(tildext1_N[i, j] - tildext1_N[i, :], beta_t[i])) * dt \
+ sigma * dwt1[i, j]
tildext2_N[i + 1, j] = tildext2_N[i, j] \
- grad_v(tildext2_N[i, j], alpha_t[i]) * dt \
- 1 / N * np.sum(grad_w(tildext2_N[i, j] - tildext2_N[i, :], beta_t[i])) * dt \
+ sigma * dwt2[i, j]
# tangent IPS integrated with parameters
if est_alpha:
if fitzhugh:
tildeyt1_N[i + 1, 0, :] = tildeyt1_N[i, 0, :] \
- grad_theta_grad_v(tildext1_N[i, :], tildext1_N_y[i, :], alpha_t[i]) * dt \
- grad_x_grad_v(tildext1_N[i, :], tildext1_N_y[i, :], alpha_t[i]) * tildeyt1_N[i, 0, :] * dt \
- grad_x2_grad_v(tildext1_N[i, :], tildext1_N_y[i, :], alpha_t[i]) * tildeyt1_N_y[i, 0, :] * dt \
- grad_x_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * (tildeyt1_N[i, 0, :] - np.mean(tildeyt1_N[i, 0, :])) * dt
tildeyt1_N_y[i + 1, 0, :] = tildeyt1_N_y[i, 0, :] + tildeyt1_N[i + 1, 0, :] * dt
elif cucker_smale:
tildeyt1_N[i + 1, 0, :] = tildeyt1_N[i, 0, :] + tildeyt1_N_v[i, 0, :] * dt
for j in range(N):
tildeyt1_N_v[i + 1, 0, j] = tildeyt1_N_v[i, 0, j] \
- grad_theta_grad_v(tildext1_N[i, j], alpha_t[i]) * dt \
- grad_x_grad_v(tildext1_N[i, j], alpha_t[i]) * tildeyt1_N[i, 0, j] * dt \
- 1 / N * np.sum(grad_x_grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :],beta_t[i], beta2_t[i]) * (tildeyt1_N[i, 0, j] - tildeyt1_N[i, 0, :])) * dt \
- 1 / N * np.sum(grad_x2_grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :],beta_t[i], beta2_t[i]) * (tildeyt1_N_v[i, 0, j] - tildeyt1_N_v[i, 0, :])) * dt
elif kuramoto:
for j in range(N):
tildeyt1_N[i + 1, 0, j] = tildeyt1_N[i, 0, j] \
- grad_theta_grad_v(tildext1_N[i, j], alpha_t[i]) * dt \
- grad_x_grad_v(tildext1_N[i, j], alpha_t[i]) * tildeyt1_N[i, 0, j] * dt \
- 1 / N * np.sum(grad_x_grad_w(tildext1_N[i, j] - tildext1_N[i, :], beta_t[i]) * (tildeyt1_N[i, 0, j] - tildeyt1_N[1, 0, :])) * dt
elif stochastic_volatility:
tildeyt1_N[i + 1, 0, :] = tildeyt1_N[i, 0, :] \
- grad_theta_grad_v(tildext1_N[i, :], alpha_t[i], alpha2_t[i]) * dt \
- grad_x_grad_v(tildext1_N[i, :], alpha_t[i], alpha2_t[i]) * tildeyt1_N[i, 0, :] * dt \
- grad_x_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * (tildeyt1_N[i, 0, :] - np.mean(tildeyt1_N[i, 0, :])) * dt \
+ 1.5 * sigma_t[i] * tildext1_N[i, :] ** 0.5 * tildeyt1_N[i, 0, :] * dwt1[i, :]
else:
if grad_w == grad_quadratic:
tildeyt1_N[i + 1, 0, :] = tildeyt1_N[i, 0, :] \
- grad_theta_grad_v(tildext1_N[i, :], alpha_t[i]) * dt \
- grad_x_grad_v(tildext1_N[i, :], alpha_t[i], alpha2_t[i]) * tildeyt1_N[i, 0, :] * dt \
- grad_x_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * (tildeyt1_N[i, 0, :] - np.mean(tildeyt1_N[i, 0, :])) * dt
if grad_w != grad_quadratic:
for j in range(N):
tildeyt1_N[i + 1, 0, j] = tildeyt1_N[i, 0, j] \
- grad_theta_grad_v(tildext1_N[i, j], alpha_t[i]) * dt \
- grad_x_grad_v(tildext1_N[i, j], alpha_t[i]) * tildeyt1_N[i, 0, j] * dt \
- 1 / N * np.sum(grad_x_grad_w(tildext1_N[i, j] - tildext1_N[i, :], beta_t[i]) * (tildeyt1_N[i, 0, j] - tildeyt1_N[1, 0, :])) * dt
if est_alpha2:
if stochastic_volatility:
tildeyt1_N[i + 1, 1, :] = tildeyt1_N[i, 1, :] \
- grad_theta2_grad_v(tildext1_N[i, :], alpha_t[i], alpha2_t[i]) * dt \
- grad_x_grad_v(tildext1_N[i, :], alpha_t[i], alpha2_t[i]) * tildeyt1_N[i, 1, :] * dt \
- grad_x_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * (tildeyt1_N[i, 1, :] - np.mean(tildeyt1_N[i, 1, :])) * dt \
+ 1.5 * sigma_t[i] * tildext1_N[i, :] ** 0.5 * tildeyt1_N[i, 1, :] * dwt1[i, :]
if est_beta:
if fitzhugh:
tildeyt1_N[i + 1, 1, :] = tildeyt1_N[i, 1, :] \
- grad_x_grad_v(tildext1_N[i, :], tildext1_N_y[i, :], alpha_t[i]) * tildeyt1_N[i, 1, :] * dt \
- grad_x2_grad_v(tildext1_N[i, :], tildext1_N_y[i, :], alpha_t[i]) * tildeyt1_N_y[i, 1, :] * dt \
- grad_theta_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * dt \
- grad_x_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * (tildeyt1_N[i, 1, :] - np.mean(tildeyt1_N[i, 1, :])) * dt
tildeyt1_N_y[i + 1, 1, :] = tildeyt1_N_y[i, 1, :] + tildeyt1_N[i + 1, 1, :] * dt
elif cucker_smale:
tildeyt1_N[i + 1, 1, :] = tildeyt1_N[i, 1, :] + tildeyt1_N_v[i, 1, :] * dt
for j in range(N):
tildeyt1_N_v[i + 1, 1, j] = tildeyt1_N_v[i, 1, j] \
- grad_x_grad_v(tildext1_N[i, j], alpha_t[i]) * tildeyt1_N[i, 1, j] * dt \
- 1 / N * np.sum(grad_theta_grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :], beta_t[i], beta2_t[i])) * dt \
- 1 / N * np.sum(grad_x_grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]) * (tildeyt1_N[i, 1, j] - tildeyt1_N[i, 1, :])) * dt \
- 1 / N * np.sum(grad_x2_grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]) * (tildeyt1_N_v[i, 1, j] - tildeyt1_N_v[i, 1, :])) * dt
elif kuramoto:
for j in range(N):
tildeyt1_N[i + 1, 1, j] = tildeyt1_N[i, 1, j] \
- grad_x_grad_v(tildext1_N[i, j], alpha_t[i]) * tildeyt1_N[i, 1, j] * dt \
- 1 / N * np.sum(grad_theta_grad_w(tildext1_N[i, j] - tildext1_N[i, :], beta_t[i])) * dt \
- 1 / N * np.sum(grad_x_grad_w(tildext1_N[i, j] - tildext1_N[i, :], beta_t[i]) * (tildeyt1_N[i, 1, j] - tildeyt1_N[1, 1, :])) * dt
elif stochastic_volatility:
tildeyt1_N[i + 1, 2, :] = tildeyt1_N[i, 2, :] \
- grad_x_grad_v(tildext1_N[i, :], alpha_t[i], alpha2_t[i]) * tildeyt1_N[i, 2, :] * dt \
- grad_theta_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * dt \
- grad_x_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * (tildeyt1_N[i, 2, :] - np.mean(tildeyt1_N[i, 2, :])) * dt \
+ 1.5 * sigma_t[i] * tildext1_N[i, :] ** 0.5 * tildeyt1_N[i, 2, :] * dwt1[i, :]
else:
if grad_w == grad_quadratic:
tildeyt1_N[i + 1, 1, :] = tildeyt1_N[i, 1, :] \
- grad_x_grad_v(tildext1_N[i, :], alpha_t[i]) * tildeyt1_N[i, 1, :] * dt \
- grad_theta_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * dt \
- grad_x_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * (tildeyt1_N[i, 1, :] - np.mean(tildeyt1_N[i, 1, :])) * dt
if grad_w != grad_quadratic:
for j in range(N):
tildeyt1_N[i + 1, 1, j] = tildeyt1_N[i, 1, j] \
- grad_x_grad_v(tildext1_N[i, j], alpha_t[i]) * tildeyt1_N[i, 1, j] * dt \
- 1 / N * np.sum(grad_theta_grad_w(tildext1_N[i, j] - tildext1_N[i, :], beta_t[i])) * dt \
- 1 / N * np.sum(grad_x_grad_w(tildext1_N[i, j] - tildext1_N[i, :], beta_t[i]) * (tildeyt1_N[i, 1, j] - tildeyt1_N[1, 1, :])) * dt
if est_beta2:
if cucker_smale:
tildeyt1_N[i + 1, 2, :] = tildeyt1_N[i, 2, :] + tildeyt1_N_v[i, 2, :] * dt
for j in range(N):
tildeyt1_N_v[i + 1, 2, j] = tildeyt1_N_v[i, 2, j] \
- grad_x_grad_v(tildext1_N[i, j], alpha_t[i]) * tildeyt1_N[i, 2, j] * dt \
- 1 / N * np.sum(grad_theta2_grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :], beta_t[i], beta2_t[i])) * dt \
- 1 / N * np.sum(grad_x_grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]) * (tildeyt1_N[i, 2, j] - tildeyt1_N[i, 2, :])) * dt \
- 1 / N * np.sum(grad_x2_grad_w(tildext1_N[i, j] - tildext1_N[i, :], tildext1_N_v[i, j] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]) * (tildeyt1_N_v[i, 2, j] - tildeyt1_N_v[i, 2, :])) * dt
if est_gamma:
if fitzhugh:
tildeyt1_N[i + 1, 2, :] = tildeyt1_N[i, 2, :] \
- grad_x_grad_v(tildext1_N[i, :], tildext1_N_y[i, :], alpha_t[i]) * tildeyt1_N[i, 2, :] * dt \
- grad_x2_grad_v(tildext1_N[i, :], tildext1_N_y[i, :], alpha_t[i]) * tildeyt1_N_y[i, 2, :] * dt \
- grad_x_grad_w(tildext1_N[i, :] - np.mean(tildext1_N[i, :]), beta_t[i]) * (tildeyt1_N[i, 2, :] - np.mean(tildeyt1_N[i, 2, :])) * dt
tildeyt1_N_y[i + 1, 2, :] = tildeyt1_N_y[i, 2, :] + (1 + tildeyt1_N[i+1, 2, :]) * dt
# online parameter updates
# alpha_t
if est_alpha:
if fitzhugh:
alpha_t[i + 1] = alpha_t[i] + all_gamma[i] \
* (-grad_theta_grad_v(xt[i], yt[i], alpha_t[i])
+ averaging_func(tildeyt1_N[i, 0, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* (dxt - (- grad_v(xt[i], yt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
elif cucker_smale:
alpha_t[i + 1] = alpha_t[i] + all_gamma[i] \
* (- grad_theta_grad_v(xt[i], alpha_t[i])
- - averaging_func(tildeyt1_N[i, 0, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], vt[i] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]))
- - averaging_func(tildeyt1_N_v[i, 0, :] * grad_x2_grad_w(xt[i] - tildext1_N[i, :], vt[i] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]))) \
* (dvt - (- grad_v(xt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], vt[i] - tildext2_N_v[i, :], beta_t[i], beta2_t[i]))) * dt)
elif kuramoto:
alpha_t[i + 1] = alpha_t[i] + all_gamma[i] \
* (- grad_theta_grad_v(xt[i], alpha_t[i])
+ averaging_func(tildeyt1_N[i, 0, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* 1 / (sigma ** 2) \
* (dxt - (-grad_v(xt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
elif stochastic_volatility:
alpha_t[i + 1] = alpha_t[i] + all_gamma[i] \
* (- grad_theta_grad_v(xt[i], alpha_t[i], alpha2_t[i])
+ averaging_func(tildeyt1_N[i, 0, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* (dxt - (-grad_v(xt[i], alpha_t[i], alpha2_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
else:
alpha_t[i+1] = alpha_t[i] + all_gamma[i] \
* (- grad_theta_grad_v(xt[i], alpha_t[i])
+ averaging_func(tildeyt1_N[i, 0, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* 1 / (sigma ** 2) \
* (dxt - (-grad_v(xt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
# alpha2_t (stochastic volatility only)
if est_alpha2:
if stochastic_volatility:
alpha2_t[i + 1] = alpha2_t[i] + all_gamma[i] \
* (- grad_theta2_grad_v(xt[i], alpha_t[i], alpha2_t[i])
+ averaging_func(tildeyt1_N[i, 1, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* (dxt - (-grad_v(xt[i], alpha_t[i], alpha2_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
# beta_t
if est_beta:
if fitzhugh:
beta_t[i + 1] = beta_t[i] + all_gamma[i] \
* (- averaging_func(grad_theta_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))
- - averaging_func(tildeyt1_N[i, 1, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* (dxt - (- grad_v(xt[i], yt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
elif cucker_smale:
beta_t[i + 1] = beta_t[i] + all_gamma[i] \
* (- averaging_func(grad_theta_grad_w(xt[i] - tildext1_N[i, :], vt[i] - tildext1_N_v[i, :], beta_true[i], beta2_t[i]))
- - averaging_func(tildeyt1_N[i, 1, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], vt[i] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]))
- - averaging_func(tildeyt1_N_v[i, 1, :] * grad_x2_grad_w(xt[i] - tildext1_N[i, :], vt[i] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]))) \
* (dvt - (- grad_v(xt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], vt[i] - tildext2_N_v[i, :], beta_t[i], beta2_t[i]))) * dt)
elif kuramoto:
beta_t[i + 1] = beta_t[i] + all_gamma[i] \
* (- averaging_func(grad_theta_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))
- - averaging_func(tildeyt1_N[i, 1, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* 1 / (sigma ** 2) * (dxt - (-grad_v(xt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
elif stochastic_volatility:
beta_t[i + 1] = beta_t[i] + (50*all_gamma[i]) \
* (-averaging_func(grad_theta_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))
+ averaging_func(tildeyt1_N[i, 2, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* (dxt - (-grad_v(xt[i], alpha_t[i], alpha2_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
else:
beta_t[i + 1] = beta_t[i] + all_gamma[i] \
* (-averaging_func(grad_theta_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))
+ averaging_func(tildeyt1_N[i, 1, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], beta_t[i]))) \
* 1/(sigma**2) * (dxt - (-grad_v(xt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt)
# beta2_t (cucker-smale only)
if est_beta2:
if cucker_smale:
beta2_t[i+1] = beta2_t[i] + all_gamma[i] \
* (-averaging_func(grad_theta2_grad_w(xt[i] - tildext1_N[i, :], vt[i] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]))
- - averaging_func(tildeyt1_N[i, 2, :] * grad_x_grad_w(xt[i] - tildext1_N[i, :], vt[i] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]))
- - averaging_func(tildeyt1_N_v[i, 2, :] * grad_x2_grad_w(xt[i] - tildext1_N[i, :], vt[i] - tildext1_N_v[i, :], beta_t[i], beta2_t[i]))) \
* (dvt - (- grad_v(xt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], vt[i] - tildext2_N_v[i, :], beta_t[i], beta2_t[i]))) * dt)
# gamma_t (fitzhugh-nagumo only)
if est_gamma:
if fitzhugh:
gamma_t[i + 1] = gamma_t[i] + all_gamma[i] \
* (averaging_func(tildeyt1_N[i, 2, :] * grad_x_grad_w(xt[i]-tildext1_N[i,:], beta_t[i]))) \
* (dxt - (- grad_v(xt[i], yt[i], alpha_t[i]) - averaging_func(grad_w(xt[i] - tildext2_N[i, :], beta_t[i]))) * dt) \
+ gamma \
* (dyt - (gamma_t[i] + xt[i]) * dt)
# sigma_t (stochastic volatility only)
if est_sigma:
if stochastic_volatility:
sigma_t[i + 1] = sigma_t[i] + all_gamma[i] \
* (2 * sigma_t[i] * xt[i] ** 3) \
* (dxt ** 2 - sigma_t[i] ** 2 * xt[i] ** 3 * dt)
if fitzhugh:
return alpha_t, beta_t, gamma_t
elif cucker_smale:
return alpha_t, beta_t, beta2_t
elif stochastic_volatility:
return alpha_t, alpha2_t, beta_t, sigma_t
else:
return alpha_t, beta_t
#######################
##############################################
#### ONLINE ESTIMATORS (MVSDE LIKELIHOOD) ####
##############################################
## Recursive MLE
## Inputs:
## -> xtN (all observed paths of IPS)
## -> grad_v (gradient of confinement potential)
## -> grad_theta_grad_v (gradient of gradient of confinement potential w.r.t theta)
## -> alpha0 (initial parameter for confinement potential)
## -> alpha_true (true parameter for confinement potential)
## -> est_alpha (whether to estimate parameter for confinement potential)
## -> grad_w (gradient of interaction potential, optional)
## -> grad_theta_grad_w (gradient of gradient of interaction potential w.r.t theta)
## -> beta0 (initial paramater for interaction potential)
## -> beta_true (true parameter for interaction potential)
## -> est_beta (whether to estimate parameter for interaction potential)
## -> sigma (noise magnitude)
## -> gamma (learning rate)
## -> N (numnber of synthetic particles to use in the estimator)
## -> seed (random seed)
## -> whether to compute average over particles (estimator in SPA paper) or not (new estimator)
## Outputs:
## -> theta_t (online parameter estimate)
def online_est_ips(xtN, dt, grad_v, grad_theta_grad_v, alpha0, alpha_true,
est_alpha, grad_w, grad_theta_grad_w, beta0, beta_true, est_beta,
sigma, gamma, seed=1, average=True, fitzhugh=False, ytN=None, gamma0=None, gamma_true=None,
est_gamma=False, kuramoto=False, cucker_smale=False, vtN=None,
grad_theta2_grad_w=None, beta20=None, beta2_true=None, est_beta2=False,
stochastic_volatility=False, grad_theta2_grad_v=None, alpha20=None, alpha2_true=None,
est_alpha2=False, sigma0=None, sigma_true=None, est_sigma=False, opinion_dynamics=False,
double_kuramoto=False):
# check inputs
if fitzhugh:
assert ytN is not None
assert gamma0 is not None
assert gamma_true is not None
assert grad_v == grad_fitzhugh
assert grad_theta_grad_v == grad_theta_grad_fitzhugh
assert grad_w == grad_quadratic
assert grad_theta_grad_w == grad_theta_grad_quadratic
if cucker_smale:
assert vtN is not None
assert beta20 is not None
assert beta2_true is not None
assert grad_v == grad_quadratic
assert grad_theta_grad_v == grad_theta_grad_quadratic
assert grad_w == grad_cucker_smale
assert grad_theta_grad_w == grad_theta1_grad_cucker_smale
assert grad_theta2_grad_w == grad_theta2_grad_cucker_smale
if kuramoto:
assert grad_w == grad_kuramoto
assert grad_theta_grad_w == grad_theta_grad_kuramoto
if stochastic_volatility:
assert alpha20 is not None
assert alpha2_true is not None
assert sigma0 is not None
assert sigma_true is not None
assert grad_v == grad_stochastic_volatility
assert grad_theta_grad_v == grad_theta1_grad_stochastic_volatility
assert grad_theta2_grad_v == grad_theta2_grad_stochastic_volatility
assert grad_w == grad_quadratic
assert grad_theta_grad_w == grad_theta_grad_quadratic
if opinion_dynamics:
assert beta20 is not None
assert beta2_true is not None
assert grad_v == grad_linear
assert grad_theta_grad_v == grad_theta_grad_linear
assert grad_w == grad_opinion_dynamics
assert grad_theta_grad_w == grad_theta1_grad_opinion_dynamics
assert grad_theta2_grad_w == grad_theta2_grad_opinion_dynamics
if double_kuramoto:
assert beta20 is not None
assert beta2_true is not None
assert grad_w == grad_double_kuramoto
assert grad_theta_grad_w == grad_theta1_grad_double_kuramoto
assert grad_theta2_grad_w == grad_theta2_grad_double_kuramoto
# averaging func
if average:
def averaging_func1(x):
return np.mean(x)
def averaging_func2(x):
return np.mean(x)
if not average: