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