1 | #define BDMLIB // not an ideal way to prevent double registration of UI factories... |
---|
2 | #include "base/bdmbase.h" |
---|
3 | #include "base/user_info.h" |
---|
4 | #include "stat/exp_family.h" |
---|
5 | #include "stat/emix.h" |
---|
6 | #include "itpp_ext.h" |
---|
7 | #include "mpdf_harness.h" |
---|
8 | #include "mat_checks.h" |
---|
9 | #include "UnitTest++.h" |
---|
10 | |
---|
11 | using namespace bdm; |
---|
12 | |
---|
13 | const double epsilon = 0.00001; |
---|
14 | |
---|
15 | namespace UnitTest |
---|
16 | { |
---|
17 | |
---|
18 | inline void CheckClose(TestResults &results, const itpp::vec &expected, |
---|
19 | const itpp::vec &actual, double tolerance, |
---|
20 | TestDetails const& details) { |
---|
21 | if (!AreClose(expected, actual, tolerance)) { |
---|
22 | MemoryOutStream stream; |
---|
23 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
24 | |
---|
25 | results.OnTestFailure(details, stream.GetText()); |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | inline void CheckClose(TestResults &results, const itpp::mat &expected, |
---|
30 | const itpp::mat &actual, double tolerance, |
---|
31 | TestDetails const& details) { |
---|
32 | if (!AreClose(expected, actual, tolerance)) { |
---|
33 | MemoryOutStream stream; |
---|
34 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
35 | |
---|
36 | results.OnTestFailure(details, stream.GetText()); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | TEST(test_mepdf) { |
---|
43 | RV::clear_all(); |
---|
44 | UIFile in("mepdf.cfg"); |
---|
45 | Array<mpdf_harness *> input; |
---|
46 | UI::get(input, in, "data"); |
---|
47 | int sz = input.size(); |
---|
48 | CHECK(sz > 0); |
---|
49 | for (int i = 0; i < sz; ++i) { |
---|
50 | input(i)->test(); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | TEST(test_mepdf_sample) { |
---|
55 | // Setup model |
---|
56 | vec mu("1.1 -1"); |
---|
57 | ldmat R(mat("1 -0.5; -0.5 2")); |
---|
58 | |
---|
59 | RV x("{x }"); |
---|
60 | RV y("{y }"); |
---|
61 | |
---|
62 | enorm<ldmat> E; |
---|
63 | E.set_rv(concat(x, y)); |
---|
64 | E.set_parameters(mu, R); |
---|
65 | |
---|
66 | epdf *Mg = E.marginal(y); |
---|
67 | CHECK_CLOSE(vec("-1"), Mg->mean(), epsilon); |
---|
68 | |
---|
69 | // putting them back together |
---|
70 | mpdf *Cn = E.condition(x); |
---|
71 | mepdf mMg(Mg); |
---|
72 | Array<mpdf *> A(2); |
---|
73 | A(0) = Cn; |
---|
74 | A(1) = &mMg; |
---|
75 | mprod mEp(A); |
---|
76 | |
---|
77 | int n = 1000; |
---|
78 | mat smp = mEp.samplecond(vec(0), n); |
---|
79 | vec emu = sum(smp, 2) / n; |
---|
80 | CHECK_CLOSE(mu, emu, 0.3); |
---|
81 | |
---|
82 | mat er = (smp * smp.T()) / n - outer_product(emu, emu); |
---|
83 | CHECK_CLOSE(R.to_mat(), er, 0.3); |
---|
84 | |
---|
85 | // test of pdflog at zero |
---|
86 | vec zero(0); |
---|
87 | vec zero2("0 0"); |
---|
88 | CHECK_CLOSE(E.evallog(zero2), mEp.evallogcond(zero2, zero), epsilon); |
---|
89 | } |
---|