Changeset 1234
- Timestamp:
- Mar 15, 2008, 5:00:54 PM (16 years ago)
- Location:
- trunk/test
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/Suite.cc
r1232 r1234 74 74 75 75 76 bool equal(double a, double b, u_int n)76 bool Suite::equal(double a, double b, u_int n) 77 77 { 78 return std::abs(a-b) <= 79 n*std::numeric_limits<double>().epsilon()*std::min(std::abs(a), 80 std::abs(b)); 78 last_error_bound_ = n*std::numeric_limits<double>().epsilon()* 79 std::min(std::abs(a), std::abs(b)); 80 return std::abs(a-b) <= last_error_bound_; 81 } 82 83 84 double Suite::last_error_bound(void) const 85 { 86 return last_error_bound_; 81 87 } 82 88 -
trunk/test/Suite.h
r1232 r1234 54 54 55 55 /** 56 \return true if \f$ |a-b| <= N * \epsilon * min(|a|,|b|) \f$ 57 where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon() 58 */ 59 bool equal(double a, double b, unsigned int N=1); 60 61 double last_error_bound(void) const; 62 63 /** 56 64 \return true if test is ok 57 65 */ … … 77 85 bool ok_; 78 86 std::ofstream* dev_null_; 87 double last_error_bound_; 79 88 80 89 }; 81 82 /**83 \return true if \f$ |a-b| <= N * \epsilon * min(|a|,|b|) \f$84 where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()85 */86 bool equal(double a, double b, unsigned int N=1);87 90 88 91 }}} -
trunk/test/averager_test.cc
r1232 r1234 244 244 // std::cout << (a.mean()==b.mean()) << std::endl; 245 245 // std::cout << (std::abs(a.variance()-b.variance()<1e-15)) << std::endl; 246 return (a.n()==b.n() && test::equal(a.mean(),b.mean(),tol) &&247 test::equal(a.variance(),b.variance(),tol));246 return (a.n()==b.n() && suite.equal(a.mean(),b.mean(),tol) && 247 suite.equal(a.variance(),b.variance(),tol)); 248 248 } 249 249 … … 252 252 { 253 253 bool equal = true; 254 if ( ! test::equal(a.mean(),b.mean(),tol)){254 if ( !suite.equal(a.mean(),b.mean(),tol)){ 255 255 equal=false; 256 256 suite.err() << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl; 257 257 suite.err() << "difference:\t" << a.mean()-b.mean() << std::endl; 258 258 } 259 if ( ! test::equal(a.variance(),b.variance(),tol) ) {259 if ( !suite.equal(a.variance(),b.variance(),tol) ) { 260 260 equal=false; 261 261 suite.err() << "error for variance:\t" << a.variance() << " " 262 262 << b.variance() << std::endl; 263 263 } 264 if ( ! test::equal(a.standard_error(),b.standard_error(),tol) ) {264 if ( !suite.equal(a.standard_error(),b.standard_error(),tol) ) { 265 265 equal =false; 266 266 suite.err() << "error for standard error:\t" << std::endl; … … 273 273 { 274 274 bool equal = true; 275 if ( ! test::equal(a.mean(),b.mean(),tol)){275 if ( !suite.equal(a.mean(),b.mean(),tol)){ 276 276 equal=false; 277 277 suite.err() << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl; 278 278 } 279 if ( ! test::equal(a.variance(),b.variance(),tol) ) {279 if ( !suite.equal(a.variance(),b.variance(),tol) ) { 280 280 equal=false; 281 281 suite.err() << "error for variance:\t" << a.variance() << " " 282 282 << b.variance() << std::endl; 283 283 } 284 if ( ! test::equal(a.standard_error(), b.standard_error(),tol) ) {284 if ( !suite.equal(a.standard_error(), b.standard_error(),tol) ) { 285 285 equal =false; 286 286 suite.err() << "error for standard error:\t" << std::endl; … … 332 332 { 333 333 bool ok = true; 334 if ( ! test::equal(a.covariance(),b.covariance(),tol) ){334 if ( !suite.equal(a.covariance(),b.covariance(),tol) ){ 335 335 suite.add(false); 336 336 suite.err() << "error covariance: " << a.covariance() << "\t" 337 337 << b.covariance() << std::endl; 338 338 } 339 if ( ! test::equal(a.correlation(), b.correlation(), tol) ) {339 if ( !suite.equal(a.correlation(), b.correlation(), tol) ) { 340 340 suite.add(false); 341 341 suite.err() << "error correlation" << std::endl; -
trunk/test/distance_test.cc
r1233 r1234 40 40 using namespace theplu::yat; 41 41 42 void check_equality(double, double, test::Suite&, const std::string&, 43 u_int N=1); 44 utility::Matrix data(void); 45 46 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&); 66 67 utility::Matrix weight(void); 68 42 69 int main(int argc, char* argv[]) 43 70 { … … 52 79 double tolerance=1e-4; 53 80 statistics::EuclideanDistance eucl_dist; 81 suite.err() << "testing EuclideanDistance" << std::endl; 82 test_distance(eucl_dist, suite); 54 83 double dist=eucl_dist(a.begin(),a.end(),b.begin()); 55 84 if(std::abs(dist-2.23607)>tolerance) { … … 59 88 60 89 statistics::PearsonDistance pear_dist; 90 suite.err() << "testing PearsonDistance" << std::endl; 91 test_distance(pear_dist, suite); 61 92 dist=pear_dist(a.begin(),a.end(),b.begin()); 62 93 if(std::abs(dist-1.5)>tolerance) { … … 113 144 } 114 145 146 115 147 return suite.return_value(); 116 148 } 117 149 118 150 151 void check_equality(double dist1, double dist2, test::Suite& suite, 152 const std::string& msg, u_int N) 153 { 154 if (!suite.equal(dist1, dist2, N)) { 155 suite.err() << "Error: " << msg << " failed.\n"; 156 suite.err() << " dist1: " << dist1 << "\n"; 157 suite.err() << " dist2: " << dist2 << "\n"; 158 suite.err() << " difference: " << dist1-dist2 << "\n"; 159 suite.err() << " error bound: " << suite.last_error_bound() << "\n"; 160 suite.add(false); 161 } 162 } 163 164 165 utility::Matrix data(void) 166 { 167 utility::Matrix res(2,10); 168 for (size_t i = 0; i<res.columns(); ++i){ 169 res(0,i) = i*i+1; 170 res(1,i) = 2*i+3; 171 } 172 return res; 173 } 174 175 template<class Distance> 176 void test_distance(Distance dist, theplu::yat::test::Suite& suite) 177 { 178 test_duplicate(dist, suite); 179 test_rescaling(dist, suite); 180 test_unity_weights(dist, suite); 181 test_self_distance(dist, suite); 182 test_symmetry(dist, suite); 183 test_zero_weight(dist, suite); 184 } 185 186 template<class Distance> 187 void test_duplicate(Distance dist, theplu::yat::test::Suite& suite) 188 { 189 utility::Matrix x(data()); 190 utility::Matrix x2(x.rows(), 2*x.columns()); 191 for (size_t i=0; i<x.rows(); ++i){ 192 std::copy(x.begin_row(i), x.end_row(i), x2.begin_row(i)); 193 std::copy(x.begin_row(i), x.end_row(i), x2.begin_row(i)+x.columns()); 194 } 195 utility::Matrix w(weight()); 196 utility::Matrix w2(w.rows(), 2*w.columns()); 197 for (size_t i=0; i<w.rows(); ++i){ 198 std::copy(w.begin_row(i), w.end_row(i), w2.begin_row(i)); 199 std::copy(w.begin_row(i), w.end_row(i), w2.begin_row(i)+w.columns()); 200 } 201 classifier::MatrixLookupWeighted ml(x2, w2); 202 double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 203 for (size_t i=0; i<w.columns(); ++i) 204 w2(0,i)=0.0; 205 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 206 check_equality(dist1, dist2, suite, "duplicate property"); 207 } 208 209 template<class Distance> 210 void test_rescaling(Distance dist, theplu::yat::test::Suite& suite) 211 { 212 utility::Matrix x=data(); 213 utility::Matrix w=weight(); 214 classifier::MatrixLookupWeighted ml(x,w); 215 double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 216 w *= 2.13; 217 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 218 check_equality(dist1, dist2, suite, "rescaling", 10); 219 } 220 221 template<class Distance> 222 void test_unity_weights(Distance dist, theplu::yat::test::Suite& suite) 223 { 224 utility::Matrix x=data(); 225 classifier::MatrixLookupWeighted ml(x); 226 double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 227 double dist2 = dist(x.begin_row(0), x.end_row(0), x.begin_row(1)); 228 check_equality(dist1, dist2, suite, "unity weights", 10); 229 } 230 231 template<class Distance> 232 void test_self_distance(Distance dist, theplu::yat::test::Suite& suite) 233 { 234 utility::Matrix x = data(); 235 double self = dist(x.begin(), x.end(), x.begin()); 236 if (!suite.equal(self, 0.0)){ 237 suite.err() << "error: self distance is " << self << "\n" 238 << "supposed to be zero.\n"; 239 suite.add(false); 240 } 241 } 242 243 244 template<class Distance> 245 void test_symmetry(Distance dist, theplu::yat::test::Suite& suite) 246 { 247 utility::Matrix x = data(); 248 double distab = dist(x.begin_row(0), x.end_row(0), x.begin_row(1)); 249 double distba = dist(x.begin_row(1), x.end_row(1), x.begin_row(0)); 250 check_equality(distab, distba, suite, "symmetry test"); 251 } 252 253 254 template<class Distance> 255 void test_zero_weight(Distance dist, theplu::yat::test::Suite& suite) 256 { 257 utility::Matrix x=data(); 258 utility::Matrix w=weight(); 259 classifier::MatrixLookupWeighted ml(x,w); 260 w(0,0) = 0.0; 261 double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 262 w(0,0) = 100*std::numeric_limits<double>().epsilon(); 263 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 264 check_equality(dist1, dist2, suite, "zero weight", 1000); 265 } 266 267 utility::Matrix weight(void) 268 { 269 utility::Matrix res(2,10); 270 for (size_t i = 0; i<res.columns(); ++i){ 271 res(0,i) = 1.0/(1+i); 272 res(1,i) = 1.0-0.1*i; 273 } 274 return res; 275 } 276 -
trunk/test/regression_test.cc
r1232 r1234 63 63 64 64 bool Local_test(regression::OneDimensionalWeighted&, 65 regression::Kernel& );65 regression::Kernel&, test::Suite&); 66 66 67 67 int main(int argc, char* argv[]) … … 81 81 regression::Polynomial polynomial(1); 82 82 polynomial.fit(x,y); 83 if ( test::equal(linear.beta(),polynomial.fit_parameters()(1)) ){83 if ( suite.equal(linear.beta(),polynomial.fit_parameters()(1)) ){ 84 84 suite.err() << "error: beta and fit_parameters(1) not equal" << std::endl; 85 85 suite.err() << " beta = " << linear.beta() << std::endl; … … 88 88 suite.add(false); 89 89 } 90 if ( test::equal(polynomial.fit_parameters()(0),90 if (suite.equal(polynomial.fit_parameters()(0), 91 91 linear.alpha()-linear.beta()*1985)){ 92 92 suite.err() << "error: fit_parameters(0) = " … … 177 177 y_predicted=naive_w.predict(0.0); 178 178 y_predicted_err=naive_w.prediction_error2(0.0); 179 if (! test::equal(y_predicted,0.1*12+0.2*11+0.3*14+0.4*13)) {179 if (!suite.equal(y_predicted,0.1*12+0.2*11+0.3*14+0.4*13)) { 180 180 suite.err() << "regression_NaiveWeighted: cannot reproduce fit.\n"; 181 181 suite.err() << "returned value: " << y_predicted << std::endl; … … 188 188 regression::KernelBox kb; 189 189 regression::LinearWeighted rl; 190 if (!Local_test(rl,kb )) {190 if (!Local_test(rl,kb, suite)) { 191 191 suite.err() << "regression_Local: Linear cannot reproduce fit." << std::endl; 192 192 suite.add(false); 193 193 } 194 194 regression::NaiveWeighted rn; 195 if (!Local_test(rn,kb )) {195 if (!Local_test(rn,kb, suite)) { 196 196 suite.err() << "regression_Local: Naive cannot reproduce fit." << std::endl; 197 197 suite.add(false); … … 413 413 z(1)=2000; 414 414 z(2)=2000*2000; 415 if (! test::equal(md.predict(z), mdw.predict(z))){415 if (!suite.equal(md.predict(z), mdw.predict(z))){ 416 416 suite.add(false); 417 417 suite.err() << "Error: predict not equal\n" … … 475 475 476 476 bool Local_test(regression::OneDimensionalWeighted& r, 477 regression::Kernel& k )477 regression::Kernel& k, test::Suite& suite) 478 478 { 479 479 regression::Local rl(r,k); … … 486 486 utility::Vector y(rl.y_predicted()); 487 487 for (size_t i=0; i<y.size(); i++) 488 if (! test::equal(y(i),10.0)){488 if (!suite.equal(y(i),10.0)){ 489 489 return false; 490 490 }
Note: See TracChangeset
for help on using the changeset viewer.