| [717] | 1 | #include "estim/arx.h" | 
|---|
|  | 2 | using namespace bdm; | 
|---|
|  | 3 |  | 
|---|
|  | 4 | int main() { | 
|---|
|  | 5 | // Setup model : ARX for 1D Gaussian | 
|---|
|  | 6 | //Test constructor | 
|---|
|  | 7 | mat V0 = 0.00001 * eye ( 2 ); | 
|---|
|  | 8 | V0 ( 0, 0 ) = 0.1; // | 
|---|
|  | 9 | ARX Ar; | 
|---|
|  | 10 | Ar.set_statistics ( 1, V0, -1.0 ); | 
|---|
|  | 11 | Ar.set_constant(true); | 
|---|
|  | 12 | Ar.validate(); | 
|---|
|  | 13 |  | 
|---|
|  | 14 | mat mu ( 1, 1 ); | 
|---|
|  | 15 | mat R ( 1, 1 ); | 
|---|
|  | 16 | Ar.posterior().mean_mat ( mu, R ); | 
|---|
|  | 17 | cout << "Prior moments: mu=" << mu << ", R=" << R << endl; | 
|---|
|  | 18 |  | 
|---|
|  | 19 | int ndat = 200; | 
|---|
|  | 20 | vec smp = randn ( ndat ); | 
|---|
|  | 21 | // | 
|---|
|  | 22 | mat Smp = ones ( 2, ndat ); | 
|---|
|  | 23 | Smp.set_row ( 0, smp ); | 
|---|
|  | 24 | // | 
|---|
|  | 25 | Ar.bayes_batch ( Smp ); | 
|---|
|  | 26 | // Ar is now filled with estimates of N(0,1); | 
|---|
|  | 27 | cout << "Empirical moments: mu=" << sum ( smp ) / ndat << ", R=" << sum_sqr ( smp ) / ndat - pow ( sum ( smp ) / ndat, 2 ) << endl; | 
|---|
|  | 28 | Ar.posterior().mean_mat ( mu, R ); | 
|---|
|  | 29 | cout << "Posterior moments: mu=" << mu << ", R=" << R << endl; | 
|---|
|  | 30 |  | 
|---|
|  | 31 | //////// TEST prediction | 
|---|
|  | 32 | vec x = linspace ( -3.0, 3.0, 100 ); | 
|---|
|  | 33 | double xstep = 6.0 / 100.0; | 
|---|
|  | 34 | mat X ( 1, 100 ); | 
|---|
|  | 35 | mat X2 ( 2, 100 ); | 
|---|
|  | 36 | X.set_row ( 0, x ); | 
|---|
|  | 37 | X2.set_row ( 0, x ); | 
|---|
|  | 38 |  | 
|---|
|  | 39 | mlstudent* Ap = Ar.predictor_student(); | 
|---|
|  | 40 | vec Ap_x = Ap->evallogcond_mat ( X, empty_vec ); | 
|---|
|  | 41 | vec ll_x = Ar.logpred_mat ( X2 ); | 
|---|
|  | 42 |  | 
|---|
|  | 43 | cout << "normalize : " << xstep*sum ( exp ( Ap_x ) ) << endl; | 
|---|
|  | 44 | cout << "normalize : " << xstep*sum ( exp ( ll_x ) ) << endl; | 
|---|
|  | 45 |  | 
|---|
|  | 46 | it_file it ( "arx_elem_test.it" ); | 
|---|
|  | 47 | it << Name ( "Ap_x" ) << Ap_x; | 
|---|
|  | 48 | it << Name ( "ll_x" ) << ll_x; | 
|---|
|  | 49 | } | 
|---|