Changeset 1247


Ignore:
Timestamp:
Mar 17, 2008, 3:54:10 PM (13 years ago)
Author:
Peter
Message:

refs #223

Location:
trunk/test
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Suite.cc

    r1244 r1247  
    9090
    9191
     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
    92108  bool Suite::ok(void) const
    93109  {
  • trunk/test/Suite.h

    r1244 r1247  
    5959    bool equal(double a, double b, unsigned long int N=1);
    6060
     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
    6167    template<typename Iterator1, typename Iterator2>
    6268    bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
  • trunk/test/distance_test.cc

    r1239 r1247  
    3434#include <fstream>
    3535#include <iostream>
     36#include <limits>
    3637#include <list>
    3738#include <vector>
     
    4546
    4647template<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&);
     48void test_distance(Distance, theplu::yat::test::Suite&, unsigned long int N=1);
     49
     50template<class Distance>
     51void test_duplicate(Distance, theplu::yat::test::Suite&, unsigned long int N=1);
     52
     53template<class Distance>
     54void test_rescaling(Distance, theplu::yat::test::Suite&, unsigned long int N=1);
     55
     56template<class Distance>
     57void test_unity_weights(Distance, theplu::yat::test::Suite&,
     58                        unsigned long int N=1);
     59
     60template<class Distance>
     61void test_self_distance(Distance, theplu::yat::test::Suite&,
     62                        unsigned long int N=1);
     63
     64template<class Distance>
     65void test_symmetry(Distance, theplu::yat::test::Suite&, unsigned long int N=1);
     66
     67template<class Distance>
     68void test_zero_weight(Distance, theplu::yat::test::Suite&,
     69                      unsigned long int N=1);
    6670
    6771utility::Matrix weight(void);
     
    8084  statistics::EuclideanDistance eucl_dist;
    8185  suite.err() << "testing EuclideanDistance" << std::endl;
    82   test_distance(eucl_dist, suite);
     86  test_distance(eucl_dist, suite, 100);
    8387  double dist=eucl_dist(a.begin(),a.end(),b.begin());
    8488  if(std::abs(dist-2.23607)>tolerance) {
     
    8993  statistics::PearsonDistance pear_dist;
    9094  suite.err() << "testing PearsonDistance" << std::endl;
    91   test_distance(pear_dist, suite);
     95  test_distance(pear_dist, suite, 1000);
    9296  dist=pear_dist(a.begin(),a.end(),b.begin());
    9397  if(std::abs(dist-1.5)>tolerance) {
     
    170174
    171175template<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)
     176void 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
     187template<class Distance>
     188void test_duplicate(Distance dist, theplu::yat::test::Suite& suite,
     189                    unsigned long int N)
    184190{
    185191  utility::Matrix x(data());
     
    200206    w2(0,i)=0.0;
    201207  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
     211template<class Distance>
     212void test_rescaling(Distance dist, theplu::yat::test::Suite& suite,
     213                    unsigned long int N)
    207214{
    208215  utility::Matrix x=data();
     
    212219  w *= 2.13;
    213220  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
     224template<class Distance>
     225void test_unity_weights(Distance dist, theplu::yat::test::Suite& suite,
     226                        unsigned long int N)
    219227{
    220228  utility::Matrix x=data();
     
    222230  double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
    223231  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
     235template<class Distance>
     236void test_self_distance(Distance dist, theplu::yat::test::Suite& suite,
     237                        unsigned long int N)
    229238{
    230239  utility::Matrix x = data();
    231240  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()) ){
    233242    suite.err() << "error: self distance is " << self << "\n"
    234243                << "supposed to be zero.\n";
     
    239248
    240249template<class Distance>
    241 void test_symmetry(Distance dist, theplu::yat::test::Suite& suite)
     250void test_symmetry(Distance dist, theplu::yat::test::Suite& suite,
     251                   unsigned long int N)
    242252{
    243253  utility::Matrix x = data();
    244254  double distab = dist(x.begin_row(0), x.end_row(0), x.begin_row(1));
    245255  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
     260template<class Distance>
     261void test_zero_weight(Distance dist, theplu::yat::test::Suite& suite,
     262                      unsigned long int N)
    252263{
    253264  utility::Matrix x=data();
     
    258269  w(0,0) = 100*std::numeric_limits<double>().epsilon();
    259270  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);
    261272}
    262273
  • trunk/test/regression_test.cc

    r1244 r1247  
    9090  }
    9191  if (!suite.equal(polynomial.fit_parameters()(0),
    92                    linear.alpha()-linear.beta()*1985, 1000)){
     92                   linear.alpha()-linear.beta()*1985, 10000)){
    9393    suite.err() << "error: fit_parameters(0) = "
    9494           << polynomial.fit_parameters()(0)<< std::endl;
     
    108108  }
    109109  if (!suite.equal(polynomial.standard_error2(1985),
    110                    linear.standard_error2(1985), 1e5)){
     110                   linear.standard_error2(1985), 100000)){
    111111    suite.err() << "error: standard_error not same in linear and polynomial(1)"
    112112                << "\n  polynomial: " << polynomial.standard_error2(1985)
     
    131131  // Comparing LinearWeighted and PolynomialWeighted(1)
    132132  suite.err() << "    comparing LinearWeighted and PolynomialWeighted(1)"
    133          << std::endl;
     133              << std::endl;
    134134  linear_w.fit(x,y,w);
    135135  regression::PolynomialWeighted polynomial_w(1);
    136136  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) ){
    138138    suite.err() << "error: beta and fit_parameters(1) not equal" << std::endl;
    139139    suite.err() << "       beta = " << linear_w.beta() << std::endl;
     
    143143  }
    144144  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) ){
    146146    suite.err() << "error: fit_parameters(0) = "
    147147           << polynomial.fit_parameters()(0)<< std::endl;
     
    155155    suite.add(false);
    156156  }
    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) ){
    158158    suite.err() << "error: predict not same in linear and polynomial(1)"
    159159           << std::endl;
     
    161161  }
    162162  if ( !suite.equal(polynomial_w.standard_error2(1985),
    163                     linear_w.standard_error2(1985), 1e5) ){
     163                    linear_w.standard_error2(1985), 100000) ){
    164164    suite.err() << "error: standard_error not same in linear and polynomial(1)"
    165165           << "\n  polynomial: " << polynomial_w.standard_error2(1985)
     
    304304  w*=2;
    305305  wr.fit(x,y,w);
    306   if (!suite.equal(wr.predict(2000), predict, 1e4) ){
     306  if (!suite.equal(wr.predict(2000), predict, 10000) ){
    307307    suite.add(false);
    308308    suite.err() << "Error: predict not equal after rescaling.\n";
     
    318318    suite.err() << "difference " << s2-wr.s2(2.0) << std::endl;
    319319  }
    320   if (!suite.equal(wr.standard_error2(2000), standard_error2, 1e9) ){
     320  if (!suite.equal_sqrt(wr.standard_error2(2000), standard_error2, 10) ){
    321321    suite.add(false);
    322322    suite.err() << "Error: standard_error2 not equal after rescaling.\n";
     
    332332    suite.err() << "Error: r2 not equal after rescaling.\n";
    333333  }
    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) ){
    335335    suite.add(false);
    336336    suite.err() << "Error: prediction_error2 not equal after rescaling.\n";
     
    366366
    367367  wr.fit(x2,y2,w2);
    368   if (!suite.equal(wr.predict(2000), predict) ) {
     368  if (!suite.equal(wr.predict(2000), predict, 10000) ) {
    369369    suite.add(false);
    370370    suite.err() << "Error: predict not equal.\n";
     
    447447  w*=2;
    448448  mdw.fit(data,y,w);
    449   if (!suite.equal(mdw.predict(z), predict, 1e4) ){
     449  if (!suite.equal(mdw.predict(z), predict, 10000) ){
    450450    suite.add(false);
    451451    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";
    453454    suite.err() << "   predict = " << mdw.predict(z) << "\n";
    454455  }
    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) ){
    456457    suite.add(false);
    457458    suite.err() << "Error: prediction_error2 not equal after rescaling.\n";
     
    467468    suite.err() << "       s2 = " << mdw.s2(2) << "\n";
    468469  }
    469   if (!suite.equal(mdw.standard_error2(z), standard_error2, 1e9) ){
     470  if (!suite.equal_sqrt(mdw.standard_error2(z), standard_error2, 10) ){
    470471    suite.add(false);
    471472    suite.err() << "Error: standard_error2 not equal after rescaling.\n";
Note: See TracChangeset for help on using the changeset viewer.