12 | | // Kalman filter |
13 | | mat A, B, C, D, R, Q, P0; |
14 | | vec mu0; |
15 | | mat Mu0;; |
16 | | // input from Matlab |
17 | | it_file fin ( "kalman_stress.it" ); |
| 12 | // Kalman filter |
| 13 | mat A, B, C, D, R, Q, P0; |
| 14 | vec mu0; |
| 15 | mat Mu0;; |
| 16 | // input from Matlab |
| 17 | it_file fin ( "kalman_stress.it" ); |
48 | | // Prepare for Kalman filters in BDM: |
49 | | RV rx ( "{x }", vec_1 ( A.cols() ) ); |
50 | | RV ru ( "{u }", vec_1 ( B.cols() ) ); |
51 | | RV ry ( "{y }", vec_1 ( C.rows() ) ); |
| 48 | // Prepare for Kalman filters in BDM: |
| 49 | RV rx ( "{x }", vec_1 ( A.cols() ) ); |
| 50 | RV ru ( "{u }", vec_1 ( B.cols() ) ); |
| 51 | RV ry ( "{y }", vec_1 ( C.rows() ) ); |
61 | | //Chol |
62 | | KalmanCh KF; |
63 | | KF.set_parameters ( A, B, C, D, chmat ( Q ), chmat ( R ) ); |
64 | | KF.set_statistics ( mu0, chmat ( P0 ) ); //prediction! |
65 | | KF.set_evalll ( false ); |
66 | | KF.validate(); |
67 | | const epdf& KFep = KF.posterior(); |
68 | | mat Xt ( dimx, Ndat ); |
69 | | Xt.set_col ( 0, KFep.mean() ); |
| 61 | //Chol |
| 62 | KalmanCh KF; |
| 63 | KF.set_parameters ( A, B, C, D, chmat ( Q ), chmat ( R ) ); |
| 64 | KF.set_statistics ( mu0, chmat ( P0 ) ); //prediction! |
| 65 | KF.set_evalll ( false ); |
| 66 | KF.validate(); |
| 67 | const epdf& KFep = KF.posterior(); |
| 68 | mat Xt ( dimx, Ndat ); |
| 69 | Xt.set_col ( 0, KFep.mean() ); |
71 | | // FULL |
72 | | KalmanFull KF2; |
73 | | KF2.set_parameters ( A, B, C, D, Q, R ); |
74 | | KF2.set_statistics ( mu0, P0 ); |
75 | | KF2.set_evalll ( false ); |
76 | | KF2.validate(); |
77 | | mat Xt2 ( dimx, Ndat ); |
78 | | Xt2.set_col ( 0, mu0 ); |
| 71 | // FULL |
| 72 | KalmanFull KF2; |
| 73 | KF2.set_parameters ( A, B, C, D, Q, R ); |
| 74 | KF2.set_statistics ( mu0, P0 ); |
| 75 | KF2.set_evalll ( false ); |
| 76 | KF2.validate(); |
| 77 | mat Xt2 ( dimx, Ndat ); |
| 78 | Xt2.set_col ( 0, mu0 ); |
81 | | // EKF |
82 | | shared_ptr<bilinfn> fxu = new bilinfn ( A, B ); |
83 | | shared_ptr<bilinfn> hxu = new bilinfn ( C, D ); |
84 | | EKFCh KFE; |
85 | | KFE.set_parameters ( fxu, hxu, Q, R ); |
86 | | KFE.set_statistics ( mu0, chmat ( P0 ) ); |
87 | | KFE.set_evalll ( false ); |
88 | | KFE.validate(); |
89 | | const epdf& KFEep = KFE.posterior(); |
90 | | mat XtE ( dimx, Ndat ); |
91 | | XtE.set_col ( 0, KFEep.mean() ); |
| 81 | // EKF |
| 82 | shared_ptr<bilinfn> fxu = new bilinfn ( A, B ); |
| 83 | shared_ptr<bilinfn> hxu = new bilinfn ( C, D ); |
| 84 | EKFCh KFE; |
| 85 | KFE.set_parameters ( fxu, hxu, Q, R ); |
| 86 | KFE.set_statistics ( mu0, chmat ( P0 ) ); |
| 87 | KFE.set_evalll ( false ); |
| 88 | KFE.validate(); |
| 89 | const epdf& KFEep = KFE.posterior(); |
| 90 | mat XtE ( dimx, Ndat ); |
| 91 | XtE.set_col ( 0, KFEep.mean() ); |
97 | | vec dt; |
98 | | tt.tic(); |
99 | | for ( int t = 1; t < Ndat; t++ ) { |
100 | | dt = Dt.get_col ( t ); |
101 | | KF.bayes ( dt.get ( 0, C.rows() - 1 ), dt.get ( C.rows(), dt.length() - 1 ) ); |
102 | | Xt.set_col ( t, KFep.mean() ); |
103 | | } |
104 | | exec_times ( 0 ) = tt.toc(); |
| 97 | vec dt; |
| 98 | tt.tic(); |
| 99 | for ( int t = 1; t < Ndat; t++ ) { |
| 100 | dt = Dt.get_col ( t ); |
| 101 | KF.bayes ( dt.get ( 0, C.rows() - 1 ), dt.get ( C.rows(), dt.length() - 1 ) ); |
| 102 | Xt.set_col ( t, KFep.mean() ); |
| 103 | } |
| 104 | exec_times ( 0 ) = tt.toc(); |
106 | | tt.tic(); |
107 | | for ( int t = 1; t < Ndat; t++ ) { |
108 | | dt = Dt.get_col ( t ); |
109 | | KF2.bayes ( dt.get ( 0, C.rows() - 1 ), dt.get ( C.rows(), dt.length() - 1 ) ); |
110 | | Xt2.set_col ( t, KF2.posterior().mean() ); |
111 | | } |
112 | | exec_times ( 1 ) = tt.toc(); |
| 106 | tt.tic(); |
| 107 | for ( int t = 1; t < Ndat; t++ ) { |
| 108 | dt = Dt.get_col ( t ); |
| 109 | KF2.bayes ( dt.get ( 0, C.rows() - 1 ), dt.get ( C.rows(), dt.length() - 1 ) ); |
| 110 | Xt2.set_col ( t, KF2.posterior().mean() ); |
| 111 | } |
| 112 | exec_times ( 1 ) = tt.toc(); |
114 | | tt.tic(); |
115 | | for ( int t = 1; t < Ndat; t++ ) { |
116 | | dt = Dt.get_col ( t ); |
117 | | KFE.bayes ( dt.get ( 0, C.rows() - 1 ), dt.get ( C.rows(), dt.length() - 1 ) ); |
118 | | XtE.set_col ( t, KFEep.mean() ); |
119 | | } |
120 | | exec_times ( 2 ) = tt.toc(); |
| 114 | tt.tic(); |
| 115 | for ( int t = 1; t < Ndat; t++ ) { |
| 116 | dt = Dt.get_col ( t ); |
| 117 | KFE.bayes ( dt.get ( 0, C.rows() - 1 ), dt.get ( C.rows(), dt.length() - 1 ) ); |
| 118 | XtE.set_col ( t, KFEep.mean() ); |
| 119 | } |
| 120 | exec_times ( 2 ) = tt.toc(); |
123 | | it_file fou ( "kalman_stress_res.it" ); |
124 | | fou << Name ( "xth" ) << Xt; |
125 | | fou << Name ( "xth2" ) << Xt2; |
126 | | fou << Name ( "xthE" ) << XtE; |
127 | | fou << Name ( "exec_times" ) << exec_times; |
| 123 | it_file fou ( "kalman_stress_res.it" ); |
| 124 | fou << Name ( "xth" ) << Xt; |
| 125 | fou << Name ( "xth2" ) << Xt2; |
| 126 | fou << Name ( "xthE" ) << XtE; |
| 127 | fou << Name ( "exec_times" ) << exec_times; |