Changeset 890


Ignore:
Timestamp:
Sep 25, 2007, 11:35:25 AM (16 years ago)
Author:
Markus Ringnér
Message:

Refs #245 #246 #247

Location:
trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/vector_distance_test.cc

    r889 r890  
    11// $Id$
    22
     3#include "yat/classifier/DataLookupWeighted1D.h"
     4#include "yat/classifier/MatrixLookupWeighted.h"
    35#include "yat/statistics/euclidean_vector_distance.h"
    46#include "yat/statistics/pearson_vector_distance.h"
     7#include "yat/utility/matrix.h"
    58#include "yat/utility/vector.h"
    69
     
    2326      std::cout << "vector_distance_test -v : for printing extra information\n";
    2427  }
    25   *error << "testing distance" << std::endl;
     28  *error << "testing vector_distance" << std::endl;
    2629  bool ok = true;
    2730
     
    3134  b(2) = 1;
    3235
    33   *error << "testing Euclidean vector_distance" << std::endl;
     36  double tolerance=1e-4;
     37
    3438  double dist=statistics::vector_distance(a.begin(),a.end(),b.begin(),
    3539                                          statistics::euclidean_vector_distance_tag());
     40  if(fabs(dist-2.23607)>tolerance) {
     41    *error << "Error in unweighted Euclidean vector_distance: " << std::endl;
     42    ok=false;
     43  }
    3644
    37   *error << "Dist: " << dist << std::endl;
    38 
    39 
    40   *error << "testing Pearson vector_distance" << std::endl;
    4145  dist=statistics::vector_distance(a.begin(),a.end(),b.begin(),
    4246                                          statistics::pearson_vector_distance_tag());
    43   *error << "Dist: " << dist << std::endl;
     47  if(fabs(dist-1.5)>tolerance) {
     48    *error << "Error in unweighted Pearson vector_distance: " << std::endl;
     49    ok=false;
     50  }
    4451
     52 
     53  // Testing weighted versions
     54  utility::matrix m(2,3,1);
     55  m(0,1)=2;
     56  m(1,0)=0;
     57  m(1,1)=0;
     58  utility::matrix w(2,3,1);
     59  w(0,0)=0;
     60  classifier::MatrixLookupWeighted mw(m,w);
     61  classifier::DataLookupWeighted1D aw(mw,0,true);
     62  classifier::DataLookupWeighted1D bw(mw,1,true);
     63 
     64  dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
     65                                          statistics::euclidean_vector_distance_tag());
     66 
     67  if(fabs(dist-2)>tolerance) {
     68    *error << "Error in weighted Euclidean vector_distance: " << std::endl;
     69    ok=false;
     70  }
     71
     72  dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
     73                                          statistics::pearson_vector_distance_tag());
     74 
     75  if(fabs(dist-2)>tolerance) {
     76    *error << "Error in weighted Pearson vector_distance: " << std::endl;
     77    ok=false;
     78  }
    4579
    4680  if (error!=&std::cerr)
  • trunk/yat/classifier/DataLookupWeighted1D.cc

    r865 r890  
    6767
    6868
     69  DataLookupWeighted1D::const_iterator DataLookupWeighted1D::begin(void) const
     70  {
     71    return DataLookupWeighted1D::const_iterator(*this, 0);
     72  }
     73
     74
    6975  double DataLookupWeighted1D::data(const size_t i) const
    7076  {
    7177    return column_vector_? matrix_->data(i,index_) : matrix_->data(index_,i);
    7278  }
     79
     80
     81  DataLookupWeighted1D::const_iterator DataLookupWeighted1D::end(void) const
     82  {
     83    return DataLookupWeighted1D::const_iterator(*this, size());
     84  }
     85
    7386
    7487
  • trunk/yat/classifier/DataLookupWeighted1D.h

    r865 r890  
    2626*/
    2727
     28#include "yat/utility/IteratorWeighted.h"
     29
    2830#include <iostream>
    2931#include <vector>
     
    4446 
    4547  public:
     48   
     49    typedef utility::IteratorWeighted<const double, const classifier::DataLookupWeighted1D>
     50    const_iterator;
    4651
    4752    ///
     
    7277
    7378    /**
     79     */
     80    const_iterator begin() const;
     81
     82    /**
    7483       \return data(i)
    7584    */
    7685    double data(const size_t i) const;
     86   
     87    /**
     88     */
     89    const_iterator end() const;
    7790
    7891    ///
  • trunk/yat/statistics/AveragerPairWeighted.cc

    r865 r890  
    9696  double AveragerPairWeighted::msd(void) const
    9797  {
    98     return ( x_.sum_wxx()+y_.sum_wxx()-2*wxy_)/w_;
     98    return sum_squared_deviation()/w_;
    9999  }
    100100
     
    104104    x_.reset(); y_.reset(); wxy_=0; w_=0;
    105105  }
     106
     107  double AveragerPairWeighted::sum_squared_deviation(void) const
     108  {
     109    return x_.sum_wxx()+y_.sum_wxx()-2*wxy_;
     110  }
     111 
    106112
    107113  double AveragerPairWeighted::sum_w(void) const
  • trunk/yat/statistics/AveragerPairWeighted.h

    r865 r890  
    133133
    134134    ///
     135    /// @return Sum of weighted squared deviation between x and y \f$
     136    /// \sum (w_xx-wyy)^2 \f$
     137    ///
     138    double sum_squared_deviation(void) const;
     139
     140    ///
    135141    /// @return \f$ \sum w_xw_y \f$
    136142    ///
     
    169175
    170176  };
     177
     178  /**
     179     \brief adding a ranges of values to AveragerPairWeighted \a ap
     180  */
     181  template <class Iter1, class Iter2>
     182  void add(AveragerPairWeighted& ap, Iter1 first1, Iter1 last1, Iter2 first2)
     183  {
     184    for ( ; first1 != last1; ++first1, ++first2)
     185      ap.add(first1.data(), first2.data(),first1.weight(),first2.weight());
     186  }
     187
     188
     189
    171190
    172191  template <typename T1,typename T2,typename T3,typename T4>
  • trunk/yat/statistics/euclidean_vector_distance.h

    r889 r890  
    55
    66#include "AveragerPair.h"
     7#include "AveragerPairWeighted.h"
    78#include "vector_distance.h"
    89
     
    4041  }
    4142 
     43
     44  ///
     45  /// implementation for distances between vectors
     46  /// (containers with random access iterators) using a Euclidean
     47  /// distance measure and iterators to weighted containers.
     48  ///
     49  template <class Iter>
     50  double vector_distance(Iter beg1,Iter end1, Iter beg2,
     51                         const euclidean_vector_distance_tag& disttype,
     52                         std::weighted_random_access_iterator_tag)
     53  {
     54    AveragerPairWeighted ap;
     55    add(ap,beg1,end1,beg2);
     56    return sqrt(ap.sum_squared_deviation());
     57  }
     58
    4259 
    4360}}} // of namespace statistics, yat, and theplu
  • trunk/yat/statistics/pearson_vector_distance.h

    r889 r890  
    3939  }
    4040 
     41  ///
     42  /// implementation for distances between vectors
     43  /// (containers with random access iterators) using a Pearson
     44  /// distance measure and iterators to unweighted containers.
     45  ///
     46  template <class Iter>
     47  double vector_distance(Iter beg1,Iter end1, Iter beg2,
     48                         const pearson_vector_distance_tag& disttype,
     49                         std::weighted_random_access_iterator_tag)
     50  {
     51    AveragerPairWeighted ap;
     52    add(ap,beg1,end1,beg2);
     53    return 1-ap.correlation();
     54  }
     55
    4156 
    4257}}} // of namespace statistics, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.