Changeset 481

Show
Ignore:
Timestamp:
08/06/09 13:38:17 (15 years ago)
Author:
vbarta
Message:

doubled tolerance computed from variance, repeating failing tests (at most) 3 times; epdf tests pass

Location:
library/tests
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • library/tests/epdf_harness.cpp

    r480 r481  
    33#include "base/user_info.h" 
    44#include "stat/exp_family.h" 
    5 #include "stat/emix.h" 
    65#include "mat_checks.h" 
    76#include "test_util.h" 
     
    5049 
    5150        if ( support.rows() == 2 ) { 
    52                 vec xb = support.get_row ( 0 ); 
    53                 vec yb = support.get_row ( 1 ); 
    54  
    5551                int old_size = nbins.size(); 
    5652                if ( old_size < 2 ) { 
     
    6359                } 
    6460 
    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 ); 
     61                check_support_mean(); 
     62                check_support_integral(); 
    6763        } 
    6864 
    6965        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 ); 
     66                check_sample_mean(); 
     67                check_covariance(); 
    7968        } 
    8069 
     
    9079                mprod mEp ( aa ); 
    9180 
    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  
     81                check_cond_mean(mEp); 
    9982                if ( R.rows() > 0 ) { 
    100                         mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); 
    101                         CHECK_CLOSE_EX ( R, er, tolerance ); 
     83                        check_cond_covariance( mEp ); 
    10284                } 
    10385 
     
    11395} 
    11496 
    115 } 
     97void epdf_harness::check_support_mean() { 
     98        vec xb = support.get_row ( 0 ); 
     99        vec yb = support.get_row ( 1 ); 
     100 
     101        int tc = 0; 
     102        Array<vec> actual(CurrentContext::max_trial_count); 
     103        do { 
     104                vec emu = num_mean2 ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ); 
     105                actual( tc ) = emu; 
     106                ++tc; 
     107        } while ( ( tc < CurrentContext::max_trial_count ) && 
     108                  !UnitTest::AreClose ( mean, actual( tc - 1 ), tolerance ) ); 
     109        if ( ( tc == CurrentContext::max_trial_count ) && 
     110             ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { 
     111                UnitTest::MemoryOutStream stream; 
     112                stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << tolerance << " but was " << actual; 
     113 
     114                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); 
     115 
     116                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); 
     117       } 
     118} 
     119 
     120void epdf_harness::check_support_integral() { 
     121        vec xb = support.get_row ( 0 ); 
     122        vec yb = support.get_row ( 1 ); 
     123 
     124        int tc = 0; 
     125        Array<double> actual(CurrentContext::max_trial_count); 
     126        do { 
     127                double nc = normcoef ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ); 
     128                actual( tc ) = nc; 
     129                ++tc; 
     130        } while ( ( tc < CurrentContext::max_trial_count ) && 
     131                  !UnitTest::AreClose ( 1.0, actual( tc - 1 ), tolerance ) ); 
     132        if ( ( tc == CurrentContext::max_trial_count ) && 
     133             ( !UnitTest::AreClose ( 1.0, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { 
     134                UnitTest::MemoryOutStream stream; 
     135                stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << tolerance << " but was " << actual; 
     136 
     137                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); 
     138 
     139                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); 
     140       } 
     141} 
     142 
     143void epdf_harness::check_sample_mean() { 
     144        // simplify overloading for Visual Studio 
     145        vec delta = 2 * ( sqrt ( variance ) / sqrt ( static_cast<double> ( nsamples ) ) ); 
     146 
     147        int tc = 0; 
     148        Array<vec> actual(CurrentContext::max_trial_count); 
     149        do { 
     150                mat smp = hepdf->sample_m ( nsamples ); 
     151                vec emu = smp * ones ( nsamples ) / nsamples; 
     152                actual( tc ) = emu; 
     153                ++tc; 
     154        } while ( ( tc < CurrentContext::max_trial_count ) && 
     155                  !UnitTest::AreClose ( mean, actual( tc - 1 ), delta ) ); 
     156        if ( ( tc == CurrentContext::max_trial_count ) && 
     157             ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), delta ) ) ) { 
     158                UnitTest::MemoryOutStream stream; 
     159                stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << delta << " but was " << actual; 
     160 
     161                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); 
     162 
     163                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); 
     164       } 
     165} 
     166 
     167 
     168void epdf_harness::check_covariance() { 
     169        int tc = 0; 
     170        Array<mat> actual(CurrentContext::max_trial_count); 
     171        do { 
     172                mat smp = hepdf->sample_m ( nsamples ); 
     173                vec emu = smp * ones ( nsamples ) / nsamples; 
     174                mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); 
     175                actual( tc ) = er; 
     176                ++tc; 
     177        } while ( ( tc < CurrentContext::max_trial_count ) && 
     178                  !UnitTest::AreClose ( R, actual( tc - 1 ), tolerance ) ); 
     179        if ( ( tc == CurrentContext::max_trial_count ) && 
     180             ( !UnitTest::AreClose ( R, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { 
     181                UnitTest::MemoryOutStream stream; 
     182                stream << CurrentContext::format_context(__LINE__) << "expected " << R << " +/- " << tolerance << " but was " << actual; 
     183 
     184                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); 
     185 
     186                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); 
     187       } 
     188} 
     189 
     190void epdf_harness::check_cond_mean( mprod &mep ) { 
     191        // simplify overloading for Visual Studio 
     192        vec delta = 2 * ( sqrt ( variance ) / sqrt ( static_cast<double> ( nsamples ) ) ); 
     193 
     194        int tc = 0; 
     195        Array<vec> actual(CurrentContext::max_trial_count); 
     196        do { 
     197                mat smp = mep.samplecond ( vec ( 0 ), nsamples ); 
     198                vec emu = sum ( smp, 2 ) / nsamples; 
     199                actual( tc ) = emu; 
     200                ++tc; 
     201        } while ( ( tc < CurrentContext::max_trial_count ) && 
     202                  !UnitTest::AreClose ( mean, actual( tc - 1 ), delta ) ); 
     203        if ( ( tc == CurrentContext::max_trial_count ) && 
     204             ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), delta ) ) ) { 
     205                UnitTest::MemoryOutStream stream; 
     206                stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << delta << " but was " << actual; 
     207 
     208                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); 
     209 
     210                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); 
     211       } 
     212} 
     213 
     214void epdf_harness::check_cond_covariance( mprod &mep ) { 
     215        int tc = 0; 
     216        Array<mat> actual(CurrentContext::max_trial_count); 
     217        do { 
     218                mat smp = mep.samplecond ( vec ( 0 ), nsamples ); 
     219                vec emu = sum ( smp, 2 ) / nsamples; 
     220                mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); 
     221                actual( tc ) = er; 
     222                ++tc; 
     223        } while ( ( tc < CurrentContext::max_trial_count ) && 
     224                  !UnitTest::AreClose ( R, actual( tc - 1 ), tolerance ) ); 
     225        if ( ( tc == CurrentContext::max_trial_count ) && 
     226             ( !UnitTest::AreClose ( R, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { 
     227                UnitTest::MemoryOutStream stream; 
     228                stream << CurrentContext::format_context(__LINE__) << "expected " << mean << " +/- " << tolerance << " but was " << actual; 
     229 
     230                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), 0, false); 
     231 
     232                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); 
     233       } 
     234} 
     235 
     236} 
  • library/tests/epdf_harness.h

    r477 r481  
    2121#include "base/bdmbase.h" 
    2222#include "base/user_info.h" 
     23#include "stat/emix.h" 
    2324 
    2425namespace bdm { 
     
    5354                return tolerance; 
    5455        } 
     56 
     57private: 
     58        // assumes support is valid 
     59        void check_support_mean(); 
     60 
     61        // assumes support is valid 
     62        void check_support_integral(); 
     63 
     64        // assumes R is valid - it probably shouldn't, but calling it 
     65        // unconditionally leads to a lot of not implemented 
     66        // functions... 
     67        void check_sample_mean(); 
     68 
     69        // assumes R is valid 
     70        void check_covariance(); 
     71 
     72        // assumes marginal_rv is valid 
     73        void check_cond_mean( mprod &mep ); 
     74 
     75        // assumes marginal_rv and R are valid 
     76        void check_cond_covariance( mprod &mep ); 
    5577}; 
    5678 
  • library/tests/mat_checks.cpp

    r477 r481  
    11#include "mat_checks.h" 
     2#include <sstream> 
    23 
    34namespace UnitTest { 
     
    6667        index = -1; 
    6768} 
     69 
     70std::string CurrentContext::format_context(int ln) { 
     71        std::stringstream ss; 
     72        ss << "error at " << config_name << '[' << index << ']'; 
     73 
     74        if ( ln >= 0 ) { 
     75                ss << ", harness line " << ln; 
     76        } 
     77 
     78        ss << ": "; 
     79        return ss.str(); 
     80} 
  • library/tests/mat_checks.h

    r477 r481  
    1616#include "../bdm/itpp_ext.h" 
    1717#include "UnitTest++.h" 
     18#include <string> 
    1819 
    1920namespace UnitTest { 
     
    5253} 
    5354 
    54 /*! CHECK_EQUAL_EX and CHECK_CLOSE_EX macros should be used only in 
    55   blocks having an instance of this class (which sets the globals for 
    56   error reporting). */ 
     55/*! CHECK_*_EX macros should be used only in blocks having an instance 
     56  of this class (which sets the globals for error reporting). */ 
    5757class CurrentContext { 
     58public: 
     59        // how many times to repeat a failing test before reporting 
     60        // failure 
     61        static const int max_trial_count = 3; 
     62 
    5863private: 
    5964        static const char *config_name; 
     
    6570        ~CurrentContext(); 
    6671 
     72        /* Should be called only in blocks having an instance of 
     73           CurrentContext. The argument, when not default, should be 
     74           __LINE__ (and it is included in the returned string). 
     75        */ 
     76        static std::string format_context( int ln = -1 ); 
     77 
    6778        template<typename Expected, typename Actual> 
    6879        static void CheckEqualEx ( UnitTest::TestResults& results, 
     
    7283                if ( ! ( expected == actual ) ) { 
    7384                        UnitTest::MemoryOutStream stream; 
    74                         stream << "error at " << config_name << '[' << index << "]: expected " << expected << " but was " << actual; 
     85                        stream << format_context() << "expected " << expected << " but was " << actual; 
    7586 
    7687                        results.OnTestFailure ( details, stream.GetText() ); 
     
    8697                if ( !UnitTest::AreClose ( expected, actual, tolerance ) ) { 
    8798                        UnitTest::MemoryOutStream stream; 
    88                         stream << "error at " << config_name << '[' << index << "]: expected " << expected << " +/- " << tolerance << " but was " << actual; 
     99                        stream << format_context() << "expected " << expected << " +/- " << tolerance << " but was " << actual; 
    89100 
    90101                        results.OnTestFailure ( details, stream.GetText() );