[263] | 1 | /*! |
---|
[477] | 2 | \file |
---|
[263] | 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 | |
---|
[386] | 13 | #include "estim/arx.h" |
---|
[254] | 14 | using namespace bdm; |
---|
[100] | 15 | |
---|
| 16 | int main() { |
---|
| 17 | // Setup model |
---|
[263] | 18 | vec th ( "0.8 -0.3 0.4 0.01" ); |
---|
[477] | 19 | int ord = th.length(); //auxiliary variable |
---|
| 20 | double sqr = 0.1; |
---|
[263] | 21 | |
---|
[100] | 22 | //Test constructor |
---|
[477] | 23 | mat V0 = 0.00001 * eye ( ord + 1 ); |
---|
| 24 | V0 ( 0.0 ) = 1; // |
---|
| 25 | double nu0 = ord + 5.0; |
---|
[263] | 26 | |
---|
[270] | 27 | ARX Ar; |
---|
[477] | 28 | Ar.set_statistics ( 1, V0, nu0 ); // Estimator |
---|
[271] | 29 | const epdf& f_thr = Ar.posterior(); // refrence to posterior of the estimator |
---|
[263] | 30 | |
---|
[100] | 31 | //Test estimation |
---|
[263] | 32 | int ndat = 100; // number of data records |
---|
| 33 | vec Yt ( ndat ); // Store generated data |
---|
[477] | 34 | Yt.set_subvector ( 0, randn ( ord ) ); //initial values |
---|
[263] | 35 | vec rgr ( ord ); // regressor |
---|
[477] | 36 | vec Psi ( ord + 1 ); // extended regressor |
---|
[263] | 37 | |
---|
| 38 | //print moments of the prior distribution |
---|
[477] | 39 | cout << "prior mean: " << f_thr.mean() << endl; |
---|
| 40 | cout << "prior variance: " << f_thr.variance() << endl; |
---|
[263] | 41 | |
---|
| 42 | // cycle over time: |
---|
[477] | 43 | for ( int t = ord; t < ndat; t++ ) { |
---|
[263] | 44 | //Generate regressor |
---|
[477] | 45 | for ( int j = 0; j < ( ord ); j++ ) { |
---|
| 46 | rgr ( j ) = Yt ( t - j - 1 ); |
---|
| 47 | } |
---|
[263] | 48 | //model |
---|
[477] | 49 | Yt ( t ) = th * rgr + sqr * NorRNG(); |
---|
[263] | 50 | |
---|
| 51 | Psi = concat ( Yt ( t ), rgr ); // Inefficient! Used for compatibility with Matlab! |
---|
| 52 | Ar.bayes ( Psi ); // Bayes rule |
---|
| 53 | |
---|
| 54 | // Build predictor |
---|
[270] | 55 | mlstudent* Pr = Ar.predictor_student ( ); |
---|
[263] | 56 | // Test similarity of likelihoods from the Bayes rule and the predictor |
---|
| 57 | cout << "BR log-lik: " << Ar._ll(); |
---|
[477] | 58 | cout << " , predictor ll: " << Pr->evallogcond ( vec_1 ( Yt ( t ) ), rgr ) << endl; |
---|
[198] | 59 | delete Pr; |
---|
[100] | 60 | } |
---|
[263] | 61 | //print posterior moments |
---|
[477] | 62 | cout << "posterior mean: " << f_thr.mean() << endl; |
---|
| 63 | cout << "posterior variance: " << f_thr.variance() << endl; |
---|
[263] | 64 | |
---|
| 65 | // Test brute-froce structure estimation |
---|
[477] | 66 | |
---|
| 67 | cout << "Structure estimation: " << endl; |
---|
| 68 | cout << Ar.structure_est ( egiw ( 1, V0, nu0 ) ) << endl; |
---|
[100] | 69 | } |
---|