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 ) = 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 ( ); |
---|
58 | estudent<ldmat>* PrS = Ar.epredictor_student (rgr ); |
---|
59 | |
---|
60 | cout << "m1: " << Pr->_A()*rgr+Pr->_mu_const() << |
---|
61 | " v1:" << Pr->e()._R(); |
---|
62 | cout << "ms: " << PrS->mean() << " vs:" << PrS->variance() << |
---|
63 | " del:" << PrS->_delta() <<endl; |
---|
64 | |
---|
65 | // Test similarity of likelihoods from the Bayes rule and the predictor |
---|
66 | cout << "BR log-lik: " << Ar._ll(); |
---|
67 | cout << " , predictor ll: " << Pr->evallogcond ( vec_1 ( Yt ( t ) ), rgr ); |
---|
68 | cout << " , epredictor_student ll: " << PrS->evallog ( vec_1 ( Yt ( t ) ) ) << endl; |
---|
69 | delete Pr; |
---|
70 | delete PrS; |
---|
71 | } |
---|
72 | //print posterior moments |
---|
73 | cout << "posterior mean: " << f_thr.mean() << endl; |
---|
74 | cout << "posterior variance: " << f_thr.variance() << endl; |
---|
75 | |
---|
76 | // Test brute-froce structure estimation |
---|
77 | |
---|
78 | cout << "Structure estimation: " << endl; |
---|
79 | cout << Ar.structure_est ( egiw ( 1, V0, nu0 ) ) << endl; |
---|
80 | } |
---|