Changeset 428

Show
Ignore:
Timestamp:
07/24/09 13:57:16 (15 years ago)
Author:
vbarta
Message:

moved enorm tests to testsuite

Location:
library/tests
Files:
1 removed
5 modified

Legend:

Unmodified
Added
Removed
  • library/tests/CMakeLists.txt

    r426 r428  
    1717 
    1818EXEC(fsqmat_test) 
    19 EXEC(enorm_test) 
    2019EXEC(egiw_test) 
    2120EXEC(emix_test) 
     
    3433EXEC(test_particle) 
    3534EXEC(testSmp) 
    36 EXEC(testEpdf) 
    3735EXEC(test_kalman_QR) 
    3836EXEC(test_kalman_QRexh) 
     
    4139 
    4240# using UnitTest++ 
    43 add_executable(testsuite datalink_test.cpp loggers_test.cpp rv_test.cpp square_mat_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp) 
     41add_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) 
    4442target_link_libraries(testsuite bdm itpp testutil unittest) 
    4543 
  • library/tests/enorm_test.cpp

    r394 r428  
     1#define BDMLIB // not an ideal way to prevent double registration of UI factories... 
    12#include "stat/exp_family.h" 
    23#include "stat/emix.h" 
     4#include "mat_checks.h" 
     5#include "UnitTest++.h" 
     6 
    37using namespace bdm; 
    48 
    5 //These lines are needed for use of cout and endl 
    6 using std::cout; 
    7 using std::endl; 
     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} 
    837 
    938double normcoef ( const epdf* ep,const vec &xb, const vec &yb, int Ngr=100 ) { 
     
    2554} 
    2655 
    27 int main() { 
    28         RNG_randomize(); 
     56TEST(test_enorm) { 
     57    RNG_randomize(); 
    2958 
    30         cout << "Testing enorm(2,2)"<<endl; 
    31         // Setup model 
    32         vec mu ( "1.1 -1" ); 
    33         ldmat R ( mat ( "1 -0.5; -0.5 2" ) ); 
     59    // Setup model 
     60    vec mu("1.1 -1"); 
     61    ldmat R(mat("1 -0.5; -0.5 2")); 
    3462 
    35         RV x ( "{x }" ); 
    36         RV y ( "{y }" ); 
     63    RV x("{x }"); 
     64    RV y("{y }"); 
    3765 
    38         enorm<ldmat> E; 
    39         E.set_rv(concat(x,y)); 
    40         E.set_parameters ( mu,R ); 
    41         cout << "enorm mean value:" << E.mean() <<endl; 
    42         cout << "enorm mean variance:" << R.to_mat() <<endl; 
    43         cout << "enorm normalizing constant:" << E.lognc() <<endl; 
    44         cout << " Numerically integrates to [1.0]: " << normcoef ( &E, vec ( "-5 5" ), vec ( "-5 5" ) ) << endl; 
     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); 
    4572 
    46         int N=1000; 
    47         vec ll ( N ); 
    48         mat Smp=E.sample ( 1000 ); 
    49         vec Emu = sum ( Smp,2 ) /N; 
    50         mat Er = ( Smp*Smp.transpose() ) /N - outer_product ( Emu,Emu ); 
     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); 
    5178 
    52         cout << "original empirical mean: " <<Emu <<endl; 
    53         cout << "original empirical variance: " <<Er <<endl; 
     79    mat Er = (Smp * Smp.T()) / N - outer_product(Emu, Emu); 
     80    CHECK_CLOSE(R.to_mat(), Er, 0.3); 
    5481 
    55         epdf* Mg = E.marginal ( y ); 
    56         mpdf* Cn = E.condition ( x ); 
     82    epdf *Mg = E.marginal(y); 
     83    CHECK_CLOSE(vec("-1"), Mg->mean(), epsilon); 
    5784 
     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); 
    5895 
    59         cout<< "marginal mean: " << Mg->mean() <<endl; 
     96    Er = (Smp * Smp.T()) / N - outer_product(Emu, Emu); 
     97    CHECK_CLOSE(R.to_mat(), Er, 0.3); 
    6098 
    61         cout << "========== putting them back together ======= "<<endl; 
    62         mepdf mMg ( Mg ); 
    63         Array<mpdf*> A ( 2 ); 
    64         A ( 0 ) = Cn; 
    65         A ( 1 ) = &mMg; 
    66         mprod mEp ( A ); 
     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} 
    67104 
    68         Smp=mEp.samplecond ( vec ( 0 ),1000 ); 
    69         Emu = sum ( Smp,2 ) /N; 
    70         Er = ( Smp*Smp.transpose() ) /N - outer_product ( Emu,Emu ); 
     105// from testEpdf 
     106TEST(test_enorm_sum) { 
     107    vec x = "-10:0.1:10"; 
     108    vec y = "-10:0.1:10"; 
    71109 
    72         cout << "composite mean: " <<Emu <<endl; 
    73         cout << "composite variance: " <<Er <<endl; 
    74          
    75         cout << endl << " test of pdflog at zero "<<endl; 
    76         cout << "original: " << E.evallog(vec("0 0")) <<endl; 
    77         cout << "composite: " << mEp.evallogcond(vec("0 0"),vec(0)) << endl; 
     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); 
    78130} 
  • library/tests/mat_checks.cpp

    r426 r428  
    33namespace UnitTest 
    44{ 
     5 
     6bool AreClose(const itpp::vec &expected, const itpp::vec &actual, 
     7              double tolerance) { 
     8    if (expected.length() != actual.length()) { 
     9        return false; 
     10    } 
     11 
     12    for (int i = 0; i < expected.length(); ++i) { 
     13        if (!AreClose(expected(i), actual(i), tolerance)) { 
     14            return false; 
     15        } 
     16    } 
     17 
     18    return true; 
     19} 
    520 
    621bool AreClose(const itpp::mat &expected, const itpp::mat &actual, double tolerance) { 
  • library/tests/mat_checks.h

    r426 r428  
    1919namespace UnitTest 
    2020{ 
     21  bool AreClose(const itpp::vec &expected, const itpp::vec &actual, 
     22                double tolerance); 
     23 
    2124  bool AreClose(const itpp::mat &expected, const itpp::mat &actual, 
    2225                double tolerance); 
  • library/tests/square_mat_test.cpp

    r427 r428  
    1212{ 
    1313 
    14 void CheckClose(TestResults &results, const itpp::mat &expected, 
    15                 const itpp::mat &actual, double tolerance, 
    16                 TestDetails const& details) { 
     14inline void CheckClose(TestResults &results, const itpp::mat &expected, 
     15                       const itpp::mat &actual, double tolerance, 
     16                       TestDetails const& details) { 
    1717    if (!AreClose(expected, actual, tolerance)) {  
    1818        MemoryOutStream stream;