| 1 | #include "epdf_harness.h" |
|---|
| 2 | #include "base/bdmbase.h" |
|---|
| 3 | #include "base/user_info.h" |
|---|
| 4 | #include "stat/exp_family.h" |
|---|
| 5 | #include "stat/emix.h" |
|---|
| 6 | #include "mat_checks.h" |
|---|
| 7 | #include "test_util.h" |
|---|
| 8 | #include "UnitTest++.h" |
|---|
| 9 | #include <memory> |
|---|
| 10 | |
|---|
| 11 | namespace bdm { |
|---|
| 12 | |
|---|
| 13 | template<> |
|---|
| 14 | const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory( |
|---|
| 15 | ParticularUI<epdf_harness>("epdf_harness")); |
|---|
| 16 | |
|---|
| 17 | void epdf_harness::test_config(const char *config_file_name) { |
|---|
| 18 | RV::clear_all(); |
|---|
| 19 | |
|---|
| 20 | UIFile in(config_file_name); |
|---|
| 21 | Array<epdf_harness *> input; |
|---|
| 22 | UI::get(input, in, "data"); |
|---|
| 23 | int sz = input.size(); |
|---|
| 24 | CHECK(sz > 0); |
|---|
| 25 | for (int i = 0; i < sz; ++i) { |
|---|
| 26 | input(i)->test(config_file_name, i); |
|---|
| 27 | } |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | void epdf_harness::from_setting(const Setting &set) { |
|---|
| 31 | hepdf = UI::build<epdf>(set, "epdf"); |
|---|
| 32 | UI::get(mean, set, "mean"); |
|---|
| 33 | UI::get(variance, set, "variance"); |
|---|
| 34 | |
|---|
| 35 | if (set.exists("support")) { |
|---|
| 36 | UI::get(support, set, "support"); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | if (set.exists("nbins")) { |
|---|
| 40 | UI::get(nbins, set, "nbins"); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | if (set.exists("nsamples")) { |
|---|
| 44 | UI::get(nsamples, set, "nsamples"); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | if (set.exists("R")) { |
|---|
| 48 | UI::get(R, set, "R"); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | if (set.exists("marginal_rv")) { |
|---|
| 52 | mrv = shared_ptr<RV>(UI::build<RV>(set, "marginal_rv")); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | if (set.exists("tolerance")) { |
|---|
| 56 | UI::get(tolerance, set, "tolerance"); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | void epdf_harness::test(const char *config_name, int idx) |
|---|
| 61 | { |
|---|
| 62 | CurrentContext cc(config_name, idx); |
|---|
| 63 | |
|---|
| 64 | CHECK_CLOSE_EX(mean, hepdf->mean(), tolerance); |
|---|
| 65 | CHECK_CLOSE_EX(variance, hepdf->variance(), tolerance); |
|---|
| 66 | |
|---|
| 67 | if (support.rows() == 2) { |
|---|
| 68 | vec xb = support.get_row(0); |
|---|
| 69 | vec yb = support.get_row(1); |
|---|
| 70 | |
|---|
| 71 | int old_size = nbins.size(); |
|---|
| 72 | if (old_size < 2) { |
|---|
| 73 | vec new_nbins("100 100"); |
|---|
| 74 | for (int i = 0; i < old_size; ++i) { |
|---|
| 75 | new_nbins(i) = nbins(i); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | nbins = new_nbins; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | CHECK_CLOSE_EX(mean, num_mean2(hepdf.get(), xb, yb, nbins(0), nbins(1)), tolerance); |
|---|
| 82 | CHECK_CLOSE_EX(1.0, normcoef(hepdf.get(), xb, yb, nbins(0), nbins(1)), tolerance); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if (R.rows() > 0) { |
|---|
| 86 | mat smp = hepdf->sample_m(nsamples); |
|---|
| 87 | int n = smp.cols(); |
|---|
| 88 | vec emu = smp * ones(n) / n; |
|---|
| 89 | mat er = (smp * smp.T()) / n - outer_product(emu, emu); |
|---|
| 90 | |
|---|
| 91 | vec delta = sqrt(variance) / sqrt(nsamples); |
|---|
| 92 | CHECK_CLOSE_EX(mean, emu, delta); |
|---|
| 93 | |
|---|
| 94 | CHECK_CLOSE_EX(R, er, tolerance); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | if (mrv.get()) { |
|---|
| 98 | RV crv = hepdf->_rv().subt(*mrv); |
|---|
| 99 | shared_ptr<epdf> m = hepdf->marginal(*mrv); |
|---|
| 100 | shared_ptr<mpdf> c = hepdf->condition(crv); |
|---|
| 101 | mepdf mm(m); |
|---|
| 102 | |
|---|
| 103 | Array<mpdf *> aa(2); |
|---|
| 104 | aa(0) = c.get(); |
|---|
| 105 | aa(1) = &mm; |
|---|
| 106 | mprod mEp(aa); |
|---|
| 107 | |
|---|
| 108 | int n = nsamples; |
|---|
| 109 | mat smp = mEp.samplecond(vec(0), n); |
|---|
| 110 | vec emu = sum(smp, 2) / n; |
|---|
| 111 | CHECK_CLOSE_EX(mean, emu, tolerance); |
|---|
| 112 | |
|---|
| 113 | if (R.rows() > 0) { |
|---|
| 114 | mat er = (smp * smp.T()) / n - outer_product(emu, emu); |
|---|
| 115 | CHECK_CLOSE_EX(R, er, tolerance); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | // test of pdflog at zero |
|---|
| 119 | vec zero(0); |
|---|
| 120 | vec zeron(hepdf->dimension()); |
|---|
| 121 | for (int i = 0; i < zeron.size(); ++i) { |
|---|
| 122 | zeron(i) = 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | CHECK_CLOSE_EX(hepdf->evallog(zeron), mEp.evallogcond(zeron, zero), tolerance); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | } |
|---|