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 "itpp_ext.h" |
---|
6 | #include "epdf_harness.h" |
---|
7 | #include "egiw_harness.h" |
---|
8 | #include "mat_checks.h" |
---|
9 | #include "UnitTest++.h" |
---|
10 | |
---|
11 | const double epsilon = 0.00001; |
---|
12 | |
---|
13 | namespace UnitTest |
---|
14 | { |
---|
15 | |
---|
16 | inline void CheckClose(TestResults &results, const itpp::vec &expected, |
---|
17 | const itpp::vec &actual, double tolerance, |
---|
18 | TestDetails const &details) { |
---|
19 | if (!AreClose(expected, actual, tolerance)) { |
---|
20 | MemoryOutStream stream; |
---|
21 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
22 | |
---|
23 | results.OnTestFailure(details, stream.GetText()); |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | } |
---|
28 | |
---|
29 | using namespace bdm; |
---|
30 | |
---|
31 | template<> |
---|
32 | const ParticularUI<egiw> &ParticularUI<egiw>::factory( |
---|
33 | ParticularUI<egiw>("egiw")); |
---|
34 | |
---|
35 | template<> |
---|
36 | const ParticularUI<egiw_harness> &ParticularUI<egiw_harness>::factory( |
---|
37 | ParticularUI<egiw_harness>("egiw_harness")); |
---|
38 | |
---|
39 | TEST(test_egiw) { |
---|
40 | RV::clear_all(); |
---|
41 | UIFile in("egiw.cfg"); |
---|
42 | Array<epdf_harness *> input; |
---|
43 | UI::get(input, in, "data"); |
---|
44 | int sz = input.size(); |
---|
45 | CHECK(sz > 0); |
---|
46 | for (int i = 0; i < sz; ++i) { |
---|
47 | input(i)->test(); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | TEST(test_egiw_1_2) { |
---|
52 | // Setup model |
---|
53 | double mu = 1.1; //unit step parametr |
---|
54 | double b = 3.0; // sequence of <1 -1 1 -1...> |
---|
55 | double s = 0.1; |
---|
56 | |
---|
57 | |
---|
58 | // TEST 1x1 EGIW |
---|
59 | mat V(3, 3); |
---|
60 | V(0, 0) = pow(mu, 2) + pow(b, 2) + s; |
---|
61 | V(1, 0) = mu; |
---|
62 | V(2, 0) = b; |
---|
63 | |
---|
64 | V(0, 1) = V(1, 0); |
---|
65 | V(1, 1) = 1.0; |
---|
66 | V(2, 1) = 0.0; |
---|
67 | |
---|
68 | V(0, 2) = V(2, 0); |
---|
69 | V(1, 2) = V(2, 1); |
---|
70 | V(2, 2) = 1.0; |
---|
71 | |
---|
72 | double nu = 20; |
---|
73 | |
---|
74 | egiw E(1, nu * V, nu); |
---|
75 | CHECK_CLOSE(vec("1.1 3.0 0.142857"), E.mean(), epsilon); |
---|
76 | CHECK_CLOSE(7.36731, E.lognc(), epsilon); |
---|
77 | |
---|
78 | int n = 100; |
---|
79 | vec rgr(3); |
---|
80 | |
---|
81 | mat Tmp(2 * n, n); |
---|
82 | |
---|
83 | double summ = 0.0; |
---|
84 | for (int k = 0; k < n; k++) { // ALL b |
---|
85 | rgr(1) = 1 + k * (1.0 / n) * 4.0; |
---|
86 | for (int i = 0; i < 2*n; i++) { //ALL mu |
---|
87 | rgr(0) = -2 + i * (1.0 / n) * 3.0; |
---|
88 | for (int j = 0; j < n; j++) { // All sigma |
---|
89 | rgr(2) = (j + 1) * (1.0 / n) * 2.0; |
---|
90 | |
---|
91 | Tmp(i, j) = E.evallog(rgr); |
---|
92 | } |
---|
93 | } |
---|
94 | summ += sumsum(exp(Tmp)) / n / n / n * 3.0 * 2.0 * 4.0; |
---|
95 | } |
---|
96 | |
---|
97 | CHECK_CLOSE(1.0, summ, 0.1); |
---|
98 | } |
---|