root/library/tests/enorm_test.cpp @ 436

Revision 436, 2.7 kB (checked in by vbarta, 15 years ago)

moved egiw_test to testsuite (partially converted to a configurable test); added public method clearing RVs

  • Property svn:eol-style set to native
Line 
1#define BDMLIB // not an ideal way to prevent double registration of UI factories...
2#include "stat/exp_family.h"
3#include "stat/emix.h"
4#include "mat_checks.h"
5#include "test_util.h"
6#include "UnitTest++.h"
7
8using namespace bdm;
9
10const double epsilon = 0.00001;
11
12namespace UnitTest
13{
14
15inline 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
26inline void CheckClose(TestResults &results, const itpp::mat &expected,
27                       const itpp::mat &actual, double tolerance,
28                       TestDetails const& details) {
29    if (!AreClose(expected, actual, tolerance)) { 
30        MemoryOutStream stream;
31        stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
32
33        results.OnTestFailure(details, stream.GetText());
34    }
35}
36
37}
38
39TEST(test_enorm) {
40    RNG_randomize();
41
42    // Setup model
43    vec mu("1.1 -1");
44    ldmat R(mat("1 -0.5; -0.5 2"));
45
46    RV x("{x }");
47    RV y("{y }");
48
49    enorm<ldmat> E;
50    E.set_rv(concat(x,y));
51    E.set_parameters(mu, R);
52    CHECK_EQUAL(mu, E.mean());
53    CHECK_CLOSE(2.11768, E.lognc(), epsilon);
54    CHECK_CLOSE(1.0, normcoef(&E, vec("-5 5"), vec("-5 5")), 0.01);
55
56    int N = 1000;
57    vec ll(N);
58    mat Smp = E.sample(1000);
59    vec Emu = sum(Smp, 2) / N;
60    CHECK_CLOSE(mu, Emu, 0.3);
61
62    mat Er = (Smp * Smp.T()) / N - outer_product(Emu, Emu);
63    CHECK_CLOSE(R.to_mat(), Er, 0.3);
64
65    epdf *Mg = E.marginal(y);
66    CHECK_CLOSE(vec("-1"), Mg->mean(), epsilon);
67
68    // putting them back together
69    mpdf *Cn = E.condition(x);
70    mepdf mMg(Mg);
71    Array<mpdf *> A(2);
72    A(0) = Cn;
73    A(1) = &mMg;
74    mprod mEp(A);
75    Smp = mEp.samplecond(vec(0), 1000);
76    Emu = sum(Smp, 2) / N;
77    CHECK_CLOSE(mu, Emu, 0.3);
78
79    Er = (Smp * Smp.T()) / N - outer_product(Emu, Emu);
80    CHECK_CLOSE(R.to_mat(), Er, 0.3);
81
82    // test of pdflog at zero
83    vec zero(0);
84    vec zero2("0 0");
85    CHECK_CLOSE(E.evallog(zero2), mEp.evallogcond(zero2, zero), epsilon);
86}
87
88// from testEpdf
89TEST(test_enorm_sum) {
90    vec x = "-10:0.1:10";
91    vec y = "-10:0.1:10";
92
93    RV rv("{x2 }", "2");
94    vec mu0 = "0.0 0.0";
95    mat V0 = "5 -0.05; -0.05 5.20";
96    fsqmat R(V0);
97
98    enorm<fsqmat> eN;
99    eN.set_rv(rv);
100    eN.set_parameters(mu0, R);
101
102    vec pom(2);
103    double suma = 0.0;
104    for (int i = 0; i < x.length(); i++) {
105        for (int j=0; j<y.length(); j++) {
106            pom(0) = x(i);
107            pom(1) = y(j);
108            suma += exp(eN.evallog(pom));
109        }
110    }
111
112    CHECK_CLOSE(100, suma, 0.1);
113}
Note: See TracBrowser for help on using the browser.