1 | #include "epdf_harness.h" |
---|
2 | #include "base/bdmbase.h" |
---|
3 | #include "base/user_info.h" |
---|
4 | #include "stat/exp_family.h" |
---|
5 | #include "mat_checks.h" |
---|
6 | #include "test_util.h" |
---|
7 | #include "UnitTest++.h" |
---|
8 | #include <memory> |
---|
9 | |
---|
10 | namespace bdm { |
---|
11 | |
---|
12 | void epdf_harness::test_config ( const char *config_file_name ) { |
---|
13 | RV::clear_all(); |
---|
14 | |
---|
15 | UIFile in ( config_file_name ); |
---|
16 | Array<shared_ptr<epdf_harness> > input; |
---|
17 | UI::get ( input, in, "data", UI::compulsory ); |
---|
18 | int sz = input.size(); |
---|
19 | CHECK ( sz > 0 ); |
---|
20 | for ( int i = 0; i < sz; ++i ) { |
---|
21 | input ( i )->test ( config_file_name, i ); |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | void epdf_harness::from_setting ( const Setting &set ) { |
---|
26 | hepdf = UI::build<epdf> ( set, "epdf", UI::compulsory ); |
---|
27 | UI::get ( mean, set, "mean", UI::compulsory ); |
---|
28 | UI::get ( variance, set, "variance", UI::compulsory ); |
---|
29 | |
---|
30 | support = UI::build<rectangular_support> ( set, "support", UI::optional ); |
---|
31 | UI::get ( nsamples, set, "nsamples", UI::optional ); |
---|
32 | UI::get ( R, set, "R", UI::optional ); |
---|
33 | |
---|
34 | mrv = UI::build<RV> ( set, "marginal_rv", UI::optional ); |
---|
35 | |
---|
36 | UI::get ( tolerance, set, "tolerance", UI::optional ); |
---|
37 | } |
---|
38 | |
---|
39 | void epdf_harness::test ( const char *config_name, int idx ) { |
---|
40 | CurrentContext cc ( config_name, idx ); |
---|
41 | |
---|
42 | CHECK_CLOSE_EX ( mean, hepdf->mean(), tolerance ); |
---|
43 | CHECK_CLOSE_EX ( variance, hepdf->variance(), tolerance ); |
---|
44 | |
---|
45 | if ( support ) { // support is given |
---|
46 | grid_fnc ep_disc; |
---|
47 | ep_disc.set_support ( *support ); |
---|
48 | ep_disc.set_values ( *hepdf ); |
---|
49 | // ep_disc is discretized at support points |
---|
50 | |
---|
51 | double point_volume = prod ( support->_steps() ); |
---|
52 | CHECK_CLOSE ( 1.0, sum ( ep_disc._values() ) *point_volume, 0.01 ); |
---|
53 | |
---|
54 | vec pdf = ep_disc._values(); |
---|
55 | pdf /= sum ( pdf ); // normalize |
---|
56 | |
---|
57 | vec mea = pdf ( 0 ) * support->first_vec(); |
---|
58 | mat Remp = pdf ( 0 ) * outer_product ( support->act_vec(), support->act_vec() ); |
---|
59 | |
---|
60 | // run through all points |
---|
61 | for ( int i = 1; i < support->points(); i++ ) { |
---|
62 | mea += pdf ( i ) * support->next_vec(); |
---|
63 | Remp += pdf ( i ) * outer_product ( support->act_vec(), support->act_vec() ); |
---|
64 | } |
---|
65 | CHECK_CLOSE ( mean, mea, tolerance ); |
---|
66 | CHECK_CLOSE ( R, Remp - outer_product ( mea, mea ), tolerance ); |
---|
67 | } |
---|
68 | |
---|
69 | if ( variance.length() > 0 ) { |
---|
70 | check_sample_mean(); |
---|
71 | } |
---|
72 | if ( R.rows() > 0 ) { |
---|
73 | check_covariance(); |
---|
74 | } |
---|
75 | |
---|
76 | if ( mrv ) { |
---|
77 | RV crv = hepdf->_rv().subt ( *mrv ); |
---|
78 | epdf_ptr m = hepdf->marginal ( *mrv ); |
---|
79 | pdf_ptr c = hepdf->condition ( crv ); |
---|
80 | |
---|
81 | pdf_array aa ( 2 ); |
---|
82 | aa ( 0 ) = c; |
---|
83 | aa ( 1 ) = m; |
---|
84 | mprod mEp ( aa ); |
---|
85 | |
---|
86 | check_cond_mean ( mEp ); |
---|
87 | if ( R.rows() > 0 ) { |
---|
88 | check_cond_covariance ( mEp ); |
---|
89 | } |
---|
90 | |
---|
91 | // test of pdflog at zero |
---|
92 | vec zero ( 0 ); |
---|
93 | vec zeron = zeros ( hepdf->dimension() ); |
---|
94 | |
---|
95 | double lpz = hepdf->evallog ( zeron ); |
---|
96 | double lpzc = mEp.evallogcond ( zeron, zero ); |
---|
97 | CHECK_CLOSE_EX ( lpz, lpzc, tolerance ); |
---|
98 | |
---|
99 | vec lpzv ( 1 ); |
---|
100 | lpzv ( 0 ) = lpz; |
---|
101 | |
---|
102 | mat zero1n ( hepdf->dimension(), 1 ); |
---|
103 | for ( int i = 0; i < zero1n.rows(); ++i ) { |
---|
104 | zero1n ( i, 0 ) = 0; |
---|
105 | } |
---|
106 | |
---|
107 | vec lpzv_act = hepdf->evallog_mat ( zero1n ); |
---|
108 | CHECK_CLOSE_EX ( lpzv, lpzv_act, tolerance ); |
---|
109 | |
---|
110 | Array<vec> zeroa ( 3 ); |
---|
111 | lpzv = vec ( zeroa.size() ); |
---|
112 | for ( int i = 0; i < zeroa.size(); ++i ) { |
---|
113 | zeroa ( i ) = zeron; |
---|
114 | lpzv ( i ) = lpz; |
---|
115 | } |
---|
116 | |
---|
117 | lpzv_act = hepdf->evallog_mat ( zeroa ); |
---|
118 | CHECK_CLOSE_EX ( lpzv, lpzv_act, tolerance ); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | void epdf_harness::check_sample_mean() { |
---|
124 | vec delta = make_close_tolerance ( variance, nsamples ); |
---|
125 | |
---|
126 | int tc = 0; |
---|
127 | Array<vec> actual ( CurrentContext::max_trial_count ); |
---|
128 | do { |
---|
129 | mat smp = hepdf->sample_mat ( nsamples ); |
---|
130 | vec emu = smp * ones ( nsamples ) / nsamples; |
---|
131 | actual ( tc ) = emu; |
---|
132 | ++tc; |
---|
133 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
134 | !UnitTest::AreClose ( mean, actual ( tc - 1 ), delta ) ); |
---|
135 | |
---|
136 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
137 | ( !UnitTest::AreClose ( mean, actual ( CurrentContext::max_trial_count - 1 ), delta ) ) ) { |
---|
138 | UnitTest::MemoryOutStream stream; |
---|
139 | stream << CurrentContext::format_context ( __LINE__ ) << "expected " << mean << " +/- " << delta << " but was " << actual; |
---|
140 | |
---|
141 | UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), 0, false ); |
---|
142 | |
---|
143 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | void epdf_harness::check_covariance() { |
---|
148 | int tc = 0; |
---|
149 | Array<mat> actual ( CurrentContext::max_trial_count ); |
---|
150 | do { |
---|
151 | mat smp = hepdf->sample_mat ( nsamples ); |
---|
152 | vec emu = smp * ones ( nsamples ) / nsamples; |
---|
153 | mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); |
---|
154 | actual ( tc ) = er; |
---|
155 | ++tc; |
---|
156 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
157 | !UnitTest::AreClose ( R, actual ( tc - 1 ), tolerance ) ); |
---|
158 | |
---|
159 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
160 | ( !UnitTest::AreClose ( R, actual ( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
161 | UnitTest::MemoryOutStream stream; |
---|
162 | stream << CurrentContext::format_context ( __LINE__ ) << "expected " << R << " +/- " << tolerance << " but was " << actual; |
---|
163 | |
---|
164 | UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), 0, false ); |
---|
165 | |
---|
166 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | void epdf_harness::check_cond_mean ( mprod &mep ) { |
---|
171 | vec delta = make_close_tolerance ( variance, nsamples ); |
---|
172 | |
---|
173 | int tc = 0; |
---|
174 | Array<vec> actual ( CurrentContext::max_trial_count ); |
---|
175 | do { |
---|
176 | mat smp = mep.samplecond_mat ( vec ( 0 ), nsamples ); |
---|
177 | vec emu = sum ( smp, 2 ) / nsamples; |
---|
178 | actual ( tc ) = emu; |
---|
179 | ++tc; |
---|
180 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
181 | !UnitTest::AreClose ( mean, actual ( tc - 1 ), delta ) ); |
---|
182 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
183 | ( !UnitTest::AreClose ( mean, actual ( CurrentContext::max_trial_count - 1 ), delta ) ) ) { |
---|
184 | UnitTest::MemoryOutStream stream; |
---|
185 | stream << CurrentContext::format_context ( __LINE__ ) << "expected " << mean << " +/- " << delta << " but was " << actual; |
---|
186 | |
---|
187 | UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), 0, false ); |
---|
188 | |
---|
189 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | void epdf_harness::check_cond_covariance ( mprod &mep ) { |
---|
194 | int tc = 0; |
---|
195 | Array<mat> actual ( CurrentContext::max_trial_count ); |
---|
196 | do { |
---|
197 | mat smp = mep.samplecond_mat ( vec ( 0 ), nsamples ); |
---|
198 | vec emu = sum ( smp, 2 ) / nsamples; |
---|
199 | mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); |
---|
200 | actual ( tc ) = er; |
---|
201 | ++tc; |
---|
202 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
203 | !UnitTest::AreClose ( R, actual ( tc - 1 ), tolerance ) ); |
---|
204 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
205 | ( !UnitTest::AreClose ( R, actual ( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
206 | UnitTest::MemoryOutStream stream; |
---|
207 | stream << CurrentContext::format_context ( __LINE__ ) << "expected " << mean << " +/- " << tolerance << " but was " << actual; |
---|
208 | |
---|
209 | UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), 0, false ); |
---|
210 | |
---|
211 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | } |
---|