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