- Timestamp:
- Mar 17, 2008, 3:54:10 PM (15 years ago)
- Location:
- trunk/test
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/Suite.cc
r1244 r1247 90 90 91 91 92 bool Suite::equal_sqrt(double a, double b, unsigned long int n) 93 { 94 double last_error_bound = n* 95 std::sqrt(std::numeric_limits<double>().epsilon())* 96 std::min(std::abs(a), std::abs(b)); 97 if (!(std::abs(a-b) <= last_error_bound)){ 98 err() << "Error: Comparing " << a << " and " << b 99 << "\n Difference: " << a - b 100 << "\n expected difference to be at most " << last_error_bound 101 << std::endl; 102 return false; 103 } 104 return true; 105 } 106 107 92 108 bool Suite::ok(void) const 93 109 { -
trunk/test/Suite.h
r1244 r1247 59 59 bool equal(double a, double b, unsigned long int N=1); 60 60 61 /** 62 \return true if \f$ |a-b| <= N * sqrt(\epsilon) * min(|a|,|b|) \f$ 63 where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon() 64 */ 65 bool equal_sqrt(double a, double b, unsigned long int N=1); 66 61 67 template<typename Iterator1, typename Iterator2> 62 68 bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2, -
trunk/test/distance_test.cc
r1239 r1247 34 34 #include <fstream> 35 35 #include <iostream> 36 #include <limits> 36 37 #include <list> 37 38 #include <vector> … … 45 46 46 47 template<class Distance> 47 void test_distance(Distance, theplu::yat::test::Suite&); 48 49 template<class Distance> 50 void test_duplicate(Distance, theplu::yat::test::Suite&); 51 52 template<class Distance> 53 void test_rescaling(Distance, theplu::yat::test::Suite&); 54 55 template<class Distance> 56 void test_unity_weights(Distance, theplu::yat::test::Suite&); 57 58 template<class Distance> 59 void test_self_distance(Distance, theplu::yat::test::Suite&); 60 61 template<class Distance> 62 void test_symmetry(Distance, theplu::yat::test::Suite&); 63 64 template<class Distance> 65 void test_zero_weight(Distance, theplu::yat::test::Suite&); 48 void test_distance(Distance, theplu::yat::test::Suite&, unsigned long int N=1); 49 50 template<class Distance> 51 void test_duplicate(Distance, theplu::yat::test::Suite&, unsigned long int N=1); 52 53 template<class Distance> 54 void test_rescaling(Distance, theplu::yat::test::Suite&, unsigned long int N=1); 55 56 template<class Distance> 57 void test_unity_weights(Distance, theplu::yat::test::Suite&, 58 unsigned long int N=1); 59 60 template<class Distance> 61 void test_self_distance(Distance, theplu::yat::test::Suite&, 62 unsigned long int N=1); 63 64 template<class Distance> 65 void test_symmetry(Distance, theplu::yat::test::Suite&, unsigned long int N=1); 66 67 template<class Distance> 68 void test_zero_weight(Distance, theplu::yat::test::Suite&, 69 unsigned long int N=1); 66 70 67 71 utility::Matrix weight(void); … … 80 84 statistics::EuclideanDistance eucl_dist; 81 85 suite.err() << "testing EuclideanDistance" << std::endl; 82 test_distance(eucl_dist, suite );86 test_distance(eucl_dist, suite, 100); 83 87 double dist=eucl_dist(a.begin(),a.end(),b.begin()); 84 88 if(std::abs(dist-2.23607)>tolerance) { … … 89 93 statistics::PearsonDistance pear_dist; 90 94 suite.err() << "testing PearsonDistance" << std::endl; 91 test_distance(pear_dist, suite );95 test_distance(pear_dist, suite, 1000); 92 96 dist=pear_dist(a.begin(),a.end(),b.begin()); 93 97 if(std::abs(dist-1.5)>tolerance) { … … 170 174 171 175 template<class Distance> 172 void test_distance(Distance dist, theplu::yat::test::Suite& suite) 173 { 174 test_duplicate(dist, suite); 175 test_rescaling(dist, suite); 176 test_unity_weights(dist, suite); 177 test_self_distance(dist, suite); 178 test_symmetry(dist, suite); 179 test_zero_weight(dist, suite); 180 } 181 182 template<class Distance> 183 void test_duplicate(Distance dist, theplu::yat::test::Suite& suite) 176 void test_distance(Distance dist, theplu::yat::test::Suite& suite, 177 unsigned int long N) 178 { 179 test_duplicate(dist, suite, N); 180 test_rescaling(dist, suite, N); 181 test_unity_weights(dist, suite, N); 182 test_self_distance(dist, suite, N); 183 test_symmetry(dist, suite, N); 184 test_zero_weight(dist, suite, N); 185 } 186 187 template<class Distance> 188 void test_duplicate(Distance dist, theplu::yat::test::Suite& suite, 189 unsigned long int N) 184 190 { 185 191 utility::Matrix x(data()); … … 200 206 w2(0,i)=0.0; 201 207 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 202 check_equality(dist1, dist2, suite, "duplicate property"); 203 } 204 205 template<class Distance> 206 void test_rescaling(Distance dist, theplu::yat::test::Suite& suite) 208 check_equality(dist1, dist2, suite, "duplicate property", N); 209 } 210 211 template<class Distance> 212 void test_rescaling(Distance dist, theplu::yat::test::Suite& suite, 213 unsigned long int N) 207 214 { 208 215 utility::Matrix x=data(); … … 212 219 w *= 2.13; 213 220 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 214 check_equality(dist1, dist2, suite, "rescaling", 10); 215 } 216 217 template<class Distance> 218 void test_unity_weights(Distance dist, theplu::yat::test::Suite& suite) 221 check_equality(dist1, dist2, suite, "rescaling", N); 222 } 223 224 template<class Distance> 225 void test_unity_weights(Distance dist, theplu::yat::test::Suite& suite, 226 unsigned long int N) 219 227 { 220 228 utility::Matrix x=data(); … … 222 230 double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 223 231 double dist2 = dist(x.begin_row(0), x.end_row(0), x.begin_row(1)); 224 check_equality(dist1, dist2, suite, "unity weights", 10); 225 } 226 227 template<class Distance> 228 void test_self_distance(Distance dist, theplu::yat::test::Suite& suite) 232 check_equality(dist1, dist2, suite, "unity weights", N); 233 } 234 235 template<class Distance> 236 void test_self_distance(Distance dist, theplu::yat::test::Suite& suite, 237 unsigned long int N) 229 238 { 230 239 utility::Matrix x = data(); 231 240 double self = dist(x.begin(), x.end(), x.begin()); 232 if (! suite.equal(self, 0.0)){241 if (!(std::abs(self) <= N*std::numeric_limits<double>().epsilon()) ){ 233 242 suite.err() << "error: self distance is " << self << "\n" 234 243 << "supposed to be zero.\n"; … … 239 248 240 249 template<class Distance> 241 void test_symmetry(Distance dist, theplu::yat::test::Suite& suite) 250 void test_symmetry(Distance dist, theplu::yat::test::Suite& suite, 251 unsigned long int N) 242 252 { 243 253 utility::Matrix x = data(); 244 254 double distab = dist(x.begin_row(0), x.end_row(0), x.begin_row(1)); 245 255 double distba = dist(x.begin_row(1), x.end_row(1), x.begin_row(0)); 246 check_equality(distab, distba, suite, "symmetry test"); 247 } 248 249 250 template<class Distance> 251 void test_zero_weight(Distance dist, theplu::yat::test::Suite& suite) 256 check_equality(distab, distba, suite, "symmetry test", N); 257 } 258 259 260 template<class Distance> 261 void test_zero_weight(Distance dist, theplu::yat::test::Suite& suite, 262 unsigned long int N) 252 263 { 253 264 utility::Matrix x=data(); … … 258 269 w(0,0) = 100*std::numeric_limits<double>().epsilon(); 259 270 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 260 check_equality(dist1, dist2, suite, "zero weight", 1000);271 check_equality(dist1, dist2, suite, "zero weight", N); 261 272 } 262 273 -
trunk/test/regression_test.cc
r1244 r1247 90 90 } 91 91 if (!suite.equal(polynomial.fit_parameters()(0), 92 linear.alpha()-linear.beta()*1985, 1000 )){92 linear.alpha()-linear.beta()*1985, 10000)){ 93 93 suite.err() << "error: fit_parameters(0) = " 94 94 << polynomial.fit_parameters()(0)<< std::endl; … … 108 108 } 109 109 if (!suite.equal(polynomial.standard_error2(1985), 110 linear.standard_error2(1985), 1 e5)){110 linear.standard_error2(1985), 100000)){ 111 111 suite.err() << "error: standard_error not same in linear and polynomial(1)" 112 112 << "\n polynomial: " << polynomial.standard_error2(1985) … … 131 131 // Comparing LinearWeighted and PolynomialWeighted(1) 132 132 suite.err() << " comparing LinearWeighted and PolynomialWeighted(1)" 133 133 << std::endl; 134 134 linear_w.fit(x,y,w); 135 135 regression::PolynomialWeighted polynomial_w(1); 136 136 polynomial_w.fit(x,y,w); 137 if ( !suite.equal(linear_w.beta(), polynomial_w.fit_parameters()(1),100 ) ){137 if ( !suite.equal(linear_w.beta(), polynomial_w.fit_parameters()(1),10000) ){ 138 138 suite.err() << "error: beta and fit_parameters(1) not equal" << std::endl; 139 139 suite.err() << " beta = " << linear_w.beta() << std::endl; … … 143 143 } 144 144 if ( !suite.equal(polynomial_w.fit_parameters()(0), 145 linear_w.alpha()-linear_w.beta()*1990, 100 ) ){145 linear_w.alpha()-linear_w.beta()*1990, 10000) ){ 146 146 suite.err() << "error: fit_parameters(0) = " 147 147 << polynomial.fit_parameters()(0)<< std::endl; … … 155 155 suite.add(false); 156 156 } 157 if ( !suite.equal(polynomial_w.predict(1.0), linear_w.predict(1.0), 100 ) ){157 if ( !suite.equal(polynomial_w.predict(1.0), linear_w.predict(1.0), 10000) ){ 158 158 suite.err() << "error: predict not same in linear and polynomial(1)" 159 159 << std::endl; … … 161 161 } 162 162 if ( !suite.equal(polynomial_w.standard_error2(1985), 163 linear_w.standard_error2(1985), 1 e5) ){163 linear_w.standard_error2(1985), 100000) ){ 164 164 suite.err() << "error: standard_error not same in linear and polynomial(1)" 165 165 << "\n polynomial: " << polynomial_w.standard_error2(1985) … … 304 304 w*=2; 305 305 wr.fit(x,y,w); 306 if (!suite.equal(wr.predict(2000), predict, 1 e4) ){306 if (!suite.equal(wr.predict(2000), predict, 10000) ){ 307 307 suite.add(false); 308 308 suite.err() << "Error: predict not equal after rescaling.\n"; … … 318 318 suite.err() << "difference " << s2-wr.s2(2.0) << std::endl; 319 319 } 320 if (!suite.equal (wr.standard_error2(2000), standard_error2, 1e9) ){320 if (!suite.equal_sqrt(wr.standard_error2(2000), standard_error2, 10) ){ 321 321 suite.add(false); 322 322 suite.err() << "Error: standard_error2 not equal after rescaling.\n"; … … 332 332 suite.err() << "Error: r2 not equal after rescaling.\n"; 333 333 } 334 if (!suite.equal (wr.prediction_error2(2000,2), prediction_error2, 1e8) ){334 if (!suite.equal_sqrt(wr.prediction_error2(2000,2), prediction_error2, 10) ){ 335 335 suite.add(false); 336 336 suite.err() << "Error: prediction_error2 not equal after rescaling.\n"; … … 366 366 367 367 wr.fit(x2,y2,w2); 368 if (!suite.equal(wr.predict(2000), predict ) ) {368 if (!suite.equal(wr.predict(2000), predict, 10000) ) { 369 369 suite.add(false); 370 370 suite.err() << "Error: predict not equal.\n"; … … 447 447 w*=2; 448 448 mdw.fit(data,y,w); 449 if (!suite.equal(mdw.predict(z), predict, 1 e4) ){449 if (!suite.equal(mdw.predict(z), predict, 10000) ){ 450 450 suite.add(false); 451 451 suite.err() << "Error: predict not equal after rescaling.\n"; 452 suite.err() << " predict = " << predict << " and after doubling weights.\n"; 452 suite.err() << " predict = " << predict 453 << " and after doubling weights.\n"; 453 454 suite.err() << " predict = " << mdw.predict(z) << "\n"; 454 455 } 455 if (!suite.equal (mdw.prediction_error2(z,2), prediction_error2, 1e8) ){456 if (!suite.equal_sqrt(mdw.prediction_error2(z,2), prediction_error2,10) ){ 456 457 suite.add(false); 457 458 suite.err() << "Error: prediction_error2 not equal after rescaling.\n"; … … 467 468 suite.err() << " s2 = " << mdw.s2(2) << "\n"; 468 469 } 469 if (!suite.equal (mdw.standard_error2(z), standard_error2, 1e9) ){470 if (!suite.equal_sqrt(mdw.standard_error2(z), standard_error2, 10) ){ 470 471 suite.add(false); 471 472 suite.err() << "Error: standard_error2 not equal after rescaling.\n";
Note: See TracChangeset
for help on using the changeset viewer.