Changeset 1139 for trunk/yat/statistics
 Timestamp:
 Feb 24, 2008, 2:59:27 AM (13 years ago)
 Location:
 trunk/yat/statistics
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/statistics/PearsonCorrelation.cc
r1049 r1139 57 57 } 58 58 59 double PearsonCorrelation::score(const classifier::Target& target,60 const utility::VectorBase& value)61 {62 nof_samples_ = value.size();63 Pearson pearson;64 return pearson.score(target,value);65 }66 67 double68 PearsonCorrelation::score(const classifier::Target& target,69 const classifier::DataLookupWeighted1D& value)70 {71 // Peter what should nof_samples be in weighted case?72 nof_samples_=0;73 Pearson pearson;74 return pearson.score(target,value);75 }76 77 double PearsonCorrelation::score(const classifier::Target& target,78 const utility::VectorBase& value,79 const utility::VectorBase& weight)80 {81 // Peter what should nof_samples be in weighted case?82 nof_samples_ = 0;83 Pearson pearson;84 return pearson.score(target,value);85 }86 87 59 }}} // of namespace statistics, yat, and theplu 
trunk/yat/statistics/PearsonCorrelation.h
r1125 r1139 7 7 Copyright (C) 2004, 2005 Peter Johansson 8 8 Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson 9 Copyright (C) 2007 Peter Johansson9 Copyright (C) 2007, 2008 Peter Johansson 10 10 11 11 This file is part of the yat library, http://trac.thep.lu.se/yat … … 27 27 */ 28 28 29 #include "Score.h" 29 #include "AveragerPair.h" 30 #include "AveragerPairWeighted.h" 31 #include "yat/classifier/Target.h" 32 #include "yat/utility/iterator_traits.h" 33 30 34 31 35 namespace theplu { … … 56 60 \f$ \frac{\vert \sum_i(x_i\bar{x})(y_i\bar{y})\vert 57 61 }{\sqrt{\sum_i (x_i\bar{x})^2\sum_i (x_i\bar{x})^2}} \f$. 62 63 64 If ForwardIterator is weighted correlation is calculated as 65 \f$ \frac{\vert \sum_iw^2_i(x_i\bar{x})(y_i\bar{y})\vert } 66 {\sqrt{\sum_iw^2_i(x_i\bar{x})^2\sum_iw^2_i(y_i\bar{y})^2}} 67 \f$, where \f$ m_x = \frac{\sum w_ix_i}{\sum w_i} \f$ and \f$ 68 m_x = \frac{\sum w_ix_i}{\sum w_i} \f$. This expression is 69 chosen to get a correlation equal to unity when \a x and \a y 70 are equal. 71 58 72 @return Pearson correlation, if absolute=true absolute value 59 73 of Pearson is used. 60 74 */ 75 template<typename ForwardIterator> 61 76 double score(const classifier::Target& target, 62 const utility::VectorBase& value);77 ForwardIterator first, ForwardIterator last); 63 78 64 79 /** … … 68 83 m_x = \frac{\sum w_ix_i}{\sum w_i} \f$. This expression is 69 84 chosen to get a correlation equal to unity when \a x and \a y 70 are equal. @return absolute value of weighted version of 71 Pearson correlation. 85 are equal. 86 87 \return absolute value of weighted version of Pearson 88 correlation. 89 90 \note ietartors must be nonweighted 72 91 */ 92 template<typename ForwardIterator1, typename ForwardIterator2> 73 93 double score(const classifier::Target& target, 74 const classifier::DataLookupWeighted1D& value); 75 76 /** 77 \f$ \frac{\vert \sum_iw^2_i(x_i\bar{x})(y_i\bar{y})\vert } 78 {\sqrt{\sum_iw^2_i(x_i\bar{x})^2\sum_iw^2_i(y_i\bar{y})^2}} 79 \f$, where \f$ m_x = \frac{\sum w_ix_i}{\sum w_i} \f$ and \f$ 80 m_x = \frac{\sum w_ix_i}{\sum w_i} \f$. This expression is 81 chosen to get a correlation equal to unity when \a x and \a y 82 are equal. @return absolute value of weighted version of 83 Pearson correlation. 84 */ 85 double score(const classifier::Target& target, 86 const utility::VectorBase& value, 87 const utility::VectorBase& weight); 94 ForwardIterator1 first1, ForwardIterator1 last1, 95 ForwardIterator2 first2); 88 96 89 97 /** … … 103 111 int nof_samples_; 104 112 113 template<typename ForwardIterator> 114 double score(const classifier::Target& target, 115 ForwardIterator first, ForwardIterator last, 116 utility::unweighted_iterator_tag); 117 118 template<typename ForwardIterator> 119 double score(const classifier::Target& target, 120 ForwardIterator first, ForwardIterator last, 121 utility::weighted_iterator_tag); 122 105 123 }; 124 125 template<typename ForwardIterator> 126 double PearsonCorrelation::score(const classifier::Target& target, 127 ForwardIterator first, 128 ForwardIterator last) 129 { 130 nof_samples_ = target.size(); 131 using utility::yat_assert; 132 yat_assert<std::runtime_error>("PearsonCorrelation: sizes mismatch"); 133 r_ = score(target, first, last, 134 utility::iterator_traits<ForwardIterator>::type()); 135 return r_; 136 } 137 138 139 template<typename ForwardIterator> 140 double PearsonCorrelation::score(const classifier::Target& target, 141 ForwardIterator first, 142 ForwardIterator last, 143 utility::unweighted_iterator_tag tag) 144 145 { 146 AveragerPair ap; 147 for (size_t i=0; first!=last; ++first, ++i) 148 ap.add(target.binary(i), *first); 149 nof_samples_ = ap.n(); 150 return ap.correlation(); 151 } 152 153 154 template<typename ForwardIterator> 155 double PearsonCorrelation::score(const classifier::Target& target, 156 ForwardIterator first, 157 ForwardIterator last, 158 utility::weighted_iterator_tag tag) 159 160 { 161 AveragerPairWeighted ap; 162 for (size_t i=0; first!=last; ++first, ++i) 163 ap.add(target.binary(i), first.data(), 1.0, first.weight()); 164 nof_samples_ = ap.n(); 165 return ap.correlation(); 166 } 167 168 template<typename ForwardIterator1, typename ForwardIterator2> 169 double PearsonCorrelation::score(const classifier::Target& target, 170 ForwardIterator1 first1, 171 ForwardIterator1 last1, 172 ForwardIterator2 first2) 173 { 174 utility::check_iterator_is_unweighted(first1); 175 utility::check_iterator_is_unweighted(first2); 176 AveragerPairWeighted ap; 177 for (size_t i=0; first1!=last1; ++first1, ++i, ++first2) 178 ap.add(target.binary(i), *first1, 1.0, *first2); 179 nof_samples_ = ap.n(); 180 r_ = ap.correlation(); 181 } 106 182 107 183 }}} // of namespace statistics, yat, and theplu
Note: See TracChangeset
for help on using the changeset viewer.