yat
0.8.3pre
|
00001 #ifndef _theplu_yat_statistics_kolmogorov_smirnov_ 00002 #define _theplu_yat_statistics_kolmogorov_smirnov_ 00003 00004 // $Id: KolmogorovSmirnov.h 2526 2011-07-25 02:03:35Z peter $ 00005 00006 /* 00007 Copyright (C) 2008 Jari Häkkinen, Peter Johansson 00008 Copyright (C) 2009, 2010, 2011 Peter Johansson 00009 00010 This file is part of the yat library, http://dev.thep.lu.se/yat 00011 00012 The yat library is free software; you can redistribute it and/or 00013 modify it under the terms of the GNU General Public License as 00014 published by the Free Software Foundation; either version 3 of the 00015 License, or (at your option) any later version. 00016 00017 The yat library is distributed in the hope that it will be useful, 00018 but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 General Public License for more details. 00021 00022 You should have received a copy of the GNU General Public License 00023 along with yat. If not, see <http://www.gnu.org/licenses/>. 00024 */ 00025 00026 #include <boost/concept_check.hpp> 00027 00028 #include <iosfwd> 00029 #include <set> 00030 #include <vector> 00031 00032 namespace theplu { 00033 namespace yat { 00034 namespace statistics { 00035 00039 class KolmogorovSmirnov 00040 { 00041 public: 00050 struct Element 00051 { 00055 Element(void); 00056 00060 Element(double x, bool class_label, double w=1.0); 00061 00065 double value; 00066 00070 bool label; 00071 00075 double weight; 00076 00080 bool operator<(const Element& rhs) const; 00081 }; 00082 00086 KolmogorovSmirnov(void); 00087 00091 void add(double value, bool class_label, double weight=1.0); 00092 00106 template <typename ForwardIterator> 00107 void add(ForwardIterator first, ForwardIterator last); 00108 00124 double p_value(void) const; 00125 00135 double p_value(size_t perm) const; 00136 00140 void reset(void); 00141 00148 double score(void) const; 00149 00158 void shuffle(void); 00159 00169 double signed_score(void) const; 00170 00171 private: 00172 void scores(std::vector<double>&) const; 00173 // add weights to sum_w1 and sum_w2 respectively depending on 00174 // label in element. 00175 template <typename ForwardIterator> 00176 void add_sum_w(ForwardIterator first, ForwardIterator last); 00177 00178 mutable bool cached_; 00179 mutable double score_; 00180 typedef std::multiset<Element> data_w; 00181 data_w data_; 00182 double sum_w1_; 00183 double sum_w2_; 00184 00185 friend std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&); 00186 00187 // using compiler generated copy and assignment 00188 //KolmogorovSmirnov(const KolmogorovSmirnov&); 00189 //KolmogorovSmirnov& operator=(const KolmogorovSmirnov&); 00190 }; 00191 00197 std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&); 00198 00199 00200 // template implementations 00201 00202 template <typename ForwardIterator> 00203 void KolmogorovSmirnov::add(ForwardIterator first, ForwardIterator last) 00204 { 00205 BOOST_CONCEPT_ASSERT((boost::ForwardIterator<ForwardIterator>)); 00206 typedef typename std::iterator_traits<ForwardIterator>::reference ref; 00207 BOOST_CONCEPT_ASSERT((boost::Convertible<ref, KolmogorovSmirnov::Element>)); 00208 ForwardIterator iter(first); 00209 typename data_w::const_iterator hint(data_.begin()); 00210 for ( ; iter!=last; ++iter) 00211 if ((*iter).weight) // ignore data points with zero weight 00212 hint = data_.insert(hint, *iter); 00213 add_sum_w(first, last); 00214 cached_=false; 00215 } 00216 00217 00218 template <typename ForwardIterator> 00219 void KolmogorovSmirnov::add_sum_w(ForwardIterator first, 00220 ForwardIterator last) 00221 { 00222 while (first!=last) { 00223 if ((*first).label) 00224 sum_w1_ += (*first).weight; 00225 else 00226 sum_w2_ += (*first).weight; 00227 ++first; 00228 } 00229 } 00230 00231 }}} // of namespace theplu yat statistics 00232 00233 #endif