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 <set>
00027 #include <vector>
00028
00029 namespace theplu {
00030 namespace yat {
00031 namespace statistics {
00032
00036 class KolmogorovSmirnov
00037 {
00038 public:
00047 struct Element
00048 {
00052 inline Element(void) {};
00053
00057 inline Element(double x, bool class_label, double w=1.0)
00058 : value(x), label(class_label), weight(w) {};
00059
00063 double value;
00064
00068 bool label;
00069
00073 double weight;
00074
00078 bool operator<(const Element rhs) const;
00079 };
00080
00084 KolmogorovSmirnov(void);
00085
00089 void add(double value, bool class_label, double weight=1.0);
00090
00104 template <typename ForwardIterator>
00105 void add(ForwardIterator first, ForwardIterator last);
00106
00122 double p_value(void) const;
00123
00133 double p_value(size_t perm) const;
00134
00138 void reset(void);
00139
00146 double score(void) const;
00147
00156 void shuffle(void);
00157
00167 double signed_score(void) const;
00168
00169 private:
00170 void scores(std::vector<double>&) const;
00171
00172
00173 template <typename ForwardIterator>
00174 void add_sum_w(ForwardIterator first, ForwardIterator last);
00175
00176 mutable bool cached_;
00177 mutable double score_;
00178 typedef std::multiset<Element> data_w;
00179 data_w data_;
00180 double sum_w1_;
00181 double sum_w2_;
00182
00183 friend std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
00184
00185
00186
00187
00188 };
00189
00193 std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
00194
00195
00196
00197
00198 template <typename ForwardIterator>
00199 void KolmogorovSmirnov::add(ForwardIterator first, ForwardIterator last)
00200 {
00201 ForwardIterator iter(first);
00202 typename data_w::const_iterator hint(data_.begin());
00203 for ( ; iter!=last; ++iter)
00204 if (iter->weight)
00205 hint = data_.insert(hint, *iter);
00206 add_sum_w(first, last);
00207 cached_=false;
00208 }
00209
00210
00211 template <typename ForwardIterator>
00212 void KolmogorovSmirnov::add_sum_w(ForwardIterator first,
00213 ForwardIterator last)
00214 {
00215 while (first!=last) {
00216 if (first->label)
00217 sum_w1_ += first->weight;
00218 else
00219 sum_w2_ += first->weight;
00220 ++first;
00221 }
00222 }
00223
00224 }}}
00225
00226 #endif