[262] | 1 | |
---|
[384] | 2 | #include <estim/kalman.h> |
---|
| 3 | #include <estim/particles.h> |
---|
[32] | 4 | |
---|
[254] | 5 | using namespace bdm; |
---|
[32] | 6 | |
---|
| 7 | //These lines are needed for use of cout and endl |
---|
| 8 | using std::cout; |
---|
| 9 | using std::endl; |
---|
| 10 | |
---|
| 11 | int main() { |
---|
| 12 | // Klaman filter |
---|
| 13 | mat A, B,C,D,R,Q,P0; |
---|
| 14 | vec mu0; |
---|
| 15 | mat Mu0;// read from matlab |
---|
| 16 | // input from Matlab |
---|
| 17 | it_file fin( "testKF.it" ); |
---|
| 18 | |
---|
[33] | 19 | mat Dt, XQRt,eR,eQ; |
---|
[32] | 20 | int Ndat; |
---|
| 21 | |
---|
| 22 | bool xxx= fin.seek( "d" ); |
---|
| 23 | if (!xxx){ it_error("testKF.it not found");} |
---|
| 24 | fin >>Dt; |
---|
| 25 | fin.seek( "A" ); |
---|
| 26 | fin >> A; |
---|
| 27 | fin.seek( "B" ); |
---|
| 28 | fin >> B; |
---|
| 29 | fin.seek( "C" ); |
---|
| 30 | fin >> C; |
---|
| 31 | fin.seek( "D" ); |
---|
| 32 | fin >> D; |
---|
| 33 | fin.seek( "R" ); |
---|
| 34 | fin >> R; |
---|
| 35 | fin.seek( "Q" ); fin >> Q; |
---|
| 36 | fin.seek( "P0" ); fin >> P0; |
---|
| 37 | fin.seek( "mu0" ); fin >> Mu0; |
---|
| 38 | mu0=Mu0.get_col(0); |
---|
| 39 | |
---|
| 40 | Ndat = Dt.cols(); |
---|
[33] | 41 | XQRt=zeros( 5,Ndat ); |
---|
| 42 | mat Xt=zeros( 2,Ndat ); |
---|
[32] | 43 | |
---|
| 44 | // cout << KF; |
---|
[162] | 45 | RV rx("{x }","2"); |
---|
| 46 | RV ru("{u }","1"); |
---|
| 47 | RV ry("{y }","1"); |
---|
| 48 | RV rQR("{Q,R }","3"); |
---|
[32] | 49 | // |
---|
[270] | 50 | KFcondQR KF; |
---|
[32] | 51 | // KF with R unknown |
---|
| 52 | KF.set_parameters(A,B,C,D,ldmat(R),ldmat(Q)); |
---|
| 53 | KF.set_est(mu0,ldmat(P0) ); |
---|
| 54 | // |
---|
[270] | 55 | Kalman<ldmat> KFtr; |
---|
[33] | 56 | // KF with R unknown |
---|
| 57 | KFtr.set_parameters(A,B,C,D,ldmat(R),ldmat(Q)); |
---|
| 58 | KFtr.set_est(mu0,ldmat(P0) ); |
---|
| 59 | |
---|
[270] | 60 | mgamma evolQR; |
---|
| 61 | evolQR.set_parameters(10.0,"1 1 1"); //sigma = 1/10 mu |
---|
[32] | 62 | |
---|
[283] | 63 | MPF<KFcondQR> KF_QR; |
---|
| 64 | KF_QR.set_parameters(&evolQR,&evolQR,100); |
---|
[33] | 65 | evolQR.condition("1 1 1"); |
---|
[283] | 66 | KF_QR.set_statistics(evolQR._e(), &KF); |
---|
[271] | 67 | const epdf& mpost=KF_QR.posterior(); |
---|
| 68 | const epdf& mposttr=KFtr.posterior(); |
---|
[32] | 69 | |
---|
[33] | 70 | XQRt.set_col( 0,mpost.mean()); |
---|
| 71 | Xt.set_col( 0,mposttr.mean()); |
---|
[32] | 72 | for ( int t=1;t<Ndat;t++ ) { |
---|
[33] | 73 | KF_QR.bayes( Dt.get_col( t )); |
---|
[32] | 74 | KFtr.bayes( Dt.get_col( t )); |
---|
| 75 | |
---|
[33] | 76 | XQRt.set_col(t,mpost.mean()); |
---|
| 77 | Xt.set_col(t,mposttr.mean()); |
---|
[32] | 78 | } |
---|
| 79 | |
---|
[33] | 80 | it_file fou( "testKF_QR_res.it" ); |
---|
| 81 | fou << Name("xqrth") << XQRt; |
---|
| 82 | fou << Name("xth") << Xt; |
---|
[32] | 83 | //Exit program: |
---|
| 84 | return 0; |
---|
| 85 | |
---|
| 86 | } |
---|