| 1 | /*! |
|---|
| 2 | \file |
|---|
| 3 | \brief Test of basic elements of the ARX class |
|---|
| 4 | |
|---|
| 5 | See file \ref arx for mathematical background. |
|---|
| 6 | |
|---|
| 7 | This class tests functions ARX::bayes (Bayes rule) ARX::structure_est and ARX::predictor_student |
|---|
| 8 | |
|---|
| 9 | Untested functions: none. |
|---|
| 10 | |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | #include "estim/arx.h" |
|---|
| 14 | #include "../mat_checks.h" |
|---|
| 15 | |
|---|
| 16 | using namespace bdm; |
|---|
| 17 | |
|---|
| 18 | TEST ( arx_stress ) { |
|---|
| 19 | // Setup model |
|---|
| 20 | vec th ( "0.8 -0.3 0.4 0.01" ); |
|---|
| 21 | int ord = th.length(); //auxiliary variable |
|---|
| 22 | double sqr = 0.1; |
|---|
| 23 | |
|---|
| 24 | //Test constructor |
|---|
| 25 | mat V0 = 0.00001 * eye ( ord + 1 ); |
|---|
| 26 | V0 ( 0.0 ) = 1; // |
|---|
| 27 | double nu0 = ord + 5.0; |
|---|
| 28 | |
|---|
| 29 | ARX Ar; |
|---|
| 30 | Ar.set_statistics ( 1, V0, nu0 ); // Estimator |
|---|
| 31 | Ar.set_constant ( false ); |
|---|
| 32 | Ar.validate(); |
|---|
| 33 | const epdf& f_thr = Ar.posterior(); // refrence to posterior of the estimator |
|---|
| 34 | |
|---|
| 35 | //Test estimation |
|---|
| 36 | int ndat = 100; // number of data records |
|---|
| 37 | vec Yt ( ndat ); // Store generated data |
|---|
| 38 | Yt.set_subvector ( 0, randn ( ord ) ); //initial values |
|---|
| 39 | vec rgr ( ord ); // regressor |
|---|
| 40 | |
|---|
| 41 | //print moments of the prior distribution |
|---|
| 42 | cout << "prior mean: " << f_thr.mean() << endl; |
|---|
| 43 | cout << "prior variance: " << f_thr.variance() << endl; |
|---|
| 44 | |
|---|
| 45 | // cycle over time: |
|---|
| 46 | for ( int t = ord; t < ndat; t++ ) { |
|---|
| 47 | //Generate regressor |
|---|
| 48 | for ( int j = 0; j < ( ord ); j++ ) { |
|---|
| 49 | rgr ( j ) = Yt ( t - j - 1 ); |
|---|
| 50 | } |
|---|
| 51 | //model |
|---|
| 52 | Yt ( t ) = th * rgr + sqr * NorRNG(); |
|---|
| 53 | |
|---|
| 54 | Ar.bayes ( vec_1 ( Yt ( t ) ), rgr ); // Bayes rule |
|---|
| 55 | |
|---|
| 56 | // Build predictor |
|---|
| 57 | mlstudent* Pr = Ar.predictor_student ( ); |
|---|
| 58 | // Test similarity of likelihoods from the Bayes rule and the predictor |
|---|
| 59 | cout << "BR log-lik: " << Ar._ll(); |
|---|
| 60 | cout << " , predictor ll: " << Pr->evallogcond ( vec_1 ( Yt ( t ) ), rgr ) << endl; |
|---|
| 61 | delete Pr; |
|---|
| 62 | } |
|---|
| 63 | //print posterior moments |
|---|
| 64 | cout << "posterior mean: " << f_thr.mean() << endl; |
|---|
| 65 | cout << "posterior variance: " << f_thr.variance() << endl; |
|---|
| 66 | |
|---|
| 67 | // Test brute-froce structure estimation |
|---|
| 68 | |
|---|
| 69 | cout << "Structure estimation: " << endl; |
|---|
| 70 | cout << Ar.structure_est ( egiw ( 1, V0, nu0 ) ) << endl; |
|---|
| 71 | } |
|---|