00001 #ifndef _theplu_yat_statistics_kolmogorov_smirnov_
00002 #define _theplu_yat_statistics_kolmogorov_smirnov_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 inline Element(void) {};
00056
00060 inline Element(double x, bool class_label, double w=1.0)
00061 : value(x), label(class_label), weight(w) {};
00062
00066 double value;
00067
00071 bool label;
00072
00076 double weight;
00077
00081 bool operator<(const Element& rhs) const;
00082 };
00083
00087 KolmogorovSmirnov(void);
00088
00092 void add(double value, bool class_label, double weight=1.0);
00093
00107 template <typename ForwardIterator>
00108 void add(ForwardIterator first, ForwardIterator last);
00109
00125 double p_value(void) const;
00126
00136 double p_value(size_t perm) const;
00137
00141 void reset(void);
00142
00149 double score(void) const;
00150
00159 void shuffle(void);
00160
00170 double signed_score(void) const;
00171
00172 private:
00173 void scores(std::vector<double>&) const;
00174
00175
00176 template <typename ForwardIterator>
00177 void add_sum_w(ForwardIterator first, ForwardIterator last);
00178
00179 mutable bool cached_;
00180 mutable double score_;
00181 typedef std::multiset<Element> data_w;
00182 data_w data_;
00183 double sum_w1_;
00184 double sum_w2_;
00185
00186 friend std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
00187
00188
00189
00190
00191 };
00192
00198 std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
00199
00200
00201
00202
00203 template <typename ForwardIterator>
00204 void KolmogorovSmirnov::add(ForwardIterator first, ForwardIterator last)
00205 {
00206 BOOST_CONCEPT_ASSERT((boost::ForwardIterator<ForwardIterator>));
00207 typedef typename std::iterator_traits<ForwardIterator>::reference ref;
00208 BOOST_CONCEPT_ASSERT((boost::Convertible<ref, KolmogorovSmirnov::Element>));
00209 ForwardIterator iter(first);
00210 typename data_w::const_iterator hint(data_.begin());
00211 for ( ; iter!=last; ++iter)
00212 if ((*iter).weight)
00213 hint = data_.insert(hint, *iter);
00214 add_sum_w(first, last);
00215 cached_=false;
00216 }
00217
00218
00219 template <typename ForwardIterator>
00220 void KolmogorovSmirnov::add_sum_w(ForwardIterator first,
00221 ForwardIterator last)
00222 {
00223 while (first!=last) {
00224 if ((*first).label)
00225 sum_w1_ += (*first).weight;
00226 else
00227 sum_w2_ += (*first).weight;
00228 ++first;
00229 }
00230 }
00231
00232 }}}
00233
00234 #endif