Changeset 1234 for trunk


Ignore:
Timestamp:
Mar 15, 2008, 5:00:54 PM (14 years ago)
Author:
Peter
Message:

working on #223

Location:
trunk/test
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Suite.cc

    r1232 r1234  
    7474
    7575
    76   bool equal(double a, double b, u_int n)
     76  bool Suite::equal(double a, double b, u_int n)
    7777  {
    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_;
    8187  }
    8288
  • trunk/test/Suite.h

    r1232 r1234  
    5454
    5555    /**
     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    /**
    5664      \return true if test is ok
    5765    */
     
    7785    bool ok_;
    7886    std::ofstream* dev_null_;
     87    double last_error_bound_;
    7988
    8089  };
    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);
    8790
    8891}}}
  • trunk/test/averager_test.cc

    r1232 r1234  
    244244//  std::cout << (a.mean()==b.mean()) << std::endl;
    245245//  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));
    248248}
    249249
     
    252252{
    253253  bool equal = true;
    254   if  ( !test::equal(a.mean(),b.mean(),tol)){
     254  if  ( !suite.equal(a.mean(),b.mean(),tol)){
    255255    equal=false;
    256256    suite.err() << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
    257257    suite.err() << "difference:\t" << a.mean()-b.mean() << std::endl;
    258258  }
    259   if ( !test::equal(a.variance(),b.variance(),tol) ) {
     259  if ( !suite.equal(a.variance(),b.variance(),tol) ) {
    260260    equal=false;
    261261    suite.err() << "error for variance:\t" << a.variance() << " "
    262262                << b.variance() << std::endl;
    263263  }
    264   if ( !test::equal(a.standard_error(),b.standard_error(),tol) ) {
     264  if ( !suite.equal(a.standard_error(),b.standard_error(),tol) ) {
    265265    equal =false;
    266266    suite.err() << "error for standard error:\t" << std::endl;
     
    273273{
    274274  bool equal = true;
    275   if  ( !test::equal(a.mean(),b.mean(),tol)){
     275  if  ( !suite.equal(a.mean(),b.mean(),tol)){
    276276    equal=false;
    277277    suite.err() << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
    278278  }
    279   if ( !test::equal(a.variance(),b.variance(),tol) ) {
     279  if ( !suite.equal(a.variance(),b.variance(),tol) ) {
    280280    equal=false;
    281281    suite.err() << "error for variance:\t" << a.variance() << " "
    282282                << b.variance() << std::endl;
    283283  }
    284   if ( !test::equal(a.standard_error(), b.standard_error(),tol) ) {
     284  if ( !suite.equal(a.standard_error(), b.standard_error(),tol) ) {
    285285    equal =false;
    286286    suite.err() << "error for standard error:\t" << std::endl;
     
    332332{
    333333  bool ok = true;
    334   if  ( !test::equal(a.covariance(),b.covariance(),tol) ){
     334  if  ( !suite.equal(a.covariance(),b.covariance(),tol) ){
    335335    suite.add(false);
    336336    suite.err() << "error covariance: " << a.covariance() << "\t"
    337337           << b.covariance() << std::endl;
    338338  }
    339   if ( !test::equal(a.correlation(), b.correlation(), tol) ) {
     339  if ( !suite.equal(a.correlation(), b.correlation(), tol) ) {
    340340    suite.add(false);
    341341    suite.err() << "error correlation" << std::endl;
  • trunk/test/distance_test.cc

    r1233 r1234  
    4040using namespace theplu::yat;
    4141
     42void check_equality(double, double, test::Suite&, const std::string&,
     43                    u_int N=1);
     44utility::Matrix data(void);
     45
     46template<class Distance>
     47void test_distance(Distance, theplu::yat::test::Suite&);
     48
     49template<class Distance>
     50void test_duplicate(Distance, theplu::yat::test::Suite&);
     51
     52template<class Distance>
     53void test_rescaling(Distance, theplu::yat::test::Suite&);
     54
     55template<class Distance>
     56void test_unity_weights(Distance, theplu::yat::test::Suite&);
     57
     58template<class Distance>
     59void test_self_distance(Distance, theplu::yat::test::Suite&);
     60
     61template<class Distance>
     62void test_symmetry(Distance, theplu::yat::test::Suite&);
     63
     64template<class Distance>
     65void test_zero_weight(Distance, theplu::yat::test::Suite&);
     66
     67utility::Matrix weight(void);
     68
    4269int main(int argc, char* argv[])
    4370
     
    5279  double tolerance=1e-4;
    5380  statistics::EuclideanDistance eucl_dist;
     81  suite.err() << "testing EuclideanDistance" << std::endl;
     82  test_distance(eucl_dist, suite);
    5483  double dist=eucl_dist(a.begin(),a.end(),b.begin());
    5584  if(std::abs(dist-2.23607)>tolerance) {
     
    5988 
    6089  statistics::PearsonDistance pear_dist;
     90  suite.err() << "testing PearsonDistance" << std::endl;
     91  test_distance(pear_dist, suite);
    6192  dist=pear_dist(a.begin(),a.end(),b.begin());
    6293  if(std::abs(dist-1.5)>tolerance) {
     
    113144  }
    114145 
     146
    115147  return suite.return_value();
    116148}
    117149
    118150
     151void 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
     165utility::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
     175template<class Distance>
     176void 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
     186template<class Distance>
     187void 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
     209template<class Distance>
     210void 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
     221template<class Distance>
     222void 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
     231template<class Distance>
     232void 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
     244template<class Distance>
     245void 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
     254template<class Distance>
     255void 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
     267utility::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  
    6363
    6464bool Local_test(regression::OneDimensionalWeighted&,
    65                 regression::Kernel&);
     65                regression::Kernel&, test::Suite&);
    6666
    6767int main(int argc, char* argv[])
     
    8181  regression::Polynomial polynomial(1);
    8282  polynomial.fit(x,y);
    83   if ( test::equal(linear.beta(),polynomial.fit_parameters()(1)) ){
     83  if ( suite.equal(linear.beta(),polynomial.fit_parameters()(1)) ){
    8484    suite.err() << "error: beta and fit_parameters(1) not equal" << std::endl;
    8585    suite.err() << "       beta = " << linear.beta() << std::endl;
     
    8888    suite.add(false);
    8989  }
    90   if (test::equal(polynomial.fit_parameters()(0),
     90  if (suite.equal(polynomial.fit_parameters()(0),
    9191                  linear.alpha()-linear.beta()*1985)){
    9292    suite.err() << "error: fit_parameters(0) = "
     
    177177  y_predicted=naive_w.predict(0.0);
    178178  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)) {
    180180    suite.err() << "regression_NaiveWeighted: cannot reproduce fit.\n";
    181181    suite.err() << "returned value: " << y_predicted << std::endl;
     
    188188  regression::KernelBox kb;
    189189  regression::LinearWeighted rl;
    190   if (!Local_test(rl,kb)) {
     190  if (!Local_test(rl,kb, suite)) {
    191191    suite.err() << "regression_Local: Linear cannot reproduce fit." << std::endl;
    192192    suite.add(false);
    193193  }
    194194  regression::NaiveWeighted rn;
    195   if (!Local_test(rn,kb)) {
     195  if (!Local_test(rn,kb, suite)) {
    196196    suite.err() << "regression_Local: Naive cannot reproduce fit." << std::endl;
    197197    suite.add(false);
     
    413413  z(1)=2000;
    414414  z(2)=2000*2000;
    415   if (!test::equal(md.predict(z), mdw.predict(z))){
     415  if (!suite.equal(md.predict(z), mdw.predict(z))){
    416416    suite.add(false);
    417417    suite.err() << "Error: predict not equal\n"
     
    475475
    476476bool Local_test(regression::OneDimensionalWeighted& r,
    477                 regression::Kernel& k)
     477                regression::Kernel& k, test::Suite& suite)
    478478{
    479479  regression::Local rl(r,k);
     
    486486  utility::Vector y(rl.y_predicted());
    487487  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)){
    489489      return false;
    490490    }
Note: See TracChangeset for help on using the changeset viewer.