- Timestamp:
- 07/28/09 15:07:47 (15 years ago)
- Location:
- library
- Files:
-
- 3 added
- 7 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.cpp
r430 r436 12 12 RV RV0=RV(); 13 13 14 void RV::clear_all() 15 { 16 RV_MAP.clear(); 17 RV_SIZES.clear(); 18 RV_NAMES = Array<string>(RV_BUFFER_STEP); 19 } 20 14 21 int RV::init ( const string &name, int size ) { 15 22 //Refer -
library/bdm/base/bdmbase.h
r432 r436 197 197 198 198 // 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(); 199 202 }; 200 203 UIREGISTER(RV); -
library/tests/CMakeLists.txt
r428 r436 7 7 link_directories (./unittest-cpp) 8 8 9 add_library(testutil mat_checks.cpp mat_checks.h test_util.cpp test_util.h)9 add_library(testutil epdf_harness.cpp epdf_harness.h mat_checks.cpp mat_checks.h test_util.cpp test_util.h) 10 10 target_link_libraries(testutil bdm itpp unittest) 11 11 … … 17 17 18 18 EXEC(fsqmat_test) 19 EXEC(egiw_test)20 19 EXEC(emix_test) 21 20 EXEC(test0) … … 39 38 40 39 # using UnitTest++ 41 add_executable(testsuite datalink_test.cpp e norm_test.cpp loggers_test.cpp rv_test.cpp square_mat_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp)40 add_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) 42 41 target_link_libraries(testsuite bdm itpp testutil unittest) 43 42 -
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" 1 4 #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 10 const double epsilon = 0.00001; 11 12 namespace UnitTest 13 { 14 15 inline 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 2 28 using namespace bdm; 3 29 4 //These lines are needed for use of cout and endl 5 using std::cout; 6 using std::endl;30 template<> 31 const ParticularUI<egiw> &ParticularUI<egiw>::factory( 32 ParticularUI<egiw>("egiw")); 7 33 8 void Test ( const egiw &E ) { 34 TEST(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 } 9 44 } 10 45 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; 46 TEST(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; 61 51 62 52 63 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; 68 58 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; 72 62 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; 76 66 67 double nu = 20; 77 68 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); 79 72 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); 83 75 84 int n=100; 85 vec t_val ( 3 ); 76 mat Tmp(2 * n, n); 86 77 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; 88 85 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 } 96 91 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 } 102 94 103 104 cout << "Numerical integral of pdf: "<<summ <<endl;105 }106 107 108 } -
library/tests/enorm_test.cpp
r428 r436 3 3 #include "stat/emix.h" 4 4 #include "mat_checks.h" 5 #include "test_util.h" 5 6 #include "UnitTest++.h" 6 7 … … 34 35 } 35 36 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;54 37 } 55 38 -
library/tests/test_util.cpp
r425 r436 1 #define BDMLIB // not an ideal way to prevent double registration of UI factories... 1 2 #include "test_util.h" 3 #include "stat/exp_family.h" 2 4 #include <fstream> 3 5 #include <stdexcept> … … 12 14 13 15 namespace bdm { 16 17 using itpp::vec; 18 using itpp::mat; 14 19 15 20 std::string load_test_file(const char *fname) … … 73 78 } 74 79 80 double 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; 75 99 } 100 101 vec 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 15 15 16 16 #include <string> 17 #include "itpp_ext.h" 17 18 18 19 namespace bdm { 20 21 class epdf; 19 22 20 23 /*! \brief Non-general but simple file load. … … 34 37 bool remove_all(const char *path); 35 38 39 double normcoef(const epdf *ep, const itpp::vec &xb, const itpp::vec &yb, 40 int xn = 100, int yn = 100); 41 42 itpp::vec num_mean2(const epdf *ep, const itpp::vec &xb, const itpp::vec &yb, 43 int xn = 100, int yn = 100); 44 36 45 } 37 46