Changeset 1031


Ignore:
Timestamp:
Feb 4, 2008, 4:44:44 PM (13 years ago)
Author:
Markus Ringnér
Message:

Fixes #272

Location:
trunk
Files:
7 edited
3 moved

Legend:

Unmodified
Added
Removed
  • trunk/test/knn_test.cc

    r999 r1031  
    2424#include "yat/classifier/KNN.h"
    2525#include "yat/classifier/MatrixLookupWeighted.h"
    26 #include "yat/statistics/euclidean_vector_distance.h"
    27 #include "yat/statistics/pearson_vector_distance.h"
     26#include "yat/statistics/euclidean_distance.h"
     27#include "yat/statistics/pearson_distance.h"
    2828#include "yat/utility/matrix.h"
    2929
     
    6666
    6767  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
    68   classifier::KNN<statistics::pearson_vector_distance_tag> knn(dataviewweighted,targets);
     68  classifier::KNN<statistics::pearson_distance_tag> knn(dataviewweighted,targets);
    6969  *error << "training KNN" << std::endl;
    7070  knn.train();
  • trunk/test/ncc_test.cc

    r1013 r1031  
    3131#include "yat/classifier/Target.h"
    3232#include "yat/utility/matrix.h"
    33 #include "yat/statistics/euclidean_vector_distance.h"
    34 #include "yat/statistics/pearson_vector_distance.h"
     33#include "yat/statistics/euclidean_distance.h"
     34#include "yat/statistics/pearson_distance.h"
    3535#include "yat/utility/utility.h"
    3636
     
    7878  vec[3]="bjds";
    7979  classifier::Target target(vec);
    80   classifier::NCC<statistics::pearson_vector_distance_tag> ncctmp(ml,target);
     80  classifier::NCC<statistics::pearson_distance_tag> ncctmp(ml,target);
    8181  *error << "training...\n";
    8282  ncctmp.train();
     
    100100  classifier::Target target1(vec1);
    101101
    102   classifier::NCC<statistics::euclidean_vector_distance_tag> ncc1(ml1,target1);
     102  classifier::NCC<statistics::euclidean_distance_tag> ncc1(ml1,target1);
    103103  ncc1.train();
    104104  utility::matrix prediction1;
     
    150150     
    151151  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
    152   classifier::NCC<statistics::pearson_vector_distance_tag> ncc(dataviewweighted,targets);
     152  classifier::NCC<statistics::pearson_distance_tag> ncc(dataviewweighted,targets);
    153153  *error << "training...\n";
    154154  ncc.train();
  • trunk/test/vector_distance_test.cc

    r1012 r1031  
    2424#include "yat/classifier/DataLookupWeighted1D.h"
    2525#include "yat/classifier/MatrixLookupWeighted.h"
    26 #include "yat/statistics/euclidean_vector_distance.h"
    27 #include "yat/statistics/pearson_vector_distance.h"
     26#include "yat/statistics/euclidean_distance.h"
     27#include "yat/statistics/pearson_distance.h"
    2828#include "yat/utility/matrix.h"
    2929#include "yat/utility/vector.h"
     
    4747    error = new std::ofstream("/dev/null");
    4848    if (argc>1)
    49       std::cout << "vector_distance_test -v : for printing extra information\n";
     49      std::cout << "distance_test -v : for printing extra information\n";
    5050  }
    51   *error << "testing vector_distance" << std::endl;
     51  *error << "testing distance" << std::endl;
    5252  bool ok = true;
    5353 
     
    5959  double tolerance=1e-4;
    6060 
    61   double dist=statistics::vector_distance(a.begin(),a.end(),b.begin(),
    62                                           statistics::euclidean_vector_distance_tag());
     61  double dist=statistics::distance(a.begin(),a.end(),b.begin(),
     62                                          statistics::euclidean_distance_tag());
    6363  if(fabs(dist-2.23607)>tolerance) {
    64     *error << "Error in unweighted Euclidean vector_distance " << std::endl;
     64    *error << "Error in unweighted Euclidean distance " << std::endl;
    6565    ok=false;
    6666  }
    6767 
    68   dist=statistics::vector_distance(a.begin(),a.end(),b.begin(),
    69                                    statistics::pearson_vector_distance_tag()); 
     68  dist=statistics::distance(a.begin(),a.end(),b.begin(),
     69                                   statistics::pearson_distance_tag());
    7070  if(fabs(dist-1.5)>tolerance) {
    71     *error << "Error in unweighted Pearson vector_distance " << std::endl;
     71    *error << "Error in unweighted Pearson distance " << std::endl;
    7272    ok=false;
    7373  }
     
    8585  classifier::DataLookupWeighted1D bw(mw,1,true);
    8686 
    87   dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
    88                                    statistics::euclidean_vector_distance_tag());
     87  dist=statistics::distance(aw.begin(),aw.end(),bw.begin(),
     88                                   statistics::euclidean_distance_tag());
    8989 
    9090  if(fabs(dist-sqrt(6))>tolerance) {
    91     *error << "Error in weighted Euclidean vector_distance " << std::endl;
     91    *error << "Error in weighted Euclidean distance " << std::endl;
    9292    ok=false;
    9393  }
    9494 
    95   dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
    96                                    statistics::pearson_vector_distance_tag());
     95  dist=statistics::distance(aw.begin(),aw.end(),bw.begin(),
     96                                   statistics::pearson_distance_tag());
    9797 
    9898  if(fabs(dist-2)>tolerance) {
    99     *error << "Error in weighted Pearson vector_distance " << std::endl;
     99    *error << "Error in weighted Pearson distance " << std::endl;
    100100    ok=false;
    101101  }
     
    108108  sb[2] = 1;
    109109 
    110   dist=statistics::vector_distance(sa.begin(),sa.end(),sb.begin(),
    111                                    statistics::euclidean_vector_distance_tag()); 
     110  dist=statistics::distance(sa.begin(),sa.end(),sb.begin(),
     111                                   statistics::euclidean_distance_tag());
    112112  if(fabs(dist-2.23607)>tolerance) {
    113     *error << "Error in vector_distance for std::vector " << std::endl;
     113    *error << "Error in distance for std::vector " << std::endl;
    114114    ok=false;
    115115  }
     
    118118  std::list<double> la;
    119119  std::copy(sa.begin(),sa.end(),std::back_inserter<std::list<double> >(la));
    120   dist=statistics::vector_distance(la.begin(),la.end(),sb.begin(),
    121                                    statistics::euclidean_vector_distance_tag()); 
     120  dist=statistics::distance(la.begin(),la.end(),sb.begin(),
     121                                   statistics::euclidean_distance_tag());
    122122  if(fabs(dist-2.23607)>tolerance) {
    123     *error << "Error in vector_distance for std::list " << std::endl;
     123    *error << "Error in distance for std::list " << std::endl;
    124124    ok=false;
    125125  }
    126126 
    127127  if(!ok) {
    128     *error << "vector_distance_test failed" << std::endl;
     128    *error << "distance_test failed" << std::endl;
    129129  }
    130130  if (error!=&std::cerr)
  • trunk/yat/classifier/IGP.h

    r1009 r1031  
    3030#include "yat/utility/vector.h"
    3131#include "yat/utility/yat_assert.h"
    32 #include "yat/statistics/vector_distance.h"
     32#include "yat/statistics/distance.h"
    3333
    3434#include <cmath>
     
    9595        DataLookup1D b(matrix_,j,false);
    9696        double dist=statistics::
    97           vector_distance(a.begin,a.end(),b.begin(),
    98                           statistics::vector_distance_traits<Distance>::distace());
     97          distance(a.begin,a.end(),b.begin(),
     98                          statistics::distance_traits<Distance>::distace());
    9999        if(j!=i && dist<mindist) {
    100100          mindist=dist;
  • trunk/yat/classifier/KNN.h

    r1028 r1031  
    3030#include "SupervisedClassifier.h"
    3131#include "Target.h"
    32 #include "yat/statistics/vector_distance.h"
     32#include "yat/statistics/distance.h"
    3333#include "yat/utility/matrix.h"
    3434#include "yat/utility/yat_assert.h"
     
    144144 
    145145  template <typename Distance>
    146   utility::matrix* KNN<Distance>::calculate_distances(const DataLookup2D& input) const
     146  utility::matrix* KNN<Distance>::calculate_distances(const DataLookup2D& test) const
    147147  {
    148148    // matrix with training samples as rows and test samples as columns
    149149    utility::matrix* distances =
    150       new utility::matrix(data_.columns(),input.columns());
    151    
    152     // if both training and test are unweighted: unweighted
    153     // calculations are used.
    154     const MatrixLookup* test_unweighted =
    155       dynamic_cast<const MatrixLookup*>(&input);     
    156     if(test_unweighted && !data_.weighted()) {
    157       const MatrixLookup* data_unweighted =
    158         dynamic_cast<const MatrixLookup*>(&data_);     
     150      new utility::matrix(data_.columns(),test.columns());
     151   
     152    // unweighted test data
     153    if(const MatrixLookup* test_unweighted =
     154       dynamic_cast<const MatrixLookup*>(&test)) {     
    159155      for(size_t i=0; i<data_.columns(); i++) {
    160         classifier::DataLookup1D training(*data_unweighted,i,false);
    161         for(size_t j=0; j<input.columns(); j++) {
     156        for(size_t j=0; j<test.columns(); j++) {
    162157          classifier::DataLookup1D test(*test_unweighted,j,false);
    163           utility::yat_assert<std::runtime_error>(training.size()==test.size());
    164158          (*distances)(i,j) =
    165             statistics::vector_distance(training.begin(),training.end(),
    166                                         test.begin(), typename statistics::vector_distance_traits<Distance>::distance());
     159            statistics::distance(classifier::DataLookup1D(data_,
     160                                                          i,false).begin(),
     161                                 classifier::DataLookup1D(data_,
     162                                                          i,false).end(),
     163                                 test.begin(),
     164                                 typename statistics::
     165                                 distance_traits<Distance>::distance());
    167166          utility::yat_assert<std::runtime_error>(!std::isnan((*distances)(i,j)));
    168167        }
    169168      }
    170169    }
    171     // if either training or test is weighted: weighted calculations
    172     // are used.
     170    // weighted test data
    173171    else {
    174172      const MatrixLookupWeighted* data_weighted =
    175173        dynamic_cast<const MatrixLookupWeighted*>(&data_);
    176174      const MatrixLookupWeighted* test_weighted =
    177         dynamic_cast<const MatrixLookupWeighted*>(&input);               
     175        dynamic_cast<const MatrixLookupWeighted*>(&test);               
    178176      if(data_weighted && test_weighted) {
    179177        for(size_t i=0; i<data_.columns(); i++) {
    180178          classifier::DataLookupWeighted1D training(*data_weighted,i,false);
    181           for(size_t j=0; j<input.columns(); j++) {
     179          for(size_t j=0; j<test.columns(); j++) {
    182180            classifier::DataLookupWeighted1D test(*test_weighted,j,false);
    183181            utility::yat_assert<std::runtime_error>(training.size()==test.size());
    184182            (*distances)(i,j) =
    185               statistics::vector_distance(training.begin(),training.end(),
    186                                           test.begin(), typename statistics::vector_distance_traits<Distance>::distance());
     183              statistics::distance(training.begin(),training.end(),
     184                                   test.begin(), typename statistics::distance_traits<Distance>::distance());
    187185            utility::yat_assert<std::runtime_error>(!std::isnan((*distances)(i,j)));
    188186          }
     
    251249
    252250  template <typename Distance>
    253   void KNN<Distance>::predict(const DataLookup2D& input,                   
     251  void KNN<Distance>::predict(const DataLookup2D& test,                     
    254252                              utility::matrix& prediction) const
    255253  {   
    256     utility::matrix* distances=calculate_distances(input);
     254    utility::yat_assert<std::runtime_error>(data_.rows()==test.rows());
     255
     256    utility::matrix* distances=calculate_distances(test);
    257257   
    258258    // for each test sample (column in distances) find the closest
    259259    // training samples
    260     prediction.clone(utility::matrix(target_.nof_classes(),input.columns(),0.0));
     260    prediction.clone(utility::matrix(target_.nof_classes(),test.columns(),0.0));
    261261    for(size_t sample=0;sample<distances->columns();sample++) {
    262262      std::vector<size_t> k_index;
  • trunk/yat/classifier/NCC.h

    r1013 r1031  
    3737#include "yat/statistics/Averager.h"
    3838#include "yat/statistics/AveragerWeighted.h"
    39 #include "yat/statistics/vector_distance.h"
     39#include "yat/statistics/distance.h"
    4040
    4141#include "yat/utility/Iterator.h"
     
    112112    // MatrixLookup and MatrixLookupWeighted
    113113    const DataLookup2D& data_;
    114 
     114    bool centroids_nan_;
    115115  };
    116116
     
    125125  template <typename Distance>
    126126  NCC<Distance>::NCC(const MatrixLookup& data, const Target& target)
    127     : SupervisedClassifier(target), centroids_(0), data_(data)
     127    : SupervisedClassifier(target), centroids_(0), data_(data), centroids_nan_(false)
    128128  {
    129129  }
     
    131131  template <typename Distance>
    132132  NCC<Distance>::NCC(const MatrixLookupWeighted& data, const Target& target)
    133     : SupervisedClassifier(target), centroids_(0), data_(data)
     133    : SupervisedClassifier(target), centroids_(0), data_(data), centroids_nan_(false)
    134134  {
    135135  }
     
    169169                              target);
    170170      }
    171       ncc->centroids_=0;
    172171    }
    173172    catch (std::bad_cast) {
     
    198197        for(size_t c=0;c<target_.nof_classes();c++) {
    199198          (*centroids_)(i,c) = class_averager[c].mean();
     199          if(class_averager[c].sum_w()==0)
     200            centroids_nan_=true;
    200201        }
    201202      }
     
    230231    prediction.clone(utility::matrix(centroids_->columns(), test.columns()));       
    231232
    232     // unweighted test data
     233    // unweighted test data and no nan's in centroids
     234    // Markus: Should test centroid_nan_ here!!!
    233235    if (const MatrixLookup* test_unweighted =
    234236        dynamic_cast<const MatrixLookup*>(&test)) {
     
    240242          utility::yat_assert<std::runtime_error>(in.size()==centroid.size());
    241243          prediction(k,j)=statistics::
    242             vector_distance(in.begin(),in.end(),centroid.begin(),
    243                             typename statistics::vector_distance_traits<Distance>::distance());
     244            distance(in.begin(),in.end(),centroid.begin(),
     245                            typename statistics::distance_traits<Distance>::distance());
    244246        }
    245247      }
     
    255257          utility::yat_assert<std::runtime_error>(in.size()==centroid.size());
    256258          prediction(k,j)=statistics::
    257             vector_distance(in.begin(),in.end(),centroid.begin(),
    258                             typename statistics::vector_distance_traits<Distance>::distance());
     259            distance(in.begin(),in.end(),centroid.begin(),
     260                            typename statistics::distance_traits<Distance>::distance());
    259261        }
    260262      }
  • trunk/yat/statistics/Makefile.am

    r1003 r1031  
    4040include_statistics_HEADERS = AUC.h Averager.h AveragerPair.h \
    4141  AveragerWeighted.h AveragerPairWeighted.h \
    42   euclidean_vector_distance.h Fisher.h \
     42  distance.h euclidean_distance.h Fisher.h \
    4343  FoldChange.h Histogram.h \
    4444  KolmogorovSmirnov.h \
    4545  Pearson.h PearsonCorrelation.h \
    46   pearson_vector_distance.h ROC.h \
     46  pearson_distance.h ROC.h \
    4747  SAMScore.h Score.h SNRScore.h tScore.h tTest.h \
    48   utility.h vector_distance.h VectorFunction.h WilcoxonFoldChange.h
     48  utility.h VectorFunction.h WilcoxonFoldChange.h
  • trunk/yat/statistics/distance.h

    r1030 r1031  
    1 #ifndef theplu_yat_statistics_vector_distance_h
    2 #define theplu_yat_statistics_vector_distance_h
     1#ifndef theplu_yat_statistics_distance_h
     2#define theplu_yat_statistics_distance_h
    33
    44// $Id$
     
    3939  /// For each measure to calculate distances between containers
    4040  /// with random access iterators one should provide a struct
    41   /// that inherits from vector_distance_tag and provides a "label" for
     41  /// that inherits from distance_tag and provides a "label" for
    4242  /// the distance measure.
    4343  ///
    44   struct vector_distance_tag {
     44  struct distance_tag {
    4545    /// tag defining distance measure
    46     typedef vector_distance_tag distance;
     46    typedef distance_tag distance;
    4747  };
    4848
     
    5454  /// distance measures.  Instead it can access the "label" of each
    5555  /// distance measure through the special template structure
    56   /// vector_distance_traits.
     56  /// distance_traits.
    5757  ///
    5858  template <class T>
    59   struct vector_distance_traits {
     59  struct distance_traits {
    6060    /// \return distance type
    6161    typedef typename T::distance distance;
     
    7373  ///
    7474  template <typename Iter1, typename Iter2, typename Dist>
    75   double vector_distance(Iter1 beg1, Iter1 end1, Iter2 beg2, const Dist disttag)
     75  double distance(Iter1 beg1, Iter1 end1, Iter2 beg2, const Dist disttag)
    7676  {
    7777    return
    78       vector_distance(beg1,end1,beg2,
    79                       typename vector_distance_traits<Dist>::distance(),
     78      distance(beg1,end1,beg2,
     79                      typename distance_traits<Dist>::distance(),
    8080                      typename utility::weighted_if_any2<Iter1, Iter2>::type());
    8181  }       
  • trunk/yat/statistics/euclidean_distance.h

    r1030 r1031  
    1 #ifndef theplu_yat_statistics_euclidean_vector_distance_h
    2 #define theplu_yat_statistics_euclidean_vector_distance_h
     1#ifndef theplu_yat_statistics_euclidean_distance_h
     2#define theplu_yat_statistics_euclidean_distance_h
    33
    44// $Id$
     
    2727#include "AveragerPair.h"
    2828#include "AveragerPairWeighted.h"
    29 #include "vector_distance.h"
     29#include "distance.h"
    3030#include "yat/utility/iterator_traits.h"
    3131
     
    4141  /// the Euclidean distance measure.
    4242  ///
    43   struct euclidean_vector_distance_tag
    44     : public vector_distance_tag
     43  struct euclidean_distance_tag
     44    : public distance_tag
    4545  {
    4646    /// \brief tag for euclidean distance
    47     typedef euclidean_vector_distance_tag distance;
     47    typedef euclidean_distance_tag distance;
    4848  };
    4949
     
    5555  ///
    5656  template <typename Iter1, typename Iter2>
    57   double vector_distance(Iter1 beg1,Iter1 end1, Iter2 beg2,
    58                          const euclidean_vector_distance_tag& disttype,
     57  double distance(Iter1 beg1,Iter1 end1, Iter2 beg2,
     58                         const euclidean_distance_tag& disttype,
    5959                         utility::unweighted_type)
    6060  {
     
    7171  ///
    7272  template <typename Iter1, typename Iter2>
    73   double vector_distance(Iter1 beg1, Iter1 end1, Iter2 beg2,
    74                          const euclidean_vector_distance_tag& disttype,
     73  double distance(Iter1 beg1, Iter1 end1, Iter2 beg2,
     74                         const euclidean_distance_tag& disttype,
    7575                         utility::weighted_type)
    7676  {
  • trunk/yat/statistics/pearson_distance.h

    r1030 r1031  
    1 #ifndef theplu_yat_statistics_pearson_vector_distance_h
    2 #define theplu_yat_statistics_pearson_vector_distance_h
     1#ifndef theplu_yat_statistics_pearson_distance_h
     2#define theplu_yat_statistics_pearson_distance_h
    33
    44// $Id$
     
    2525*/
    2626
    27 #include "vector_distance.h"
     27#include "distance.h"
    2828
    2929#include "AveragerPair.h"
     
    4040  /// the Pearson distance measure.
    4141  ///
    42   struct pearson_vector_distance_tag
    43     : public vector_distance_tag
     42  struct pearson_distance_tag
     43    : public distance_tag
    4444  {
    4545    /// \brief tag for pearson distance
    46     typedef pearson_vector_distance_tag distance;
     46    typedef pearson_distance_tag distance;
    4747  };
    4848
     
    5454  ///
    5555  template <class Iter>
    56   double vector_distance(Iter beg1,Iter end1, Iter beg2,
    57                          const pearson_vector_distance_tag& disttype,
     56  double distance(Iter beg1,Iter end1, Iter beg2,
     57                         const pearson_distance_tag& disttype,
    5858                         utility::unweighted_type)
    5959  {
     
    6969  ///
    7070  template <class Iter>
    71   double vector_distance(Iter beg1,Iter end1, Iter beg2,
    72                          const pearson_vector_distance_tag& disttype,
     71  double distance(Iter beg1,Iter end1, Iter beg2,
     72                         const pearson_distance_tag& disttype,
    7373                         utility::weighted_type)
    7474  {
Note: See TracChangeset for help on using the changeset viewer.