1 | #include "epdf_harness.h" |
---|
2 | #include "base/bdmbase.h" |
---|
3 | #include "base/user_info.h" |
---|
4 | #include "stat/exp_family.h" |
---|
5 | #include "stat/emix.h" |
---|
6 | #include "mat_checks.h" |
---|
7 | #include "test_util.h" |
---|
8 | #include "UnitTest++.h" |
---|
9 | #include <memory> |
---|
10 | |
---|
11 | namespace bdm { |
---|
12 | |
---|
13 | template<> |
---|
14 | const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory ( |
---|
15 | ParticularUI<epdf_harness> ( "epdf_harness" ) ); |
---|
16 | |
---|
17 | void epdf_harness::test_config ( const char *config_file_name ) { |
---|
18 | RV::clear_all(); |
---|
19 | |
---|
20 | UIFile in ( config_file_name ); |
---|
21 | Array<epdf_harness *> input; |
---|
22 | UI::get ( input, in, "data" ); |
---|
23 | int sz = input.size(); |
---|
24 | CHECK ( sz > 0 ); |
---|
25 | for ( int i = 0; i < sz; ++i ) { |
---|
26 | input ( i )->test ( config_file_name, i ); |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | void epdf_harness::from_setting ( const Setting &set ) { |
---|
31 | hepdf = UI::build<epdf> ( set, "epdf", UI::compulsory ); |
---|
32 | UI::get ( mean, set, "mean", UI::compulsory ); |
---|
33 | UI::get ( variance, set, "variance", UI::compulsory ); |
---|
34 | |
---|
35 | |
---|
36 | UI::get ( support, set, "support" ); |
---|
37 | UI::get ( nbins, set, "nbins" ); |
---|
38 | UI::get ( nsamples, set, "nsamples" ); |
---|
39 | UI::get ( R, set, "R" ); |
---|
40 | |
---|
41 | RV* rv = UI::build<RV> ( set, "marginal_rv" ); |
---|
42 | if ( rv ) |
---|
43 | mrv = shared_ptr<RV> ( rv ); |
---|
44 | |
---|
45 | UI::get ( tolerance, set, "tolerance" ); |
---|
46 | } |
---|
47 | |
---|
48 | void epdf_harness::test ( const char *config_name, int idx ) { |
---|
49 | CurrentContext cc ( config_name, idx ); |
---|
50 | |
---|
51 | CHECK_CLOSE_EX ( mean, hepdf->mean(), tolerance ); |
---|
52 | CHECK_CLOSE_EX ( variance, hepdf->variance(), tolerance ); |
---|
53 | |
---|
54 | if ( support.rows() == 2 ) { |
---|
55 | vec xb = support.get_row ( 0 ); |
---|
56 | vec yb = support.get_row ( 1 ); |
---|
57 | |
---|
58 | int old_size = nbins.size(); |
---|
59 | if ( old_size < 2 ) { |
---|
60 | vec new_nbins ( "100 100" ); |
---|
61 | for ( int i = 0; i < old_size; ++i ) { |
---|
62 | new_nbins ( i ) = nbins ( i ); |
---|
63 | } |
---|
64 | |
---|
65 | nbins = new_nbins; |
---|
66 | } |
---|
67 | |
---|
68 | CHECK_CLOSE_EX ( mean, num_mean2 ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ), tolerance ); |
---|
69 | CHECK_CLOSE_EX ( 1.0, normcoef ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ), tolerance ); |
---|
70 | } |
---|
71 | |
---|
72 | if ( R.rows() > 0 ) { |
---|
73 | mat smp = hepdf->sample_m ( nsamples ); |
---|
74 | vec emu = smp * ones ( nsamples ) / nsamples; |
---|
75 | mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); |
---|
76 | |
---|
77 | // simplify overloading for Visual Studio |
---|
78 | vec delta = sqrt ( variance ) / sqrt ( static_cast<double> ( nsamples ) ); |
---|
79 | CHECK_CLOSE_EX ( mean, emu, delta ); |
---|
80 | |
---|
81 | CHECK_CLOSE_EX ( R, er, tolerance ); |
---|
82 | } |
---|
83 | |
---|
84 | if ( mrv.get() ) { |
---|
85 | RV crv = hepdf->_rv().subt ( *mrv ); |
---|
86 | shared_ptr<epdf> m = hepdf->marginal ( *mrv ); |
---|
87 | shared_ptr<mpdf> c = hepdf->condition ( crv ); |
---|
88 | mepdf mm ( m ); |
---|
89 | |
---|
90 | Array<mpdf *> aa ( 2 ); |
---|
91 | aa ( 0 ) = c.get(); |
---|
92 | aa ( 1 ) = &mm; |
---|
93 | mprod mEp ( aa ); |
---|
94 | |
---|
95 | mat smp = mEp.samplecond ( vec ( 0 ), nsamples ); |
---|
96 | vec emu = sum ( smp, 2 ) / nsamples; |
---|
97 | |
---|
98 | // simplify overloading for Visual Studio |
---|
99 | vec delta = sqrt ( variance ) / sqrt ( static_cast<double> ( nsamples ) ); |
---|
100 | CHECK_CLOSE_EX ( mean, emu, delta ); |
---|
101 | |
---|
102 | if ( R.rows() > 0 ) { |
---|
103 | mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); |
---|
104 | CHECK_CLOSE_EX ( R, er, tolerance ); |
---|
105 | } |
---|
106 | |
---|
107 | // test of pdflog at zero |
---|
108 | vec zero ( 0 ); |
---|
109 | vec zeron ( hepdf->dimension() ); |
---|
110 | for ( int i = 0; i < zeron.size(); ++i ) { |
---|
111 | zeron ( i ) = 0; |
---|
112 | } |
---|
113 | |
---|
114 | CHECK_CLOSE_EX ( hepdf->evallog ( zeron ), mEp.evallogcond ( zeron, zero ), tolerance ); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | } |
---|