Changeset 436

Show
Ignore:
Timestamp:
07/28/09 15:07:47 (15 years ago)
Author:
vbarta
Message:

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

Location:
library
Files:
3 added
7 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.cpp

    r430 r436  
    1212RV RV0=RV(); 
    1313 
     14void RV::clear_all() 
     15{ 
     16    RV_MAP.clear(); 
     17    RV_SIZES.clear(); 
     18    RV_NAMES = Array<string>(RV_BUFFER_STEP); 
     19} 
     20   
    1421int RV::init ( const string &name, int size ) { 
    1522        //Refer 
  • library/bdm/base/bdmbase.h

    r432 r436  
    197197 
    198198  // TODO dodelat void to_setting( Setting &set ) const; 
     199 
     200  //! Invalidate all named RVs. Use before initializing any RV instances, with care... 
     201  static void clear_all(); 
    199202}; 
    200203UIREGISTER(RV); 
  • library/tests/CMakeLists.txt

    r428 r436  
    77link_directories (./unittest-cpp) 
    88 
    9 add_library(testutil mat_checks.cpp mat_checks.h test_util.cpp test_util.h) 
     9add_library(testutil epdf_harness.cpp epdf_harness.h mat_checks.cpp mat_checks.h test_util.cpp test_util.h) 
    1010target_link_libraries(testutil bdm itpp unittest) 
    1111 
     
    1717 
    1818EXEC(fsqmat_test) 
    19 EXEC(egiw_test) 
    2019EXEC(emix_test) 
    2120EXEC(test0) 
     
    3938 
    4039# using UnitTest++ 
    41 add_executable(testsuite datalink_test.cpp enorm_test.cpp loggers_test.cpp rv_test.cpp square_mat_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp) 
     40add_executable(testsuite datalink_test.cpp egiw_test.cpp enorm_test.cpp loggers_test.cpp rv_test.cpp square_mat_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp) 
    4241target_link_libraries(testsuite bdm itpp testutil unittest) 
    4342 
  • library/tests/egiw_test.cpp

    r386 r436  
     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" 
    14#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 
     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 
     26} 
     27 
    228using namespace bdm; 
    329 
    4 //These lines are needed for use of cout and endl 
    5 using std::cout; 
    6 using std::endl; 
     30template<> 
     31const ParticularUI<egiw> &ParticularUI<egiw>::factory( 
     32    ParticularUI<egiw>("egiw")); 
    733 
    8 void Test ( const egiw &E ) { 
     34TEST(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    } 
    944} 
    1045 
    11 int main() { 
    12         cout << "Testing eGiw(1,1)"<<endl; 
    13         { 
    14                 // Setup model 
    15                 double mu=1.1; 
    16                 double s=0.1; 
    17  
    18                 // TEST 1x1 EGIW 
    19                 mat V ( 2,2 ); 
    20                 V ( 0,0 ) = pow ( mu,2 ) +s; 
    21                 V ( 1,0 ) = mu; 
    22                 V ( 0,1 ) = V ( 1,0 ); 
    23                 V ( 1,1 ) = 1.0; 
    24  
    25                 double nu=10; 
    26  
    27                 egiw E ( 1,nu*V,nu ); 
    28                 cout << "egiw mean value:" << E.mean() <<endl; 
    29                 cout << "egiw normalizing constant:" << E.lognc() <<endl; 
    30  
    31                 int n=100; 
    32                 vec t_val ( 2 ); 
    33  
    34                 mat pdf ( 2*n,n ); 
    35                 vec Mu ( 2*n ); 
    36                 vec Si ( n ); 
    37  
    38                 for ( int i=0;i<2*n;i++ ) { 
    39                         Mu ( i ) = -2+i* ( 1.0/ ( n ) ) *3.0; 
    40                         t_val ( 0 ) = Mu ( i ); 
    41                         for ( int j=0;j<n;j++ ) { 
    42                                 Si ( j ) = ( j+1 ) * ( 1.0/n ) *2; 
    43                                 t_val ( 1 ) = Si ( j ); 
    44  
    45                                 pdf ( i,j ) =E.evallog ( t_val ); 
    46                         } 
    47                 } 
    48  
    49                 mat Pdf=exp ( pdf ); 
    50                 vec fm=sum ( Pdf,2 ); 
    51                 vec fs=sum ( Pdf,1 ); 
    52                 cout << "Numerical mean: " << vec_2 ( Mu*fm/sum ( fm ), Si*fs/sum ( fs ) ) <<endl; 
    53                 cout << "Numerical integral of pdf: "<<sumsum ( Pdf/n/n*3*2 ) <<endl; 
    54         } 
    55         cout << "Testing Egiw(1,2)"<<endl; 
    56         { 
    57                 // Setup model 
    58                 double mu=1.1; //unit step parametr 
    59                 double b=3.0; // sequence of <1 -1 1 -1...> 
    60                 double s=0.1; 
     46TEST(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; 
    6151 
    6252 
    63                 // TEST 1x1 EGIW 
    64                 mat V ( 3,3 ); 
    65                 V ( 0,0 ) = pow ( mu,2 ) +pow ( b ,2 )  +s; 
    66                 V ( 1,0 ) = mu; 
    67                 V ( 2,0 ) = b; 
     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; 
    6858 
    69                 V ( 0,1 ) = V ( 1,0 ); 
    70                 V ( 1,1 ) = 1.0; 
    71                 V ( 2,1 ) = 0.0; 
     59    V(0, 1) = V(1, 0); 
     60    V(1, 1) = 1.0; 
     61    V(2, 1) = 0.0; 
    7262 
    73                 V ( 0,2 ) = V ( 2,0 ); 
    74                 V ( 1,2 ) = V ( 2,1 ); 
    75                 V ( 2,2 ) = 1.0; 
     63    V(0, 2) = V(2, 0); 
     64    V(1, 2) = V(2, 1); 
     65    V(2, 2) = 1.0; 
    7666 
     67    double nu = 20; 
    7768 
    78                 double nu=20; 
     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); 
    7972 
    80                 egiw E ( 1,nu*V,nu ); 
    81                 cout << "egiw mean value:" << E.mean() <<endl; 
    82                 cout << "egiw normalizing constant:" << E.lognc() <<endl; 
     73    int n = 100; 
     74    vec rgr(3); 
    8375 
    84                 int n=100; 
    85                 vec t_val ( 3 ); 
     76    mat Tmp(2 * n, n); 
    8677 
    87                 mat Tmp= zeros ( 2*n,n ); 
     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; 
    8885 
    89                 double summ=0.0; 
    90                 for ( int k=0;k<n;k++ ) { // ALL b 
    91                         t_val ( 1 ) = 1 + k* ( 1.0/n ) * 4.0; 
    92                         for ( int i=0;i<2*n;i++ ) { //ALL mu 
    93                                 t_val ( 0 ) = -2+i* ( 1.0/  n ) *3.0; 
    94                                 for ( int j=0;j<n;j++ ) { // All sigma 
    95                                         t_val ( 2 ) = ( j+1 ) * ( 1.0/n ) *2.0; 
     86                Tmp(i, j) = E.evallog(rgr); 
     87            } 
     88        } 
     89        summ += sumsum(exp(Tmp)) / n / n / n * 3.0 * 2.0 * 4.0; 
     90    } 
    9691 
    97                                         Tmp ( i,j ) = E.evallog ( t_val ); 
    98                                 } 
    99                         } 
    100                         summ += sumsum ( exp ( Tmp ) ) /n/n/n*3.0*2.0*4.0; 
    101                 } 
     92    CHECK_CLOSE(1.0, summ, 0.1); 
     93} 
    10294 
    103  
    104                 cout << "Numerical integral of pdf: "<<summ <<endl; 
    105         } 
    106          
    107          
    108 } 
  • library/tests/enorm_test.cpp

    r428 r436  
    33#include "stat/emix.h" 
    44#include "mat_checks.h" 
     5#include "test_util.h" 
    56#include "UnitTest++.h" 
    67 
     
    3435} 
    3536 
    36 } 
    37  
    38 double 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; 
    5437} 
    5538 
  • library/tests/test_util.cpp

    r425 r436  
     1#define BDMLIB // not an ideal way to prevent double registration of UI factories... 
    12#include "test_util.h" 
     3#include "stat/exp_family.h" 
    24#include <fstream> 
    35#include <stdexcept> 
     
    1214 
    1315namespace bdm { 
     16 
     17using itpp::vec; 
     18using itpp::mat; 
    1419 
    1520std::string load_test_file(const char *fname) 
     
    7378} 
    7479 
     80double normcoef(const epdf *ep, const vec &xb, const vec &yb, 
     81                int xn, int yn) { 
     82    mat Pdf(xn + 1, yn + 1); 
     83    vec rgr(2); 
     84 
     85    double xstep = (xb(1) - xb(0)) / xn; 
     86    double ystep = (yb(1) - yb(0)) / yn; 
     87 
     88    int i = 0; 
     89    for (double x = xb(0); x <= xb(1); x += xstep, i++) { 
     90        rgr(0) = x; 
     91        int j = 0; 
     92        for (double y = yb(0); y <= yb(1); y += ystep, j++) { 
     93            rgr(1) = y; 
     94            Pdf(i, j) = exp(ep->evallog(rgr)); 
     95        } 
     96    } 
     97 
     98    return sumsum(Pdf) * xstep * ystep; 
    7599} 
     100 
     101vec num_mean2(const epdf *ep, const vec &xb, const vec &yb, 
     102              int xn, int yn) { 
     103    mat Pdf(xn + 1, yn + 1); 
     104    vec rgr(2); 
     105 
     106    double xstep = (xb(1) - xb(0)) / xn; 
     107    double ystep = (yb(1) - yb(0)) / yn; 
     108 
     109    vec Mu(xn + 1); 
     110    vec Si(yn + 1); 
     111 
     112    int i = 0; 
     113    for (double x = xb(0); x <= xb(1); x += xstep, i++) { 
     114        Mu(i) = x; 
     115        rgr(0) = x; 
     116        int j = 0; 
     117        for (double y = yb(0); y <= yb(1); y += ystep, j++) { 
     118            Si(j) = y; 
     119            rgr(1) = y; 
     120            Pdf(i, j) = exp(ep->evallog(rgr)); 
     121        } 
     122    } 
     123 
     124    vec fm = sum(Pdf, 2); 
     125    vec fs = sum(Pdf, 1); 
     126    return vec_2(Mu * fm / sum(fm), Si * fs / sum(fs)); 
     127} 
     128 
     129} 
  • library/tests/test_util.h

    r425 r436  
    1515 
    1616#include <string> 
     17#include "itpp_ext.h" 
    1718 
    1819namespace bdm { 
     20 
     21class epdf; 
    1922 
    2023/*! \brief Non-general but simple file load. 
     
    3437bool remove_all(const char *path); 
    3538 
     39double normcoef(const epdf *ep, const itpp::vec &xb, const itpp::vec &yb, 
     40                int xn = 100, int yn = 100); 
     41 
     42itpp::vec num_mean2(const epdf *ep, const itpp::vec &xb, const itpp::vec &yb, 
     43                    int xn = 100, int yn = 100); 
     44 
    3645} 
    3746