Changeset 890
- Timestamp:
- Sep 25, 2007, 11:35:25 AM (16 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/vector_distance_test.cc
r889 r890 1 1 // $Id$ 2 2 3 #include "yat/classifier/DataLookupWeighted1D.h" 4 #include "yat/classifier/MatrixLookupWeighted.h" 3 5 #include "yat/statistics/euclidean_vector_distance.h" 4 6 #include "yat/statistics/pearson_vector_distance.h" 7 #include "yat/utility/matrix.h" 5 8 #include "yat/utility/vector.h" 6 9 … … 23 26 std::cout << "vector_distance_test -v : for printing extra information\n"; 24 27 } 25 *error << "testing distance" << std::endl;28 *error << "testing vector_distance" << std::endl; 26 29 bool ok = true; 27 30 … … 31 34 b(2) = 1; 32 35 33 *error << "testing Euclidean vector_distance" << std::endl; 36 double tolerance=1e-4; 37 34 38 double dist=statistics::vector_distance(a.begin(),a.end(),b.begin(), 35 39 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 } 36 44 37 *error << "Dist: " << dist << std::endl;38 39 40 *error << "testing Pearson vector_distance" << std::endl;41 45 dist=statistics::vector_distance(a.begin(),a.end(),b.begin(), 42 46 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 } 44 51 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 } 45 79 46 80 if (error!=&std::cerr) -
trunk/yat/classifier/DataLookupWeighted1D.cc
r865 r890 67 67 68 68 69 DataLookupWeighted1D::const_iterator DataLookupWeighted1D::begin(void) const 70 { 71 return DataLookupWeighted1D::const_iterator(*this, 0); 72 } 73 74 69 75 double DataLookupWeighted1D::data(const size_t i) const 70 76 { 71 77 return column_vector_? matrix_->data(i,index_) : matrix_->data(index_,i); 72 78 } 79 80 81 DataLookupWeighted1D::const_iterator DataLookupWeighted1D::end(void) const 82 { 83 return DataLookupWeighted1D::const_iterator(*this, size()); 84 } 85 73 86 74 87 -
trunk/yat/classifier/DataLookupWeighted1D.h
r865 r890 26 26 */ 27 27 28 #include "yat/utility/IteratorWeighted.h" 29 28 30 #include <iostream> 29 31 #include <vector> … … 44 46 45 47 public: 48 49 typedef utility::IteratorWeighted<const double, const classifier::DataLookupWeighted1D> 50 const_iterator; 46 51 47 52 /// … … 72 77 73 78 /** 79 */ 80 const_iterator begin() const; 81 82 /** 74 83 \return data(i) 75 84 */ 76 85 double data(const size_t i) const; 86 87 /** 88 */ 89 const_iterator end() const; 77 90 78 91 /// -
trunk/yat/statistics/AveragerPairWeighted.cc
r865 r890 96 96 double AveragerPairWeighted::msd(void) const 97 97 { 98 return ( x_.sum_wxx()+y_.sum_wxx()-2*wxy_)/w_;98 return sum_squared_deviation()/w_; 99 99 } 100 100 … … 104 104 x_.reset(); y_.reset(); wxy_=0; w_=0; 105 105 } 106 107 double AveragerPairWeighted::sum_squared_deviation(void) const 108 { 109 return x_.sum_wxx()+y_.sum_wxx()-2*wxy_; 110 } 111 106 112 107 113 double AveragerPairWeighted::sum_w(void) const -
trunk/yat/statistics/AveragerPairWeighted.h
r865 r890 133 133 134 134 /// 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 /// 135 141 /// @return \f$ \sum w_xw_y \f$ 136 142 /// … … 169 175 170 176 }; 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 171 190 172 191 template <typename T1,typename T2,typename T3,typename T4> -
trunk/yat/statistics/euclidean_vector_distance.h
r889 r890 5 5 6 6 #include "AveragerPair.h" 7 #include "AveragerPairWeighted.h" 7 8 #include "vector_distance.h" 8 9 … … 40 41 } 41 42 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 42 59 43 60 }}} // of namespace statistics, yat, and theplu -
trunk/yat/statistics/pearson_vector_distance.h
r889 r890 39 39 } 40 40 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 41 56 42 57 }}} // of namespace statistics, yat, and theplu
Note: See TracChangeset
for help on using the changeset viewer.