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