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