Changeset 1582 for trunk


Ignore:
Timestamp:
Oct 15, 2008, 7:59:21 PM (13 years ago)
Author:
Peter
Message:

refs #396 - fixing distance_test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/distance_test.cc

    r1487 r1582  
    2626#include "yat/statistics/EuclideanDistance.h"
    2727#include "yat/statistics/PearsonDistance.h"
     28#include "yat/utility/DataIterator.h"
    2829#include "yat/utility/Matrix.h"
    2930#include "yat/utility/MatrixWeighted.h"
    3031#include "yat/utility/Vector.h"
     32#include "yat/utility/WeightIterator.h"
    3133
    3234#include <cassert>
     
    4345                    unsigned long int N=1);
    4446utility::Matrix data(void);
     47utility::MatrixWeighted data_weighted(void);
    4548
    4649template<class Distance>
     
    8588  test_distance(eucl_dist, suite, 100);
    8689  double dist=eucl_dist(a.begin(),a.end(),b.begin());
    87   if(std::abs(dist-2.23607)>tolerance) {
     90  if (!suite.equal(dist, 2.23606797749978988178)) {
    8891    suite.err() << "Error in unweighted Euclidean distance " << std::endl;
    8992    suite.add(false);
     
    9497  test_distance(pear_dist, suite, 1000);
    9598  dist=pear_dist(a.begin(),a.end(),b.begin());
    96   if(std::abs(dist-1.5)>tolerance) {
     99  if (!suite.equal(dist, 1.5)) {
    97100    suite.err() << "Error in unweighted Pearson distance " << std::endl;
    98101    suite.add(false);
     
    105108  m(1,0)=0;
    106109  m(1,1)=0;
    107   utility::Matrix w(2,3,1);
    108   w(0,0)=0;
    109   classifier::MatrixLookupWeighted mw(m,w);
     110  utility::MatrixWeighted m_w(m);
     111  m_w(0,0).weight()=0;
     112  classifier::MatrixLookupWeighted mw(m_w);
    110113  classifier::DataLookupWeighted1D aw(mw,0,true);
    111114  classifier::DataLookupWeighted1D bw(mw,1,true);
     
    113116  dist=eucl_dist(aw.begin(),aw.end(),bw.begin());
    114117 
    115   if(std::abs(dist-sqrt(6))>tolerance) {
     118  if (!suite.equal_sqrt(dist, sqrt(6))) {
    116119    suite.err() << "Error in weighted Euclidean distance " << std::endl;
    117120    suite.add(false);
     
    120123  dist=pear_dist(aw.begin(),aw.end(),bw.begin());
    121124 
    122   if(std::abs(dist-2)>tolerance) {
     125  if (!suite.equal(dist, 2)) {
    123126    suite.err() << "Error in weighted Pearson distance " << std::endl;
    124127    suite.add(false);
     
    172175}
    173176
     177utility::MatrixWeighted data_weighted(void)
     178{
     179  utility::Matrix x = data();
     180  utility::Matrix w = weight();
     181  utility::MatrixWeighted res(x.rows(), x.columns());
     182  std::copy(x.begin(), x.end(), utility::data_iterator(res.begin()));
     183  std::copy(w.begin(), w.end(), utility::weight_iterator(res.begin()));
     184  return res;
     185}
     186
    174187template<class Distance>
    175188void test_distance(Distance dist, theplu::yat::test::Suite& suite,
     
    188201                    unsigned long int N)
    189202{
    190   utility::Matrix x(data());
    191   utility::Matrix x2(x.rows(), 2*x.columns());
     203  utility::MatrixWeighted x(data_weighted());
     204  utility::MatrixWeighted mw(x.rows(), 2*x.columns());
    192205  for (size_t i=0; i<x.rows(); ++i){
    193     std::copy(x.begin_row(i), x.end_row(i), x2.begin_row(i));
    194     std::copy(x.begin_row(i), x.end_row(i), x2.begin_row(i)+x.columns());
    195   }
    196   utility::Matrix w(weight());
    197   utility::Matrix w2(w.rows(), 2*w.columns());
    198   for (size_t i=0; i<w.rows(); ++i){
    199     std::copy(w.begin_row(i), w.end_row(i), w2.begin_row(i));
    200     std::copy(w.begin_row(i), w.end_row(i), w2.begin_row(i)+w.columns());
    201   }
    202   classifier::MatrixLookupWeighted ml(x2, w2);
    203   double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
    204   for (size_t i=0; i<w.columns(); ++i)
    205     w2(0,i)=0.0;
    206   double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     206    std::copy(x.begin_row(i), x.end_row(i), mw.begin_row(i));
     207    std::copy(x.begin_row(i), x.end_row(i), mw.begin_row(i)+x.columns());
     208  }
     209  double dist1 = dist(mw.begin_row(0), mw.end_row(0), mw.begin_row(1));
     210  for (size_t i=0; i<x.columns(); ++i)
     211    mw(0,i).weight()=0.0;
     212  double dist2 = dist(mw.begin_row(0), mw.end_row(0), mw.begin_row(1));
    207213  check_equality(dist1, dist2, suite, "duplicate property", N);
    208214}
     
    212218                    unsigned long int N)
    213219{
    214   utility::Matrix x=data();
    215   utility::Matrix w=weight();
    216   classifier::MatrixLookupWeighted ml(x,w);
    217   double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
    218   w *= 2.13;
    219   double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     220  utility::MatrixWeighted wx=data_weighted();
     221  double dist1 = dist(wx.begin_row(0), wx.end_row(0), wx.begin_row(1));
     222  // rescale weights
     223  for (size_t i=0; i<wx.rows(); ++i)
     224    for (size_t j=0; j<wx.columns(); ++j)
     225      wx(i,j).weight() *= 2.13;
     226  double dist2 = dist(wx.begin_row(0), wx.end_row(0), wx.begin_row(1));
    220227  check_equality(dist1, dist2, suite, "rescaling", N);
    221228}
     
    261268                      unsigned long int N)
    262269{
    263   utility::Matrix x=data();
    264   utility::Matrix w=weight();
    265   classifier::MatrixLookupWeighted ml(x,w);
    266   w(0,0) = 0.0;
    267   double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
    268   w(0,0) = 100*std::numeric_limits<double>().epsilon();
    269   double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     270  utility::MatrixWeighted wx=data_weighted();
     271  wx(0,0).weight() = 0.0;
     272  double dist1 = dist(wx.begin_row(0), wx.end_row(0), wx.begin_row(1));
     273  wx(0,0).weight() = 100*std::numeric_limits<double>().epsilon();
     274  double dist2 = dist(wx.begin_row(0), wx.end_row(0), wx.begin_row(1));
    270275  check_equality(dist1, dist2, suite, "zero weight", N);
    271276}
Note: See TracChangeset for help on using the changeset viewer.