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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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 }