[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" |
---|
[462] | 5 | #include "stat/emix.h" |
---|
[436] | 6 | #include "mat_checks.h" |
---|
| 7 | #include "test_util.h" |
---|
| 8 | #include "UnitTest++.h" |
---|
[462] | 9 | #include <memory> |
---|
[436] | 10 | |
---|
| 11 | namespace bdm { |
---|
| 12 | |
---|
| 13 | template<> |
---|
| 14 | const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory( |
---|
| 15 | ParticularUI<epdf_harness>("epdf_harness")); |
---|
| 16 | |
---|
[456] | 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 | |
---|
[436] | 30 | void epdf_harness::from_setting(const Setting &set) { |
---|
[471] | 31 | hepdf = UI::build<epdf>(set, "epdf", UI::compulsory); |
---|
| 32 | UI::get(mean, set, "mean", UI::compulsory); |
---|
| 33 | UI::get(variance, set, "variance", UI::compulsory); |
---|
[436] | 34 | |
---|
[441] | 35 | |
---|
[471] | 36 | UI::get(support, set, "support"); |
---|
| 37 | UI::get(nbins, set, "nbins"); |
---|
| 38 | UI::get(nsamples, set, "nsamples"); |
---|
| 39 | UI::get(R, set, "R"); |
---|
[442] | 40 | |
---|
[471] | 41 | RV* rv = UI::build<RV>(set, "marginal_rv"); |
---|
| 42 | if (rv) |
---|
| 43 | mrv = shared_ptr<RV>(rv); |
---|
[436] | 44 | |
---|
[471] | 45 | UI::get(tolerance, set, "tolerance"); |
---|
[436] | 46 | } |
---|
| 47 | |
---|
[456] | 48 | void epdf_harness::test(const char *config_name, int idx) |
---|
[436] | 49 | { |
---|
[456] | 50 | CurrentContext cc(config_name, idx); |
---|
[436] | 51 | |
---|
[456] | 52 | CHECK_CLOSE_EX(mean, hepdf->mean(), tolerance); |
---|
| 53 | CHECK_CLOSE_EX(variance, hepdf->variance(), tolerance); |
---|
| 54 | |
---|
[441] | 55 | if (support.rows() == 2) { |
---|
| 56 | vec xb = support.get_row(0); |
---|
| 57 | vec yb = support.get_row(1); |
---|
[442] | 58 | |
---|
| 59 | int old_size = nbins.size(); |
---|
| 60 | if (old_size < 2) { |
---|
| 61 | vec new_nbins("100 100"); |
---|
| 62 | for (int i = 0; i < old_size; ++i) { |
---|
| 63 | new_nbins(i) = nbins(i); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | nbins = new_nbins; |
---|
| 67 | } |
---|
| 68 | |
---|
[462] | 69 | CHECK_CLOSE_EX(mean, num_mean2(hepdf.get(), xb, yb, nbins(0), nbins(1)), tolerance); |
---|
| 70 | CHECK_CLOSE_EX(1.0, normcoef(hepdf.get(), xb, yb, nbins(0), nbins(1)), tolerance); |
---|
[441] | 71 | } |
---|
| 72 | |
---|
| 73 | if (R.rows() > 0) { |
---|
[442] | 74 | mat smp = hepdf->sample_m(nsamples); |
---|
[466] | 75 | vec emu = smp * ones(nsamples) / nsamples; |
---|
| 76 | mat er = (smp * smp.T()) / nsamples - outer_product(emu, emu); |
---|
[465] | 77 | |
---|
[469] | 78 | // simplify overloading for Visual Studio |
---|
| 79 | vec delta = sqrt(variance) / sqrt(static_cast<double>(nsamples)); |
---|
[465] | 80 | CHECK_CLOSE_EX(mean, emu, delta); |
---|
| 81 | |
---|
[461] | 82 | CHECK_CLOSE_EX(R, er, tolerance); |
---|
[441] | 83 | } |
---|
[462] | 84 | |
---|
| 85 | if (mrv.get()) { |
---|
| 86 | RV crv = hepdf->_rv().subt(*mrv); |
---|
| 87 | shared_ptr<epdf> m = hepdf->marginal(*mrv); |
---|
| 88 | shared_ptr<mpdf> c = hepdf->condition(crv); |
---|
| 89 | mepdf mm(m); |
---|
| 90 | |
---|
| 91 | Array<mpdf *> aa(2); |
---|
| 92 | aa(0) = c.get(); |
---|
| 93 | aa(1) = &mm; |
---|
| 94 | mprod mEp(aa); |
---|
| 95 | |
---|
[466] | 96 | mat smp = mEp.samplecond(vec(0), nsamples); |
---|
| 97 | vec emu = sum(smp, 2) / nsamples; |
---|
[462] | 98 | |
---|
[469] | 99 | // simplify overloading for Visual Studio |
---|
| 100 | vec delta = sqrt(variance) / sqrt(static_cast<double>(nsamples)); |
---|
[466] | 101 | CHECK_CLOSE_EX(mean, emu, delta); |
---|
| 102 | |
---|
[462] | 103 | if (R.rows() > 0) { |
---|
[466] | 104 | mat er = (smp * smp.T()) / nsamples - outer_product(emu, emu); |
---|
[462] | 105 | CHECK_CLOSE_EX(R, er, tolerance); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // test of pdflog at zero |
---|
| 109 | vec zero(0); |
---|
| 110 | vec zeron(hepdf->dimension()); |
---|
| 111 | for (int i = 0; i < zeron.size(); ++i) { |
---|
| 112 | zeron(i) = 0; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | CHECK_CLOSE_EX(hepdf->evallog(zeron), mEp.evallogcond(zeron, zero), tolerance); |
---|
| 116 | } |
---|
[436] | 117 | } |
---|
| 118 | |
---|
| 119 | } |
---|