root/library/tests/enorm_test.cpp @ 443

Revision 443, 3.1 kB (checked in by vbarta, 15 years ago)

converted some enorm tests to configurable

  • 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 "epdf_harness.h"
5#include "mat_checks.h"
6#include "test_util.h"
7#include "UnitTest++.h"
8
9using namespace bdm;
10
11const double epsilon = 0.00001;
12
13namespace UnitTest
14{
15
16inline 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
27inline void CheckClose(TestResults &results, const itpp::mat &expected,
28                       const itpp::mat &actual, double tolerance,
29                       TestDetails const& details) {
30    if (!AreClose(expected, actual, tolerance)) { 
31        MemoryOutStream stream;
32        stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
33
34        results.OnTestFailure(details, stream.GetText());
35    }
36}
37
38}
39
40template<>
41const ParticularUI<enorm<ldmat> > &ParticularUI<enorm<ldmat> >::factory(
42    ParticularUI<enorm<ldmat> >("enorm<ldmat>"));
43
44template<>
45const ParticularUI<enorm<fsqmat> > &ParticularUI<enorm<fsqmat> >::factory(
46    ParticularUI<enorm<fsqmat> >("enorm<fsqmat>"));
47
48TEST(test_enorm) {
49    RV::clear_all();
50    UIFile in("enorm.cfg");
51    Array<epdf_harness *> input;
52    UI::get(input, in, "data");
53    int sz = input.size();
54    CHECK(sz > 0);
55    for (int i = 0; i < sz; ++i) {
56        input(i)->test();
57    }
58}
59
60TEST(test_enorm_sample) {
61    RNG_randomize();
62
63    // Setup model
64    vec mu("1.1 -1");
65    ldmat R(mat("1 -0.5; -0.5 2"));
66
67    RV x("{x }");
68    RV y("{y }");
69
70    enorm<ldmat> E;
71    E.set_rv(concat(x, y));
72    E.set_parameters(mu, R);
73
74    int N = 1000;
75    vec ll(N);
76    mat Smp = E.sample(1000);
77    vec Emu = sum(Smp, 2) / N;
78    CHECK_CLOSE(mu, Emu, 0.3);
79
80    mat Er = (Smp * Smp.T()) / N - outer_product(Emu, Emu);
81    CHECK_CLOSE(R.to_mat(), Er, 0.3);
82
83    epdf *Mg = E.marginal(y);
84    CHECK_CLOSE(vec("-1"), Mg->mean(), epsilon);
85
86    // putting them back together
87    mpdf *Cn = E.condition(x);
88    mepdf mMg(Mg);
89    Array<mpdf *> A(2);
90    A(0) = Cn;
91    A(1) = &mMg;
92    mprod mEp(A);
93    Smp = mEp.samplecond(vec(0), 1000);
94    Emu = sum(Smp, 2) / N;
95    CHECK_CLOSE(mu, Emu, 0.3);
96
97    Er = (Smp * Smp.T()) / N - outer_product(Emu, Emu);
98    CHECK_CLOSE(R.to_mat(), Er, 0.3);
99
100    // test of pdflog at zero
101    vec zero(0);
102    vec zero2("0 0");
103    CHECK_CLOSE(E.evallog(zero2), mEp.evallogcond(zero2, zero), epsilon);
104}
105
106#if false
107// from testEpdf
108TEST(test_enorm_sum) {
109    vec x = "-10:0.1:10";
110    vec y = "-10:0.1:10";
111
112    RV rv("{x2 }", "2");
113    vec mu0 = "0.0 0.0";
114    mat V0 = "5 -0.05; -0.05 5.20";
115    fsqmat R(V0);
116
117    enorm<fsqmat> eN;
118    eN.set_rv(rv);
119    eN.set_parameters(mu0, R);
120
121    vec pom(2);
122    double suma = 0.0;
123    for (int i = 0; i < x.length(); i++) {
124        for (int j=0; j<y.length(); j++) {
125            pom(0) = x(i);
126            pom(1) = y(j);
127            suma += exp(eN.evallog(pom));
128        }
129    }
130
131    CHECK_CLOSE(100, suma, 0.1);
132}
133#endif
Note: See TracBrowser for help on using the browser.