| 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 | |
|---|
| 9 | namespace bdm { |
|---|
| 10 | |
|---|
| 11 | template<> |
|---|
| 12 | const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory( |
|---|
| 13 | ParticularUI<epdf_harness>("epdf_harness")); |
|---|
| 14 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 33 | if (set.exists("support")) { |
|---|
| 34 | UI::get(support, set, "support"); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | if (set.exists("nbins")) { |
|---|
| 38 | UI::get(nbins, set, "nbins"); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | if (set.exists("nsamples")) { |
|---|
| 42 | UI::get(nsamples, set, "nsamples"); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | if (set.exists("integral")) { |
|---|
| 46 | UI::get(integral, set, "integral"); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | if (set.exists("R")) { |
|---|
| 50 | UI::get(R, set, "R"); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | if (set.exists("tolerance")) { |
|---|
| 54 | UI::get(tolerance, set, "tolerance"); |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void epdf_harness::test(const char *config_name, int idx) |
|---|
| 59 | { |
|---|
| 60 | CurrentContext cc(config_name, idx); |
|---|
| 61 | |
|---|
| 62 | CHECK_CLOSE_EX(mean, hepdf->mean(), tolerance); |
|---|
| 63 | CHECK_CLOSE_EX(variance, hepdf->variance(), tolerance); |
|---|
| 64 | |
|---|
| 65 | if (support.rows() == 2) { |
|---|
| 66 | vec xb = support.get_row(0); |
|---|
| 67 | vec yb = support.get_row(1); |
|---|
| 68 | |
|---|
| 69 | int old_size = nbins.size(); |
|---|
| 70 | if (old_size < 2) { |
|---|
| 71 | vec new_nbins("100 100"); |
|---|
| 72 | for (int i = 0; i < old_size; ++i) { |
|---|
| 73 | new_nbins(i) = nbins(i); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | nbins = new_nbins; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | CHECK_CLOSE_EX(mean, num_mean2(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1); |
|---|
| 80 | CHECK_CLOSE_EX(integral, normcoef(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | if (R.rows() > 0) { |
|---|
| 84 | mat smp = hepdf->sample_m(nsamples); |
|---|
| 85 | int n = smp.cols(); |
|---|
| 86 | vec Emu = smp * ones(n) / n; |
|---|
| 87 | mat Er = (smp*smp.transpose())/n - outer_product(Emu,Emu); |
|---|
| 88 | CHECK_CLOSE_EX(mean, Emu, tolerance); |
|---|
| 89 | CHECK_CLOSE_EX(R, Er, tolerance); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | } |
|---|