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