root/library/tests/enorm_test.cpp @ 428

Revision 428, 3.2 kB (checked in by vbarta, 15 years ago)

moved enorm tests to testsuite

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