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