[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 | |
---|
[480] | 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 | |
---|
[480] | 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 | |
---|
[477] | 61 | if ( R.rows() > 0 ) { |
---|
[481] | 62 | check_sample_mean(); |
---|
| 63 | check_covariance(); |
---|
[477] | 64 | } |
---|
[465] | 65 | |
---|
[529] | 66 | if ( mrv ) { |
---|
[477] | 67 | RV crv = hepdf->_rv().subt ( *mrv ); |
---|
[529] | 68 | epdf_ptr m = hepdf->marginal ( *mrv ); |
---|
| 69 | mpdf_ptr c = hepdf->condition ( crv ); |
---|
[462] | 70 | |
---|
[529] | 71 | mpdf_array aa ( 2 ); |
---|
[507] | 72 | aa ( 0 ) = c; |
---|
[675] | 73 | aa ( 1 ) = m; |
---|
[477] | 74 | mprod mEp ( aa ); |
---|
[462] | 75 | |
---|
[481] | 76 | check_cond_mean(mEp); |
---|
[477] | 77 | if ( R.rows() > 0 ) { |
---|
[481] | 78 | check_cond_covariance( mEp ); |
---|
[477] | 79 | } |
---|
[466] | 80 | |
---|
[477] | 81 | // test of pdflog at zero |
---|
| 82 | vec zero ( 0 ); |
---|
[598] | 83 | vec zeron=zeros ( hepdf->dimension() ); |
---|
[462] | 84 | |
---|
[502] | 85 | double lpz = hepdf->evallog ( zeron ); |
---|
[598] | 86 | double lpzc=mEp.evallogcond ( zeron, zero ); |
---|
| 87 | CHECK_CLOSE_EX ( lpz, lpzc, tolerance ); |
---|
[502] | 88 | |
---|
| 89 | vec lpzv(1); |
---|
| 90 | lpzv(0) = lpz; |
---|
| 91 | |
---|
| 92 | mat zero1n ( hepdf->dimension(), 1 ); |
---|
| 93 | for ( int i = 0; i < zero1n.rows(); ++i ) { |
---|
| 94 | zero1n ( i, 0 ) = 0; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | vec lpzv_act = hepdf->evallog_m ( zero1n ); |
---|
| 98 | CHECK_CLOSE_EX ( lpzv, lpzv_act, tolerance ); |
---|
| 99 | |
---|
| 100 | Array<vec> zeroa(3); |
---|
| 101 | lpzv = vec( zeroa.size() ); |
---|
| 102 | for ( int i = 0; i < zeroa.size(); ++i ) { |
---|
| 103 | zeroa(i) = zeron; |
---|
| 104 | lpzv(i) = lpz; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | lpzv_act = hepdf->evallog_m ( zeroa ); |
---|
| 108 | CHECK_CLOSE_EX ( lpzv, lpzv_act, tolerance ); |
---|
[462] | 109 | } |
---|
[436] | 110 | } |
---|
| 111 | |
---|
[481] | 112 | void epdf_harness::check_support_mean() { |
---|
| 113 | vec xb = support.get_row ( 0 ); |
---|
| 114 | vec yb = support.get_row ( 1 ); |
---|
| 115 | |
---|
| 116 | int tc = 0; |
---|
| 117 | Array<vec> actual(CurrentContext::max_trial_count); |
---|
| 118 | do { |
---|
| 119 | vec emu = num_mean2 ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ); |
---|
| 120 | actual( tc ) = emu; |
---|
| 121 | ++tc; |
---|
| 122 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 123 | !UnitTest::AreClose ( mean, actual( tc - 1 ), tolerance ) ); |
---|
| 124 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 125 | ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
| 126 | UnitTest::MemoryOutStream stream; |
---|
| 127 | stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << tolerance << " but was " << actual; |
---|
| 128 | |
---|
| 129 | UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); |
---|
| 130 | |
---|
| 131 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 132 | } |
---|
[436] | 133 | } |
---|
[481] | 134 | |
---|
| 135 | void epdf_harness::check_support_integral() { |
---|
| 136 | vec xb = support.get_row ( 0 ); |
---|
| 137 | vec yb = support.get_row ( 1 ); |
---|
| 138 | |
---|
| 139 | int tc = 0; |
---|
| 140 | Array<double> actual(CurrentContext::max_trial_count); |
---|
| 141 | do { |
---|
| 142 | double nc = normcoef ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ); |
---|
| 143 | actual( tc ) = nc; |
---|
| 144 | ++tc; |
---|
| 145 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 146 | !UnitTest::AreClose ( 1.0, actual( tc - 1 ), tolerance ) ); |
---|
| 147 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 148 | ( !UnitTest::AreClose ( 1.0, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
| 149 | UnitTest::MemoryOutStream stream; |
---|
| 150 | stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << tolerance << " but was " << actual; |
---|
| 151 | |
---|
| 152 | UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); |
---|
| 153 | |
---|
| 154 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | void epdf_harness::check_sample_mean() { |
---|
[484] | 159 | vec delta = make_close_tolerance ( variance, nsamples ); |
---|
[481] | 160 | |
---|
| 161 | int tc = 0; |
---|
| 162 | Array<vec> actual(CurrentContext::max_trial_count); |
---|
| 163 | do { |
---|
| 164 | mat smp = hepdf->sample_m ( nsamples ); |
---|
| 165 | vec emu = smp * ones ( nsamples ) / nsamples; |
---|
| 166 | actual( tc ) = emu; |
---|
| 167 | ++tc; |
---|
| 168 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 169 | !UnitTest::AreClose ( mean, actual( tc - 1 ), delta ) ); |
---|
| 170 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 171 | ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), delta ) ) ) { |
---|
| 172 | UnitTest::MemoryOutStream stream; |
---|
| 173 | stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << delta << " but was " << actual; |
---|
| 174 | |
---|
| 175 | UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); |
---|
| 176 | |
---|
| 177 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void epdf_harness::check_covariance() { |
---|
| 182 | int tc = 0; |
---|
| 183 | Array<mat> actual(CurrentContext::max_trial_count); |
---|
| 184 | do { |
---|
| 185 | mat smp = hepdf->sample_m ( nsamples ); |
---|
| 186 | vec emu = smp * ones ( nsamples ) / nsamples; |
---|
| 187 | mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); |
---|
| 188 | actual( tc ) = er; |
---|
| 189 | ++tc; |
---|
| 190 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 191 | !UnitTest::AreClose ( R, actual( tc - 1 ), tolerance ) ); |
---|
[489] | 192 | |
---|
[481] | 193 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 194 | ( !UnitTest::AreClose ( R, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
| 195 | UnitTest::MemoryOutStream stream; |
---|
| 196 | stream << CurrentContext::format_context(__LINE__) << "expected " << R << " +/- " << tolerance << " but was " << actual; |
---|
| 197 | |
---|
| 198 | UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); |
---|
| 199 | |
---|
| 200 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | void epdf_harness::check_cond_mean( mprod &mep ) { |
---|
[484] | 205 | vec delta = make_close_tolerance ( variance, nsamples ); |
---|
[481] | 206 | |
---|
| 207 | int tc = 0; |
---|
| 208 | Array<vec> actual(CurrentContext::max_trial_count); |
---|
| 209 | do { |
---|
[536] | 210 | mat smp = mep.samplecond_m ( vec ( 0 ), nsamples ); |
---|
[481] | 211 | vec emu = sum ( smp, 2 ) / nsamples; |
---|
| 212 | actual( tc ) = emu; |
---|
| 213 | ++tc; |
---|
| 214 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 215 | !UnitTest::AreClose ( mean, actual( tc - 1 ), delta ) ); |
---|
| 216 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 217 | ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), delta ) ) ) { |
---|
| 218 | UnitTest::MemoryOutStream stream; |
---|
| 219 | stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << delta << " but was " << actual; |
---|
| 220 | |
---|
| 221 | UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); |
---|
| 222 | |
---|
| 223 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | void epdf_harness::check_cond_covariance( mprod &mep ) { |
---|
| 228 | int tc = 0; |
---|
| 229 | Array<mat> actual(CurrentContext::max_trial_count); |
---|
| 230 | do { |
---|
[536] | 231 | mat smp = mep.samplecond_m ( vec ( 0 ), nsamples ); |
---|
[481] | 232 | vec emu = sum ( smp, 2 ) / nsamples; |
---|
| 233 | mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); |
---|
| 234 | actual( tc ) = er; |
---|
| 235 | ++tc; |
---|
| 236 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 237 | !UnitTest::AreClose ( R, actual( tc - 1 ), tolerance ) ); |
---|
| 238 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 239 | ( !UnitTest::AreClose ( R, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
| 240 | UnitTest::MemoryOutStream stream; |
---|
| 241 | stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << tolerance << " but was " << actual; |
---|
| 242 | |
---|
| 243 | UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); |
---|
| 244 | |
---|
| 245 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | } |
---|