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