[717] | 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" |
---|
[721] | 14 | #include "../mat_checks.h" |
---|
| 15 | |
---|
[717] | 16 | using namespace bdm; |
---|
| 17 | |
---|
[721] | 18 | TEST ( arx_stress ) { |
---|
[1064] | 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; |
---|
[717] | 23 | |
---|
[1064] | 24 | //Test constructor |
---|
| 25 | mat V0 = 0.00001 * eye ( ord + 1 ); |
---|
| 26 | V0 ( 0 ) = 1; // |
---|
| 27 | double nu0 = ord + 5.0; |
---|
[717] | 28 | |
---|
[1064] | 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 |
---|
[717] | 34 | |
---|
[1064] | 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 |
---|
[717] | 40 | |
---|
[1064] | 41 | //print moments of the prior distribution |
---|
| 42 | cout << "prior mean: " << f_thr.mean() << endl; |
---|
| 43 | cout << "prior variance: " << f_thr.variance() << endl; |
---|
[717] | 44 | |
---|
[1064] | 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(); |
---|
[717] | 53 | |
---|
[1064] | 54 | Ar.bayes ( vec_1 ( Yt ( t ) ), rgr ); // Bayes rule |
---|
[717] | 55 | |
---|
[1064] | 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; |
---|
[717] | 66 | |
---|
[1064] | 67 | // Test brute-froce structure estimation |
---|
[717] | 68 | |
---|
[1064] | 69 | cout << "Structure estimation: " << endl; |
---|
| 70 | cout << Ar.structure_est ( egiw ( 1, V0, nu0 ) ) << endl; |
---|
[717] | 71 | } |
---|