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 | vec emu = smp * ones(nsamples) / nsamples; |
---|
88 | mat er = (smp * smp.T()) / nsamples - outer_product(emu, emu); |
---|
89 | |
---|
90 | vec delta = sqrt(variance) / sqrt(nsamples); |
---|
91 | CHECK_CLOSE_EX(mean, emu, delta); |
---|
92 | |
---|
93 | CHECK_CLOSE_EX(R, er, tolerance); |
---|
94 | } |
---|
95 | |
---|
96 | if (mrv.get()) { |
---|
97 | RV crv = hepdf->_rv().subt(*mrv); |
---|
98 | shared_ptr<epdf> m = hepdf->marginal(*mrv); |
---|
99 | shared_ptr<mpdf> c = hepdf->condition(crv); |
---|
100 | mepdf mm(m); |
---|
101 | |
---|
102 | Array<mpdf *> aa(2); |
---|
103 | aa(0) = c.get(); |
---|
104 | aa(1) = &mm; |
---|
105 | mprod mEp(aa); |
---|
106 | |
---|
107 | mat smp = mEp.samplecond(vec(0), nsamples); |
---|
108 | vec emu = sum(smp, 2) / nsamples; |
---|
109 | |
---|
110 | vec delta = sqrt(variance) / sqrt(nsamples); |
---|
111 | CHECK_CLOSE_EX(mean, emu, delta); |
---|
112 | |
---|
113 | if (R.rows() > 0) { |
---|
114 | mat er = (smp * smp.T()) / nsamples - 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 | } |
---|