[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" |
---|
| 8 | |
---|
| 9 | namespace bdm { |
---|
| 10 | |
---|
| 11 | template<> |
---|
| 12 | const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory( |
---|
| 13 | ParticularUI<epdf_harness>("epdf_harness")); |
---|
| 14 | |
---|
[456] | 15 | void epdf_harness::test_config(const char *config_file_name) { |
---|
| 16 | RV::clear_all(); |
---|
| 17 | |
---|
| 18 | UIFile in(config_file_name); |
---|
| 19 | Array<epdf_harness *> input; |
---|
| 20 | UI::get(input, in, "data"); |
---|
| 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 | |
---|
[436] | 28 | void epdf_harness::from_setting(const Setting &set) { |
---|
| 29 | hepdf = UI::build<epdf>(set, "epdf"); |
---|
| 30 | UI::get(mean, set, "mean"); |
---|
| 31 | UI::get(variance, set, "variance"); |
---|
| 32 | |
---|
[441] | 33 | if (set.exists("support")) { |
---|
| 34 | UI::get(support, set, "support"); |
---|
| 35 | } |
---|
| 36 | |
---|
[442] | 37 | if (set.exists("nbins")) { |
---|
| 38 | UI::get(nbins, set, "nbins"); |
---|
| 39 | } |
---|
| 40 | |
---|
[436] | 41 | if (set.exists("nsamples")) { |
---|
| 42 | UI::get(nsamples, set, "nsamples"); |
---|
| 43 | } |
---|
| 44 | |
---|
[441] | 45 | if (set.exists("R")) { |
---|
| 46 | UI::get(R, set, "R"); |
---|
| 47 | } |
---|
[456] | 48 | |
---|
| 49 | if (set.exists("tolerance")) { |
---|
| 50 | UI::get(tolerance, set, "tolerance"); |
---|
| 51 | } |
---|
[436] | 52 | } |
---|
| 53 | |
---|
[456] | 54 | void epdf_harness::test(const char *config_name, int idx) |
---|
[436] | 55 | { |
---|
[456] | 56 | CurrentContext cc(config_name, idx); |
---|
[436] | 57 | |
---|
[456] | 58 | CHECK_CLOSE_EX(mean, hepdf->mean(), tolerance); |
---|
| 59 | CHECK_CLOSE_EX(variance, hepdf->variance(), tolerance); |
---|
| 60 | |
---|
[441] | 61 | if (support.rows() == 2) { |
---|
| 62 | vec xb = support.get_row(0); |
---|
| 63 | vec yb = support.get_row(1); |
---|
[442] | 64 | |
---|
| 65 | int old_size = nbins.size(); |
---|
| 66 | if (old_size < 2) { |
---|
| 67 | vec new_nbins("100 100"); |
---|
| 68 | for (int i = 0; i < old_size; ++i) { |
---|
| 69 | new_nbins(i) = nbins(i); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | nbins = new_nbins; |
---|
| 73 | } |
---|
| 74 | |
---|
[456] | 75 | CHECK_CLOSE_EX(mean, num_mean2(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1); |
---|
[458] | 76 | CHECK_CLOSE_EX(1.0, normcoef(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1); |
---|
[441] | 77 | } |
---|
| 78 | |
---|
| 79 | if (R.rows() > 0) { |
---|
[442] | 80 | mat smp = hepdf->sample_m(nsamples); |
---|
[447] | 81 | int n = smp.cols(); |
---|
[461] | 82 | vec emu = smp * ones(n) / n; |
---|
| 83 | mat er = (smp * smp.T()) / n - outer_product(emu, emu); |
---|
| 84 | CHECK_CLOSE_EX(mean, emu, tolerance); |
---|
| 85 | CHECK_CLOSE_EX(R, er, tolerance); |
---|
[441] | 86 | } |
---|
[436] | 87 | } |
---|
| 88 | |
---|
| 89 | } |
---|