| 1 | #include "epdf_harness.h" |
|---|
| 2 | #include "stat/exp_family.h" |
|---|
| 3 | #include "mat_checks.h" |
|---|
| 4 | #include "test_util.h" |
|---|
| 5 | #include "UnitTest++.h" |
|---|
| 6 | |
|---|
| 7 | const double epsilon = 0.00001; |
|---|
| 8 | |
|---|
| 9 | namespace UnitTest |
|---|
| 10 | { |
|---|
| 11 | |
|---|
| 12 | inline void CheckClose(TestResults &results, const itpp::vec &expected, |
|---|
| 13 | const itpp::vec &actual, double tolerance, |
|---|
| 14 | TestDetails const &details) { |
|---|
| 15 | if (!AreClose(expected, actual, tolerance)) { |
|---|
| 16 | MemoryOutStream stream; |
|---|
| 17 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
|---|
| 18 | |
|---|
| 19 | results.OnTestFailure(details, stream.GetText()); |
|---|
| 20 | } |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | inline void CheckClose(TestResults &results, const itpp::mat &expected, |
|---|
| 24 | const itpp::mat &actual, double tolerance, |
|---|
| 25 | TestDetails const &details) { |
|---|
| 26 | if (!AreClose(expected, actual, tolerance)) { |
|---|
| 27 | MemoryOutStream stream; |
|---|
| 28 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
|---|
| 29 | |
|---|
| 30 | results.OnTestFailure(details, stream.GetText()); |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | namespace bdm { |
|---|
| 37 | |
|---|
| 38 | template<> |
|---|
| 39 | const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory( |
|---|
| 40 | ParticularUI<epdf_harness>("epdf_harness")); |
|---|
| 41 | |
|---|
| 42 | void epdf_harness::from_setting(const Setting &set) { |
|---|
| 43 | hepdf = UI::build<epdf>(set, "epdf"); |
|---|
| 44 | UI::get(mean, set, "mean"); |
|---|
| 45 | UI::get(variance, set, "variance"); |
|---|
| 46 | |
|---|
| 47 | if (set.exists("support")) { |
|---|
| 48 | UI::get(support, set, "support"); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | if (set.exists("nbins")) { |
|---|
| 52 | UI::get(nbins, set, "nbins"); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | if (set.exists("nsamples")) { |
|---|
| 56 | UI::get(nsamples, set, "nsamples"); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | if (set.exists("integral")) { |
|---|
| 60 | UI::get(integral, set, "integral"); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | if (set.exists("R")) { |
|---|
| 64 | UI::get(R, set, "R"); |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void epdf_harness::test() |
|---|
| 69 | { |
|---|
| 70 | CHECK_CLOSE(mean, hepdf->mean(), epsilon); |
|---|
| 71 | CHECK_CLOSE(variance, hepdf->variance(), epsilon); |
|---|
| 72 | |
|---|
| 73 | if (support.rows() == 2) { |
|---|
| 74 | vec xb = support.get_row(0); |
|---|
| 75 | vec yb = support.get_row(1); |
|---|
| 76 | |
|---|
| 77 | int old_size = nbins.size(); |
|---|
| 78 | if (old_size < 2) { |
|---|
| 79 | vec new_nbins("100 100"); |
|---|
| 80 | for (int i = 0; i < old_size; ++i) { |
|---|
| 81 | new_nbins(i) = nbins(i); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | nbins = new_nbins; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | CHECK_CLOSE(mean, num_mean2(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1); |
|---|
| 88 | CHECK_CLOSE(integral, normcoef(hepdf.get(), xb, yb, nbins(0), nbins(1)), 0.1); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | if (R.rows() > 0) { |
|---|
| 92 | mat smp = hepdf->sample_m(nsamples); |
|---|
| 93 | int n = smp.cols(); |
|---|
| 94 | vec Emu = smp * ones(n) / n; |
|---|
| 95 | mat Er = (smp*smp.transpose())/n - outer_product(Emu,Emu); |
|---|
| 96 | CHECK_CLOSE(mean, Emu, 0.1); |
|---|
| 97 | CHECK_CLOSE(R, Er, 0.1); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | } |
|---|