[693] | 1 | #include "pdf_harness.h" |
---|
[456] | 2 | #include "base/bdmbase.h" |
---|
| 3 | #include "base/user_info.h" |
---|
[447] | 4 | #include "stat/exp_family.h" |
---|
| 5 | #include "mat_checks.h" |
---|
| 6 | #include "test_util.h" |
---|
| 7 | #include "UnitTest++.h" |
---|
| 8 | |
---|
| 9 | namespace bdm { |
---|
| 10 | |
---|
[693] | 11 | void pdf_harness::test_config ( const char *config_file_name ) { |
---|
[1064] | 12 | RV::clear_all(); |
---|
[456] | 13 | |
---|
[1064] | 14 | UIFile in ( config_file_name ); |
---|
| 15 | Array<shared_ptr<pdf_harness> > input; |
---|
| 16 | UI::get ( input, in, "data", UI::compulsory ); |
---|
| 17 | int sz = input.size(); |
---|
| 18 | CHECK ( sz > 0 ); |
---|
| 19 | for ( int i = 0; i < sz; ++i ) { |
---|
| 20 | input ( i )->test ( config_file_name, i ); |
---|
| 21 | } |
---|
[456] | 22 | } |
---|
| 23 | |
---|
[693] | 24 | void pdf_harness::from_setting ( const Setting &set ) { |
---|
[1064] | 25 | hpdf = UI::build<pdf> ( set, "pdf", UI::compulsory ); |
---|
| 26 | UI::get ( mean, set, "mean", UI::compulsory ); |
---|
| 27 | UI::get ( variance, set, "variance", UI::compulsory ); |
---|
[447] | 28 | |
---|
[1064] | 29 | if ( !UI::get ( cond, set, "cond", UI::optional ) ) { |
---|
| 30 | cond = zeros ( hpdf->dimensionc() ); |
---|
| 31 | } |
---|
[484] | 32 | |
---|
[1064] | 33 | UI::get ( nsamples, set, "nsamples", UI::optional ); |
---|
| 34 | UI::get ( R, set, "R", UI::optional ); |
---|
| 35 | UI::get ( tolerance, set, "tolerance", UI::optional ); |
---|
[447] | 36 | } |
---|
| 37 | |
---|
[693] | 38 | void pdf_harness::test ( const char *config_name, int idx ) { |
---|
[1064] | 39 | CurrentContext cc ( config_name, idx ); |
---|
| 40 | check_mean(); |
---|
| 41 | if ( R.rows() > 0 ) { |
---|
| 42 | check_covariance(); |
---|
| 43 | } |
---|
[447] | 44 | } |
---|
| 45 | |
---|
[693] | 46 | void pdf_harness::check_mean() { |
---|
[1064] | 47 | vec delta = make_close_tolerance ( variance, nsamples ); |
---|
[484] | 48 | |
---|
[1064] | 49 | int tc = 0; |
---|
| 50 | Array<vec> actual ( CurrentContext::max_trial_count ); |
---|
| 51 | do { |
---|
| 52 | mat smp = hpdf->samplecond_mat ( cond, nsamples ); |
---|
| 53 | vec emu = smp * ones ( nsamples ) / nsamples; |
---|
| 54 | actual ( tc ) = emu; |
---|
| 55 | ++tc; |
---|
| 56 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 57 | !UnitTest::AreClose ( mean, actual ( tc - 1 ), delta ) ); |
---|
| 58 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 59 | ( !UnitTest::AreClose ( mean, actual ( CurrentContext::max_trial_count - 1 ), delta ) ) ) { |
---|
| 60 | UnitTest::MemoryOutStream stream; |
---|
| 61 | stream << CurrentContext::format_context ( __LINE__ ) << "expected " << mean << " +/- " << delta << " but was " << actual; |
---|
[484] | 62 | |
---|
[1064] | 63 | UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), 0, false ); |
---|
[484] | 64 | |
---|
[1064] | 65 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 66 | } |
---|
[447] | 67 | } |
---|
[484] | 68 | |
---|
[693] | 69 | void pdf_harness::check_covariance() { |
---|
[1064] | 70 | int tc = 0; |
---|
| 71 | Array<mat> actual ( CurrentContext::max_trial_count ); |
---|
| 72 | do { |
---|
| 73 | mat smp = hpdf->samplecond_mat ( cond, nsamples ); |
---|
| 74 | vec emu = smp * ones ( nsamples ) / nsamples; |
---|
| 75 | mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); |
---|
| 76 | actual ( tc ) = er; |
---|
| 77 | ++tc; |
---|
| 78 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 79 | !UnitTest::AreClose ( R, actual ( tc - 1 ), tolerance ) ); |
---|
| 80 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 81 | ( !UnitTest::AreClose ( R, actual ( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
| 82 | UnitTest::MemoryOutStream stream; |
---|
| 83 | stream << CurrentContext::format_context ( __LINE__ ) << "expected " << R << " +/- " << tolerance << " but was " << actual; |
---|
[484] | 84 | |
---|
[1064] | 85 | UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), 0, false ); |
---|
[484] | 86 | |
---|
[1064] | 87 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 88 | } |
---|
[484] | 89 | } |
---|
| 90 | |
---|
| 91 | } |
---|