root/library/tests/epdf_harness.cpp @ 480

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

fixed tests for new UI::get & UI::build

Line 
1#include "epdf_harness.h"
2#include "base/bdmbase.h"
3#include "base/user_info.h"
4#include "stat/exp_family.h"
5#include "stat/emix.h"
6#include "mat_checks.h"
7#include "test_util.h"
8#include "UnitTest++.h"
9#include <memory>
10
11namespace bdm {
12
13template<>
14const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory (
15    ParticularUI<epdf_harness> ( "epdf_harness" ) );
16
17void epdf_harness::test_config ( const char *config_file_name ) {
18        RV::clear_all();
19
20        UIFile in ( config_file_name );
21        Array<epdf_harness *> input;
22        UI::get ( input, in, "data", UI::compulsory );
23        int sz = input.size();
24        CHECK ( sz > 0 );
25        for ( int i = 0; i < sz; ++i ) {
26                input ( i )->test ( config_file_name, i );
27        }
28}
29
30void epdf_harness::from_setting ( const Setting &set ) {
31        hepdf = UI::build<epdf> ( set, "epdf", UI::compulsory );
32        UI::get ( mean, set, "mean", UI::compulsory );
33        UI::get ( variance, set, "variance", UI::compulsory );
34
35        UI::get (support, set, "support", UI::optional );
36        UI::get (nbins, set, "nbins", UI::optional );
37        UI::get (nsamples, set, "nsamples", UI::optional );
38        UI::get (R, set, "R", UI::optional );
39
40        mrv = UI::build<RV> (set, "marginal_rv", UI::optional );
41
42        UI::get ( tolerance, set, "tolerance", UI::optional );
43}
44
45void epdf_harness::test ( const char *config_name, int idx ) {
46        CurrentContext cc ( config_name, idx );
47
48        CHECK_CLOSE_EX ( mean, hepdf->mean(), tolerance );
49        CHECK_CLOSE_EX ( variance, hepdf->variance(), tolerance );
50
51        if ( support.rows() == 2 ) {
52                vec xb = support.get_row ( 0 );
53                vec yb = support.get_row ( 1 );
54
55                int old_size = nbins.size();
56                if ( old_size < 2 ) {
57                        vec new_nbins ( "100 100" );
58                        for ( int i = 0; i < old_size; ++i ) {
59                                new_nbins ( i ) = nbins ( i );
60                        }
61
62                        nbins = new_nbins;
63                }
64
65                CHECK_CLOSE_EX ( mean, num_mean2 ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ), tolerance );
66                CHECK_CLOSE_EX ( 1.0, normcoef ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ), tolerance );
67        }
68
69        if ( R.rows() > 0 ) {
70                mat smp = hepdf->sample_m ( nsamples );
71                vec emu = smp * ones ( nsamples ) / nsamples;
72                mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu );
73
74                // simplify overloading for Visual Studio
75                vec delta = sqrt ( variance ) / sqrt ( static_cast<double> ( nsamples ) );
76                CHECK_CLOSE_EX ( mean, emu, delta );
77
78                CHECK_CLOSE_EX ( R, er, tolerance );
79        }
80
81        if ( mrv.get() ) {
82                RV crv = hepdf->_rv().subt ( *mrv );
83                shared_ptr<epdf> m = hepdf->marginal ( *mrv );
84                shared_ptr<mpdf> c = hepdf->condition ( crv );
85                mepdf mm ( m );
86
87                Array<mpdf *> aa ( 2 );
88                aa ( 0 ) = c.get();
89                aa ( 1 ) = &mm;
90                mprod mEp ( aa );
91
92                mat smp = mEp.samplecond ( vec ( 0 ), nsamples );
93                vec emu = sum ( smp, 2 ) / nsamples;
94
95                // simplify overloading for Visual Studio
96                vec delta = sqrt ( variance ) / sqrt ( static_cast<double> ( nsamples ) );
97                CHECK_CLOSE_EX ( mean, emu, delta );
98
99                if ( R.rows() > 0 ) {
100                        mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu );
101                        CHECK_CLOSE_EX ( R, er, tolerance );
102                }
103
104                // test of pdflog at zero
105                vec zero ( 0 );
106                vec zeron ( hepdf->dimension() );
107                for ( int i = 0; i < zeron.size(); ++i ) {
108                        zeron ( i ) = 0;
109                }
110
111                CHECK_CLOSE_EX ( hepdf->evallog ( zeron ), mEp.evallogcond ( zeron, zero ), tolerance );
112        }
113}
114
115}
Note: See TracBrowser for help on using the browser.