-
Notifications
You must be signed in to change notification settings - Fork 0
/
compass_em_e.m
1657 lines (1604 loc) · 66.9 KB
/
compass_em_e.m
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
function [rXSmt,rSSmt,Param,rXPos,rSPos,ML,EYn,EYb,rYn,rYb]=compass_em(DISTR,Uk,In,Ib,Yn,Yb,Param,obs_valid)
%% Here, We assume each observation is scalar
%% Input Argument
% DISTR, a vecotr of two variables. The [1 0] means there is only normal
% observation/s, [0 1] means there is only binary observation/s, and [1 1]
% will be both observations.
% Uk: is a matrix of size KxS1 - K is the length of observation - input to
% State model - X(k)=A*X(k-1)+B*Uk+Wk
% In: is a matrix of size KxS3 - K is the length of observation - input to Normal observation model
% Yn(k)=(C.*MCk)*X(k)+(D.*MDk)*In+Vk - C and D are free parameters,
% and MCk and MDk are input dependent components
% Ib: is a matrix of size KxS5 - K is the length of observation - input to Binary observation model
% P(Yb(k)==1)=sigmoid((E.*MEk)*X(k)+(F.*MFk)*Ib - E and F are free parameters,
% and MEk and MFk are input dependent components
% Yn: is a matrix of size KxN - K is the length of observation, matrix of
% normal observation
% Yb: is a matrix of size KxN - K is the length of observation, matrix of
% binary observation
% Param: it keeps the model information, and paramaters
%% Output Argument
% XSmt is the smoothing result - mean
% SSmt is the smoothing result - variance
% Param is the updated model parameters
% XPos is the filtering result - mean
% SPos is the filtering result - variance
% ML is the value of E-step maximization
% EYn is the prdiction of the Yn
% EYb is not added yet, but it can be the prediction of binary probability
%% obs_valid has three values (0= MAR, 1=observed, 2= censored)
EPS = realmin('single');
MAX_EXP = 100;
update_mode=Param.UpdateMode;
%% Observation Mode, from 1 to 7
if DISTR(1)==1
observe_mode = DISTR(1) + 2*DISTR(2);
elseif DISTR(1)==2
observe_mode = 2*DISTR(1) + DISTR(2);
%% Yalda:
elseif DISTR(1)==3
observe_mode = 2*DISTR(1) + DISTR(2);
else
observe_mode = 2*DISTR(2);
end
%% Build Mask Ck, Dk ,EK and Fk - note that Ck, Ek are time dependent and the Dk and Fk is linked to a subset of Input
[MCk,MDk] = compass_Tk(In,Param);
%% Yalda:
if DISTR(2)==1
[MEk,MFk] = compass_Qk(Ib,Param);
end
%% State Space Model (X(k+1)= Ak*X(k) + Bk*Uk + Wk*iid white noise )
% ------------------
% X(k) is the state, and Uk is the input
% Ak, Bk, Wk are model paramateres
% ------------------
% Ak, MxM matrix (M is the length of the X)
Ak = Param.Ak;
% Bk, MxS1 matrix (S1 is the length of Uk, Uk is a vector of size S1x1)
Bk = Param.Bk;
% Wk, is MxS2 matrix (S2 is the length of Noise, we normally set the noise with the same dimension as the X - S2=M)
Wk = Param.Wk;
% X0, is a Mx1 matrix (initial value of X0)
X0 = Param.X0;
W0 = Param.W0;
% Unobserved Noise Scale
ws = Param.ws;
% This is extending x
xM = Param.xM;
%% Censored Reaction Time
if ~isempty(find(obs_valid==2))
censor_time = Param.censor_time;
Yn(find(obs_valid~=1))= censor_time;
end
%% Normal/Gamms Observation Model
if DISTR(1)> 0
% For Normal, Y(k)=(Ck.*Tk)*X(k)+Dk*Ik + Vk Vk variance of iid white noise
% For Gamma, Y(k)=exp((Ck.*Tk)*X(k)+Dk*Ik) Vk is dispersion term
% ------------------
% Y(k) is the observation, and Ik is the input either indicator or continuous
% Ck, Dk, Vk are the model paramateres
% Tk is model specific function - it is original set to but a one matrix
% ------------------
% Ck, 1xM matrix - (Y is an scalar observation at each time point ... - The Tk has the same size of input,
% and it is specfically designed for our task. It can be set to all 1 matrix)
Ck = Param.Ck;
% Bk, NxS3 matrix - (We have an input of the length S3, and Dk will be size of NxS3)
Dk = Param.Dk.*MDk;
% Vk, is scaler represnting noise in Normal or Dispresion Term in Gamma
Vk = Param.Vk;
% Length of data
K = length(Yn);
end
%% Binary Observation Model (P(k)=sigmoid((Ek.*Qk)*X(k)+Fk*Ik) )
%% Yalda:
if DISTR(2)==1
% ------------------
% P(k) is the observation probability at time k, and Ik is the input either indicator or continuous
% Ek, and Fk are the model paramateres
% Qk is model specific function - it is original set to but a one matrix
% ------------------
% Ck, NxM matrix - similar to Ck, Tk
Ek = Param.Ek;
% Fk, NxS5 matrix - Similar to Dk
Fk = Param.Fk.*MFk;
% Length of data
K = length(Yb);
end
%% Gamma, Extra Parameter - Time Shift
if DISTR(1)==2
S = Param.S;
end
%% Point Process and marked Point Process, Extra Parameter - Time Resolution
%% Yalda:
if DISTR(1)==3
dt = Param.dt;
end
%% Check Uk
if isempty(Uk)
Uk= zeros(K,size(Bk,2));
end
%% EM Update Loop
% Model Prediction
EYn = [];
EYb = [];
if DISTR(1)
EYn = zeros(K,1);
end
if DISTR(2)
EYb = zeros(K,1);
end
% ML array
ML = [];
Prv_ML = - 1e-10;
% Main Loop
for iter=1:Param.Iter
% display iter
disp(['iteration ' num2str(iter) ' out of ' num2str(Param.Iter)])
%% Run the Filtering Part
% One step perdiction mean
XPre = cell(K,1);
% One step perdiction covariance
SPre = cell(K,1);
% Filter mean
XPos = cell(K,1);
% Filter covariance
SPos = cell(K,1);
% Filter
for k=1:K
% One step prediction
if k == 1
XPre{k} = Ak * X0 + Bk * Uk(k,:)';
SPre{k} = Ak * W0 * Ak'+ ws^(1-obs_valid(k))*Wk;
else
XPre{k} = Ak * XPos{k-1} + Bk * Uk(k,:)';
SPre{k} = Ak * SPos{k-1}* Ak' + ws^(1-obs_valid(k))* Wk;
end
% if k == 1
% XPre{k} = Ak * X0 + Bk * Uk(k,:)';
% SPre{k} = Ak * W0 * Ak'+ Wk;
% else
% XPre{k} = Ak * XPos{k-1} + Bk * Uk(k,:)';
% SPre{k} = Ak * SPos{k-1}* Ak' + Wk;
% end
%%
% Check if the data point is censored or not
if obs_valid(k)
% Draw a sample if it is censored in censor_mode 1 (sampling)
if obs_valid(k) == 2 && Param.censor_mode==1
tIn = [];
tIb = [];
tUk = [];
if DISTR(1), tIn = In(k,:); end
if DISTR(2), tIb = Ib(k,:); end
if isempty(Uk)~=0, tUk = Uk(k,:); end
[tYP,tYB]=compass_sampling(DISTR,censor_time,tUk,tIn,tIb,Param,XPre{k},SPre{k});
if DISTR(1), Yn(k)=tYP; end;
if DISTR(2), Yb(k)=tYB; end;
end
% Observation: Normal
if observe_mode == 1
CTk = (Ck.*MCk{k})*xM;
DTk = Dk;
if obs_valid(k) == 2 && Param.censor_mode==2
% censor time
T = Param.censor_time;
if Param.censor_update_mode ==1
% SPos Update first
Mx = CTk * XPre{k} + DTk * In(k,:)';
Lx = max(EPS,normcdf(Yn(k),Mx,sqrt(Vk),'upper'));
Gx = normpdf(Yn(k),Mx,sqrt(Vk));
Tx = Gx/Lx;
% Calculate SPos
Hx = (Yn(k)-Mx)/Vk;
Sc = (CTk'*CTk)* Tx * (Tx-Hx);
SPos{k} = ((SPre{k}^-1)+Sc)^-1;
% XPos update next
Ac = CTk' * Tx;
XPos{k} = XPre{k} + SPos{k} * Ac;
else
in_loop = 10;
% XPos update first
xpos = XPre{k};
for h= 1:in_loop
Mx = CTk * xpos + DTk * In(k,:)';
Lx = max(EPS,normcdf(Yn(k),Mx,sqrt(Vk),'upper'));
Gx = normpdf(Yn(k),Mx,sqrt(Vk));
Tx = Gx/Lx;
% update rule
Ac = CTk' * Tx;
xpos = XPre{k} + SPre{k} * Ac;
end
XPos{k} = xpos;
Mx = CTk * xpos + DTk * In(k,:)';
Lx = max(EPS,normcdf(Yn(k),Mx,sqrt(Vk),'upper'));
Gx = normpdf(Yn(k),Mx,sqrt(Vk));
Tx = Gx/Lx;
% SPos update next
Hx = (Yn(k)-Mx)/Vk;
Sc = (CTk'*CTk)*Tx * (Tx-Hx);
SPos{k} = ((SPre{k}^-1)+Sc)^-1;
end
else
% XPos
Sk = CTk * SPre{k} * CTk' + Vk;
Yp = CTk * XPre{k} + DTk * In(k,:)';
XPos{k} = XPre{k} + SPre{k} * CTk'* Sk^-1* (Yn(k)-Yp);
% SPos
SPos{k} = (SPre{k}^-1 + CTk' * Vk^-1 * CTk)^-1;
end
end
% Observation: Bernoulli
% For Bernoulli mehtod, if there is any censored data it will be only based on resammpling technique
if observe_mode == 2
ETk = (Ek.*MEk{k})*xM;
FTk = Fk;
% XPos, SPos
% recursive mode
if update_mode==1
in_loop = 10;
% XPos update
xpos = XPre{k};
for h= 1:in_loop
st = min(MAX_EXP,ETk * xpos + FTk * Ib(k,:)');
pk = exp(st)./(1+exp(st));
xpos = XPre{k} + SPre{k} * ETk' *(Yb(k)-pk);
end
XPos{k} = xpos;
% SPos
SPos{k} = (SPre{k}^-1 + ETk'*diag(pk.*(1-pk))*ETk)^-1;
end
% one-step mode
if update_mode==2
st = min(MAX_EXP,ETk * XPre{k} + FTk * Ib(k,:)');
pk = exp(st)./(1+exp(st));
SPos{k} = (SPre{k}^-1 + ETk'*diag(pk.*(1-pk))*ETk)^-1;
XPos{k} = XPre{k} + SPos{k} * ETk' *(Yb(k)-pk);
end
end
% Observation: Normal+Bernouli
if observe_mode == 3
CTk = (Ck.*MCk{k})*xM;
DTk = Dk;
ETk = (Ek.*MEk{k})*xM;
FTk = Fk;
if obs_valid(k) == 2 && Param.censor_mode==2
% This is exactly the same for Normal distribution
% censor time
T = Param.censor_time;
% update mode 1
if Param.censor_update_mode==1
% SPos Update first
Mx = CTk * XPre{k} + DTk * In(k,:)';
Lx = max(EPS,normcdf(Yn(k),Mx,sqrt(Vk),'upper'));
Gx = normpdf(Yn(k),Mx,sqrt(Vk));
Tx = Gx/Lx;
% Update S
Hx = (Yn(k)-Mx)/Vk;
Sc = (CTk'*CTk)* Tx * (Tx-Hx);
SPos{k} = ((SPre{k}^-1)+Sc)^-1;
% XPos Update next
Ac = CTk' * Tx;
XPos{k} = XPre{k} + SPos{k} * Ac;
else
in_loop = 10;
% XPos update first
xpos = XPre{k};
for h= 1:in_loop
Mx = CTk * xpos + DTk * In(k,:)';
Lx = max(EPS,normcdf(Yn(k),Mx,sqrt(Vk),'upper'));
Gx = normpdf(Yn(k),Mx,sqrt(Vk));
Tx = Gx/Lx;
% S update
Ac = CTk' * Tx;
xpos = XPre{k} + SPre{k} * Ac;
end
XPos{k} = xpos;
Mx = CTk * xpos + DTk * In(k,:)';
Lx = max(EPS,normcdf(T,Mx,sqrt(Vk),'upper'));
Gx = normpdf(T,Mx,sqrt(Vk));
Tx = Gx/Lx;
% SPos update next
Hx = (Yn(k)-Mx)/Vk;
Sc = (CTk'*CTk)* Tx * (Tx-Hx);
SPos{k} = ((SPre{k}^-1)+Sc)^-1;
end
else
% XPos, SPos
% recursive mode
if update_mode==1
% recursive mode
in_loop = 10;
xpos = XPre{k};
Yp = CTk * XPre{k} + DTk * In(k,:)';
Sk = (CTk' * Vk^-1 * CTk + SPre{k}^-1);
for z= 1:in_loop
st = min(MAX_EXP,ETk * xpos + FTk * Ib(k,:)');
pk = exp(st)./(1+exp(st));
xpos = XPre{k} + Sk^-1 * ( ETk' *(Yb(k)-pk) + CTk'* Vk^-1 *(Yn(k)-Yp));
end
XPos{k} = xpos;
% SPos
SPos{k} = (SPre{k}^-1 + CTk' * Vk^-1 * CTk + ETk'*diag(pk.*(1-pk))*ETk )^-1;
end
% one-step mode
if update_mode==2
Yp = CTk * XPre{k} + DTk * In(k,:)';
st = min(MAX_EXP,ETk * XPre{k} + FTk * Ib(k,:)');
pk = exp(st)./(1+exp(st));
SPos{k} = (SPre{k}^-1 + ETk'*diag(pk.*(1-pk))*ETk + CTk' * Vk^-1 * CTk )^-1;
XPos{k} = XPre{k} + SPos{k} * (ETk' *(Yb(k)-pk) + CTk'* (Yn(k)-Yp) * Vk^-1);
end
end
end
% Observation: Gamma
if observe_mode == 4
CTk = (Ck.*MCk{k})*xM;
DTk = Dk;
% this is exactly equal to Normal case
if obs_valid(k) == 2 && Param.censor_mode==2
% censor time
if Param.censor_update_mode==1
% expected y
Mx = exp(CTk * XPre{k} + DTk * In(k,:)');
Hx = (Yn(k)-S)*Vk/Mx;
% components to estimate posterior
Lx = max(EPS,gammainc(Hx,Vk,'upper'));
Gx = gampdf(Hx,Vk,1);
% temporary
Ta = Gx/Lx;
% variace update
Sc = (CTk'*CTk)*((V-Hx)+Hx*Ta)*Hx*Ta;
SPos{k} = ((SPre{k}^-1)+Sc)^-1;
% XPos Update next
Ac = CTk' * Ta * Hx;
XPos{k} = XPre{k} + SPos{k} * Ac;
else
in_loop = 10;
% XPos update first
xpos = XPre{k};
for h= 1:in_loop
% expected y
Mx = exp(CTk * xpos + DTk * In(k,:)');
Hx = (Yn(k)-S)*Vk/Mx;
% components to estimate posterior
Lx = max(EPS,gammainc(Hx,Vk,'upper'));
Gx = gampdf(Hx,Vk,1);
% temporary
Ta = Gx/Lx;
% XPos Update next
Ac = CTk' * Ta * Hx;
xpos = XPre{k} + SPre{k} * Ac;
end
XPos{k} = xpos;
Mx = exp(CTk * xpos + DTk * In(k,:)');
Hx = (Yn(k)-S)*Vk/Mx;
% components to estimate posterior
Lx = max(EPS,gammainc(Hx,Vk,'upper'));
Gx = gampdf(Hx,Vk,1);
% temporary
Ta = Gx/Lx;
% variace update
Sc = (CTk'*CTk)*((Vk-Hx)+Hx*Ta)*Hx*Ta;
SPos{k} = ((SPre{k}^-1)+Sc)^-1;
end
else
% XPos, SPos
% recursive mode
if update_mode==1
% recursive mode
Yk = Yn(k) - S;
in_loop = 10;
xpos = XPre{k};
for h= 1:in_loop
Yp = exp(CTk * xpos + DTk * In(k,:)');
xpos = XPre{k} - SPre{k} * Vk * CTk'* (1-Yk/Yp);
end
XPos{k} = xpos;
SPos{k} = (SPre{k}^-1 + (Vk*(Yk/Yp))*CTk'*CTk)^-1;
end
if update_mode==2
Yk = Yn(k) - S;
Yp = exp(CTk * XPre{k} + DTk * In(k,:)');
SPos{k} = (SPre{k}^-1 + (Vk*(Yk/Yp))*CTk'*CTk)^-1;
XPos{k} = XPre{k} - SPos{k} * Vk * CTk'* (1-Yk/Yp);
end
end
end
% Observation: Gamma+Bernoulli
if observe_mode == 5
CTk = (Ck.*MCk{k})*xM;
DTk = Dk;
ETk = (Ek.*MEk{k})*xM;
FTk = Fk;
if obs_valid(k) == 2 && Param.censor_mode==2
% censor time
if Param.censor_update_mode==1
% expected y
Mx = exp(CTk * XPre{k} + DTk * In(k,:)');
Hx = (Yn(k)-S)*Vk/Mx;
% components to estimate posterior
Lx = max(EPS,gammainc(Hx,Vk,'upper'));
Gx = gampdf(Hx,Vk,1);
% temporary
Ta = Gx/Lx;
% variace update
Sc = (CTk'*CTk)*((Vk-Hx)+Hx*Ta)*Hx*Ta;
SPos{k} = ((SPre{k}^-1)+Sc)^-1;
% XPos Update next
Ac = CTk' * Ta * Hx;
XPos{k} = XPre{k} + SPos{k} * Ac;
else
in_loop = 10;
% XPos update first
xpos = XPre{k};
for h= 1:in_loop
% expected y
Mx = exp(CTk * xpos + DTk * In(k,:)');
Hx = (Yn(k)-S)*Vk/Mx;
% components to estimate posterior
Lx = max(EPS,gammainc(Hx,Vk,'upper'));
Gx = gampdf(Hx,Vk,1);
% temporary
Ta = Gx/Lx;
% XPos Update next
Ac = CTk' * Ta * Hx;
xpos = XPre{k} + SPre{k} * Ac;
end
XPos{k} = xpos;
Mx = exp(CTk * xpos + DTk * In(k,:)');
Hx = (Yn(k)-S)*Vk/Mx;
% components to estimate posterior
Lx = max(EPS,gammainc(Hx,Vk,'upper'));
Gx = gampdf(Hx,Vk,1);
% temporary
Ta = Gx/Lx;
% variace update
Sc = (CTk'*CTk)*((Vk-Hx)+Hx*Ta)*Hx*Ta;
SPos{k} = ((SPre{k}^-1)+Sc)^-1;
end
else
% recursive mode
if update_mode==1
% XPos, SPos
in_loop = 10;
Yk = Yn(k) - S;
xpos = XPre{k};
for h = 1:in_loop
st = min(MAX_EXP, ETk * xpos + FTk * Ib(k,:)');
pk = exp(st)./(1+exp(st));
Yp = exp(CTk * xpos + DTk * In(k,:)');
xpos = XPre{k} + SPre{k} * ( ETk' *(Yb(k)-pk) - Vk * CTk'* (1-Yk/Yp) );
end
XPos{k} = xpos;
% SPos
SPos{k} = (SPre{k}^-1 + CTk' * CTk * Vk *(Yk/Yp) + ETk'*ETk *diag(pk.*(1-pk)))^-1;
end
% one-step mode
if update_mode==2
% XPos, SPos
Yk = Yn(k) - S;
Yp = exp(CTk * XPre{k} + DTk * In(k,:)');
% Pk
st = min(MAX_EXP, ETk * XPre{k} + FTk * Ib(k,:)');
pk = exp(st)./(1+exp(st));
% SPos
SPos{k} = (SPre{k}^-1 + CTk' * CTk * Vk*(Yk/Yp) + ETk'*ETk*diag(pk.*(1-pk)) )^-1;
% XPos
XPos{k} = XPre{k} + SPos{k} * ( ETk' *(Yb(k)-pk) - Vk * CTk'* (1-Yk/Yp) );
end
end
end
%% Yalda:
% Observation: Point Process & Marked Point Process
if observe_mode == 6 || observe_mode == 7
CTk = (Ck.*MCk{k})*xM;
DTk = Dk;
Yk = Yn(k);
% XPos, SPos
% recursive mode
if update_mode==1
% recursive mode
in_loop = 10;
xpos = XPre{k};
for h= 1:in_loop
Yp = min(1,dt * exp(CTk * xpos + DTk * In(k,:)'));
xpos = XPre{k} - SPre{k} * CTk'* (Yp-Yk);
end
XPos{k} = xpos;
SPos{k} = pinv(pinv(SPre{k}) + (CTk'*Yp*CTk));
end
if update_mode==2
Yp = min(1,dt*exp(CTk * XPre{k} + DTk * In(k,:)'));
SPos{k} = pinv(pinv(SPre{k})+ (CTk'*Yp*CTk));
XPos{k} = XPre{k} - SPos{k} * CTk'* (Yp-Yk);
end
if observe_mode == 7 && Yk==1
ETk = (Ek.*MEk{k})*xM;
FTk = Fk;
% observation = Mark/event
% recursive mode
if update_mode==1
% XPos, SPos
in_loop = 10;
xpos = XPre{k};
for h = 1:in_loop
st = min(MAX_EXP, ETk * xpos + FTk * Ib(k,:)');
pk = exp(st)./(1+exp(st));
Yp = min(1,dt*exp(CTk * xpos + DTk * In(k,:)'));
xpos = XPre{k} + SPre{k} * ( ETk' *(Yb(k)-pk) - CTk'* (Yp-Yk) );
end
XPos{k} = xpos;
% SPos
SPos{k} = (SPre{k}^-1 + CTk' *Yp* CTk + ETk'*diag(pk.*(1-pk))*ETk )^-1;
end
% one-step mode
if update_mode==2
% XPos, SPos
Yp = min(1,dt*exp(CTk * XPre{k} + DTk * In(k,:)'));
% Pk
st = min(MAX_EXP, ETk * XPre{k} + FTk * Ib(k,:)');
pk = exp(st)./(1+exp(st));
% SPos
SPos{k} = (SPre{k}^-1 + CTk' *Yp* CTk + ETk'*diag(pk.*(1-pk))*ETk )^-1;
% XPos
XPos{k} = XPre{k} + SPos{k} * ( ETk' *(Yb(k)-pk) - CTk'* (Yp-Yk) );
end
end
end
else
% randomly censored, the filter estimate will be equal to one step prediction
XPos{k} = XPre{k};
SPos{k} = SPre{k};
end
end
%% Smoother Part - it is based on classical Kalman Smoother
% Kalman Smoothing
As = cell(K,1);
% posterior mean
XSmt = cell(K,1);
XSmt{end} = XPos{end};
% posterior variance
SSmt = cell(K,1);
SSmt{end} = SPos{end};
for k=K-1:-1:1
% Ak, equation (A.10)
As{k} = SPos{k} * Ak' * SPre{k+1}^-1 ;
% Smting function, equation (A.9)
XSmt{k} = XPos{k} + As{k} * (XSmt{k+1}- XPre{k+1});
% Variance update, equation (A.11)
SSmt{k} = SPos{k} + As{k} * (SSmt{k+1}- SPre{k+1}) * As{k}';
end
% Kalman smoother for time 0
As0 = W0 * Ak' * SPre{1}^-1;
XSmt0 = X0 + As0 * (XSmt{1}- XPre{1}) ;
SSmt0 = W0 + As0 * (SSmt{1}- SPre{1}) * As0';
%% Extra Component of the State Prediction Ckk = E(Xk*Xk)
% Ckk = E(Xk*Xk) prediction by smoothing
Ckk = cell(K,1);
for k = 1:K
% Wk update - Smting Xk*Xk
Ckk{k} = SSmt{k} + XSmt{k} * XSmt{k}';
end
Ckk0 = SSmt0 + XSmt0 * XSmt0';
%% Extra Component of the State Prediction Ckk = E(Xk*Xk-1)
% Ckk_1=E(Xk-1*Xk) prediction by smoothing - it is kept at index K
% Wkk_1= Ckk_1 + Bias
% Covariance for smoothed estimates in state space models - Biometrica
% 1988- 601-602
Ckk_1 = cell(K,1);
Wkk_1 = cell(K,1);
for k = 1:K
% Wkk update - Smoothing Xk-1*Xk
if k>1
Wkk_1{k} = As{k-1} * SSmt{k};
Ckk_1{k} = Wkk_1{k} + XSmt{k} * XSmt{k-1}';
else
Wkk_1{k} = As0 * SSmt{k};
Ckk_1{k} = Wkk_1{k} + XSmt{k} * XSmt0';
end
end
%% Here, We Generate EYn and Yb Prediction
%% Generate EYn
if DISTR(1)>0
for k=1:K
% Filtering
CTk = (Ck.*MCk{k})*xM;
DTk = Dk;
% EYn
if DISTR(1)==1
temp = CTk * XSmt{k} + DTk * In(k,:)';
EYn(k) = temp';
else
temp = CTk * XSmt{k} + DTk * In(k,:)';
EYn(k) = exp(temp);
end
end
end
if DISTR(2)==1
for k=1:K
% Filtering
ETk = (Ek.*MEk{k})*xM;
FTk = Fk;
% YP
temp = ETk * XSmt{k} + FTk * Ib(k,:)';
EYb(k) = exp(temp')./(1+exp(temp'));
end
end
%% Parameter Estimation Section
%% Description of model parameters
% Note that at index K of above variables, we keep:
% 1. XPos(k) and SPos(k) are the optimum estimates of the Xk given 1:k
% 2. XPre(k) and SPre(k) are the optimum estimates of the Xk give 1:k-1
% 3. XSmt(k) and SSmt(k) are the optimum estimates of the Xk give 1:K
% 4. Wk(k) is the E(Xk*Xk' given 1:K) observation
% 5. Wkk(k) is the E(Xk-1*Xk' given 1:K) observation
% note that Wkk(1), E(X0*X1)=X0*E(X1) as X0 is a deterministic term
% Calculate State Transition Model Parameters
% Update Ak and Bk - we assume iid noise terms
upAk = Ak;
upBk = Bk;
upWk = Wk;
% dimension of Xk
dx = size(Ak,2);
% dimension of input
di = size(Bk,2);
if Param.UpdateStateParam == 1 % A and B is getting updated
if Param.DiagonalA == 0
% Update the Ak, Bk row by row
% We define At once
At = zeros(dx+di,dx+di);
for k = 1:K
if k==1
% Calculate At
SecA = Ckk0;
SecB = XSmt0*Uk(k,:);
SecC = Uk(k,:)'*XSmt0';
SecD = Uk(k,:)'*Uk(k,:);
At = At + [[SecA SecB]
[SecC SecD]];
else
% Calculate At
SecA = Ckk{k-1};
SecB = XSmt{k-1}*Uk(k,:);
SecC = Uk(k,:)'*XSmt{k-1}';
SecD = Uk(k,:)'*Uk(k,:);
At = At + [[SecA SecB]
[SecC SecD]];
end
end
% We define Bt per row
for d=1:dx
% Build At, Bt
Bt = zeros(dx+di,1);
for k = 1:K
% Calculate Bt
temp = Ckk_1{k}; SecA = temp(:,d);
temp = XSmt{k}; SecB = temp(d)* Uk(k,:)';
Bt = Bt + [SecA;SecB];
end
% Calculate At and Bt for the d-th row
T = pinv(At)*Bt;
upAk(d,:) = T(1:dx);
upBk(d,:) = T(dx+1:end)';
end
end
if Param.DiagonalA == 1
% Update the Ak, Bk row by row
for d=1:dx
% Build At, Bt
At = zeros(1+di,1+di);
Bt = zeros(1+di,1);
for k = 1:K
if k==1
% Calculate At
temp = Ckk0;
SecA = temp(d,d);
temp = XSmt0;
SecB = temp(d)*Uk(k,:);
SecC = Uk(k,:)'*temp(d);
SecD = Uk(k,:)'*Uk(k,:);
At = At + [[SecA SecB]
[SecC SecD]];
else
% Calculate At
temp = Ckk{k-1};
SecA = temp(d,d);
temp = XSmt{k-1};
SecB = temp(d)*Uk(k,:);
SecC = Uk(k,:)'*temp(d);
SecD = Uk(k,:)'*Uk(k,:);
At = At + [[SecA SecB]
[SecC SecD]];
end
% Calculate Bt
temp = Ckk_1{k}; SecA = temp(d,d)';
temp = XSmt{k}; SecB = temp(d)* Uk(k,:)';
Bt = Bt + [SecA;SecB];
end
% Calculate At and Bt for the d-th row
T = pinv(At)*Bt;
upAk(d,d) = T(1);
upBk(d,:) = T(2:end);
end
end
end
if Param.UpdateStateParam == 2 % A fixed, and B is getting updated
% Update the Bk row by row
% We define At once
At = zeros(di,di);
for k = 1:K
% Calculate At
SecD = Uk(k,:)'*Uk(k,:);
At = At + SecD;
end
% We define Bt per row
for d=1:dx
% Build At, Bt
Bt = zeros(di,1);
for k = 1:K
% Calculate Bt
if k==1
temp = XSmt{k}-Ak*XSmt0;
else
temp = XSmt{k}-Ak*XSmt{k-1};
end
Bt = Bt + temp(d)*Uk(k,:)';
end
% Calculate At and Bt for the d-th row
upBk(d,:) = pinv(At)*Bt;
end
end
if Param.UpdateStateParam == 3 % A and B with specific structure
% here, we find A and B for specific class of problem
for d=1:2:size(upAk,1)-1 % odd components
num_a = 0;
den_a = 0;
for k = 1:K
if k == 1
num_a = num_a + SSmt0(d,d)+XSmt0(d)^2;
num_a = num_a - XSmt0(d)*0;
num_a = num_a - Ckk0(d,d);
num_a = num_a + XSmt{k}(d)*0;
den_a = den_a + SSmt0(d,d)+XSmt0(d)^2;
else
num_a = num_a + SSmt{k-1}(d,d)+XSmt{k-1}(d)^2;
num_a = num_a - XSmt{k-1}(d)*Uk(k-1,(d+1)/2);
num_a = num_a - Ckk{k-1}(d,d);
num_a = num_a + XSmt{k}(d)*Uk(k-1,(d+1)/2);
den_a = den_a + SSmt{k-1}(d,d)+(XSmt{k-1}(d)-Uk(k-1,(d+1)/2))^2;
end
end
upAk(d,d) = 1- (num_a/den_a);
upBk(d,(d+1)/2) = num_a/den_a;
end
for d=2:2:size(upAk,1)-1 % even components
num_a = 0;
den_a = 0;
for k = 1:K
if k == 1
num_a = num_a + SSmt0(d,d-1)+XSmt0(d)*XSmt0(d-1);
num_a = num_a - XSmt0(d)*0;
num_a = num_a - Ckk0(d,d-1);
num_a = num_a + XSmt{k}(d)*0;
den_a = den_a + SSmt0(d-1,d-1)+XSmt0(d-1)^2;
else
num_a = num_a + SSmt{k-1}(d,d-1)+XSmt{k-1}(d)*XSmt{k-1}(d-1);
num_a = num_a - XSmt{k-1}(d)*Uk(k-1,d/2);
num_a = num_a - Ckk{k-1}(d,d-1);
num_a = num_a + XSmt{k}(d)*Uk(k-1,d/2);
den_a = den_a + SSmt{k-1}(d-1,d-1)+(XSmt{k-1}(d-1)-sum(Uk(k-1,:)))^2;
end
end
upAk(d,d) = 1;
upAk(d,d-1) = - (num_a/den_a);
upBk(d,d/2) = num_a/den_a;
end
d = size(upAk,1);
num_a = 0;
den_a = 0;
for k = 1:K
if k == 1
num_a = num_a + SSmt0(d,d)+XSmt0(d)^2;
num_a = num_a - XSmt0(d)*0;
num_a = num_a - Ckk0(d,d);
num_a = num_a + XSmt{k}(d)*0;
den_a = den_a + SSmt0(d,d)+XSmt0(d)^2;
else
num_a = num_a + SSmt{k-1}(d,d)+XSmt{k-1}(d)^2;
num_a = num_a - XSmt{k-1}(d)*sum(Uk(k-1,:));
num_a = num_a - Ckk{k-1}(d,d);
num_a = num_a + XSmt{k}(d)*sum(Uk(k-1,:));
den_a = den_a + SSmt{k-1}(d,d)+(XSmt{k-1}(d)-sum(Uk(k-1,:)))^2;
end
end
upAk(d,d) = 1- (num_a/den_a);
upBk(d,:) = num_a/den_a;
end
% Calculate Wk - we assume Wk is diagonal
for d=1:dx
upWk_a(d,d) = 0;
upWk_b(d,d) = 0;
for k = 1:K
if obs_valid(k)==1
if k==1
% add E(Xk*Xk)
temp = Ckk{k}; upWk_a(d,d) = upWk_a(d,d) + temp(d,d);
% add E(Xk-1*Xk-1)*A^2
temp = Ckk0; upWk_a(d,d) = upWk_a(d,d) + upAk(d,:) * temp * upAk(d,:)';
% add Bk*Uk^2
upWk_a(d,d) = upWk_a(d,d) + (upBk(d,:) * Uk(k,:)')^2;
% add -2*A*E(Xk-1*Xk)
temp = Ckk_1{k}; upWk_a(d,d) = upWk_a(d,d) - upAk(d,:) * (temp(:,d)+ temp(d,:)');
% add -2*B*U*E(Xk)
temp = XSmt{k}; upWk_a(d,d) = upWk_a(d,d) - 2 * (upBk(d,:) * Uk(k,:)') * temp(d);
% add 2 *B*U*A*E(Xk-1)
temp = XSmt0; upWk_a(d,d) = upWk_a(d,d) + 2*(upBk(d,:) * Uk(k,:)')*(upAk(d,:)*temp);
else
% add E(Xk*Xk)
temp = Ckk{k}; upWk_a(d,d) = upWk_a(d,d) + temp(d,d);
% add E(Xk-1*Xk-1)*A^2
temp = Ckk{k-1}; upWk_a(d,d) = upWk_a(d,d) + upAk(d,:) * temp * upAk(d,:)';
% add Bk*Uk^2
upWk_a(d,d) = upWk_a(d,d) + (upBk(d,:) * Uk(k,:)')^2;
% add -2*A*E(Xk-1*Xk)
temp = Ckk_1{k}; upWk_a(d,d) = upWk_a(d,d) - upAk(d,:) * (temp(:,d)+ temp(d,:)');
% add -2*B*U*E(Xk)
temp = XSmt{k}; upWk_a(d,d) = upWk_a(d,d) - 2 * (upBk(d,:) * Uk(k,:)') * temp(d);
% add 2 *B*U*A*E(Xk-1)
temp = XSmt{k-1};upWk_a(d,d) = upWk_a(d,d) + 2*(upBk(d,:) * Uk(k,:)')*(upAk(d,:)*temp);
end
end
if obs_valid(k)==0
if k==1
% add E(Xk*Xk)
temp = Ckk{k}; upWk_b(d,d) = upWk_b(d,d) + temp(d,d);
% add E(Xk-1*Xk-1)*A^2
temp = Ckk0; upWk_b(d,d) = upWk_b(d,d) + upAk(d,:) * temp * upAk(d,:)';
% add Bk*Uk^2
upWk_b(d,d) = upWk_b(d,d) + (upBk(d,:) * Uk(k,:)')^2;
% add -2*A*E(Xk-1*Xk)
temp = Ckk_1{k}; upWk_b(d,d) = upWk_b(d,d) - upAk(d,:) * (temp(:,d)+ temp(d,:)');
% add -2*B*U*E(Xk)
temp = XSmt{k}; upWk_b(d,d) = upWk_b(d,d) - 2 * (upBk(d,:) * Uk(k,:)') * temp(d);
% add 2 *B*U*A*E(Xk-1)
temp = XSmt0; upWk_b(d,d) = upWk_b(d,d) + 2*(upBk(d,:) * Uk(k,:)')*(upAk(d,:)*temp);
else
% add E(Xk*Xk)
temp = Ckk{k}; upWk_b(d,d) = upWk_b(d,d) + temp(d,d);
% add E(Xk-1*Xk-1)*A^2
temp = Ckk{k-1}; upWk_b(d,d) = upWk_b(d,d) + upAk(d,:) * temp * upAk(d,:)';
% add Bk*Uk^2
upWk_b(d,d) = upWk_b(d,d) + (upBk(d,:) * Uk(k,:)')^2;
% add -2*A*E(Xk-1*Xk)
temp = Ckk_1{k}; upWk_b(d,d) = upWk_b(d,d) - upAk(d,:) * (temp(:,d)+ temp(d,:)');
% add -2*B*U*E(Xk)
temp = XSmt{k}; upWk_b(d,d) = upWk_b(d,d) - 2 * (upBk(d,:) * Uk(k,:)') * temp(d);
% add 2 *B*U*A*E(Xk-1)
temp = XSmt{k-1};upWk_b(d,d) = upWk_b(d,d) + 2*(upBk(d,:) * Uk(k,:)')*(upAk(d,:)*temp);
end
end
end
end
K0 = length(find(obs_valid==0));
up_ws = ws;
for xi = 1:10
for d=1:dx
upWk(d,d) = (upWk_a(d,d) + upWk_b(d,d) /up_ws ) /K;
end
temp = 0;
for d=1:dx
temp = temp + upWk_b(d,d) / upWk(d,d);
end
up_ws = temp/(dx*K0);
end
Ak = upAk;
Bk = upBk;
if Param.UpdateStateNoise == 1
Wk = upWk;
ws = up_ws;
end
% % Calculate Wk - we assume Wk is diagonal
% for d=1:dx
% upWk(d,d) = 0;
% for k = 1:K
% if obs_valid(k)==1
% if k==1
% % add E(Xk*Xk)
% temp = Ckk{k}; upWk(d,d) = upWk(d,d) + temp(d,d);
% % add E(Xk-1*Xk-1)*A^2
% temp = Ckk0; upWk(d,d) = upWk(d,d) + upAk(d,:) * temp * upAk(d,:)';
% % add Bk*Uk^2
% upWk(d,d) = upWk(d,d) + (upBk(d,:) * Uk(k,:)')^2;
% % add -2*A*E(Xk-1*Xk)
% temp = Ckk_1{k}; upWk(d,d) = upWk(d,d) - upAk(d,:) * (temp(:,d)+ temp(d,:)');
% % add -2*B*U*E(Xk)
% temp = XSmt{k}; upWk(d,d) = upWk(d,d) - 2 * (upBk(d,:) * Uk(k,:)') * temp(d);
% % add 2 *B*U*A*E(Xk-1)
% temp = XSmt0; upWk(d,d) = upWk(d,d) + 2*(upBk(d,:) * Uk(k,:)')*(upAk(d,:)*temp);
% else
% % add E(Xk*Xk)
% temp = Ckk{k}; upWk(d,d) = upWk(d,d) + temp(d,d);
% % add E(Xk-1*Xk-1)*A^2
% temp = Ckk{k-1}; upWk(d,d) = upWk(d,d) + upAk(d,:) * temp * upAk(d,:)';
% % add Bk*Uk^2
% upWk(d,d) = upWk(d,d) + (upBk(d,:) * Uk(k,:)')^2;
% % add -2*A*E(Xk-1*Xk)
% temp = Ckk_1{k}; upWk(d,d) = upWk(d,d) - upAk(d,:) * (temp(:,d)+ temp(d,:)');
% % add -2*B*U*E(Xk)
% temp = XSmt{k}; upWk(d,d) = upWk(d,d) - 2 * (upBk(d,:) * Uk(k,:)') * temp(d);
% % add 2 *B*U*A*E(Xk-1)
% temp = XSmt{k-1};upWk(d,d) = upWk(d,d) + 2*(upBk(d,:) * Uk(k,:)')*(upAk(d,:)*temp);
% end
% end
% end
% K0 = length(find(obs_valid==1));
% upWk(d,d) = upWk(d,d)/K0;
% %---------------------------------
% % Update State parameters
% %----------------------------------
% Ak = upAk;
% Bk = upBk;
% if Param.UpdateStateNoise == 1
% Wk = upWk;
% end
% end
% Calculate the X0 parameters
if Param.UpdateStateX0 == 1
X0 = XSmt0;
W0 = SSmt0;
end
% -----------------------------------------------
% Calculate likelihood function (Hidden Variable)
% Constant terms are excluded
% -----------------------------------------------
MaxH = 0;
K0 = length(find(obs_valid==1));
for d=1:size(Ak,2)
%-- first variance
MaxH = MaxH -0.5*K0*log(Wk(d,d));
%-- other terms
TempH = 0;
for k = 1:K
if obs_valid(k)==1
if k==1