yat  0.13.2pre
KolmogorovSmirnov.h
1 #ifndef _theplu_yat_statistics_kolmogorov_smirnov_
2 #define _theplu_yat_statistics_kolmogorov_smirnov_
3 
4 // $Id: KolmogorovSmirnov.h 3295 2014-07-25 04:24:39Z peter $
5 
6 /*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 2012, 2013 Peter Johansson
9 
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11 
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16 
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #include <boost/concept_check.hpp>
27 
28 #include <iosfwd>
29 #include <set>
30 #include <vector>
31 
32 namespace theplu {
33 namespace yat {
34 namespace statistics {
35 
40  {
41  public:
50  struct Element
51  {
55  Element(void);
56 
60  Element(double x, bool class_label, double w=1.0);
61 
65  double value;
66 
70  bool label;
71 
75  double weight;
76 
80  bool operator<(const Element& rhs) const;
81  };
82 
86  KolmogorovSmirnov(void);
87 
91  void add(double value, bool class_label, double weight=1.0);
92 
106  template <typename ForwardIterator>
107  void add(ForwardIterator first, ForwardIterator last);
108 
124  double p_value(void) const;
125 
135  double p_value(size_t perm) const;
136 
145  void remove(double value, bool class_label, double weight=1.0);
146 
150  void reset(void);
151 
158  double score(void) const;
159 
168  void shuffle(void);
169 
179  double signed_score(void) const;
180 
181  private:
182  void scores(std::vector<double>&) const;
183  // add weights to sum_w1 and sum_w2 respectively depending on
184  // label in element.
185  template <typename ForwardIterator>
186  void add_sum_w(ForwardIterator first, ForwardIterator last);
187 
188  mutable bool cached_;
189  mutable double score_;
190  typedef std::multiset<Element> data_w;
191  data_w data_;
192  double sum_w1_;
193  double sum_w2_;
194 
195  friend std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
196 
197  // using compiler generated copy and assignment
198  //KolmogorovSmirnov(const KolmogorovSmirnov&);
199  //KolmogorovSmirnov& operator=(const KolmogorovSmirnov&);
200  };
201 
207  std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
208 
209 
210  // template implementations
211 
212  template <typename ForwardIterator>
213  void KolmogorovSmirnov::add(ForwardIterator first, ForwardIterator last)
214  {
215  BOOST_CONCEPT_ASSERT((boost::ForwardIterator<ForwardIterator>));
216  ForwardIterator iter(first);
217  typename data_w::const_iterator hint(data_.begin());
218  for ( ; iter!=last; ++iter)
219  if ((*iter).weight) // ignore data points with zero weight
220  hint = data_.insert(hint, *iter);
221  add_sum_w(first, last);
222  cached_=false;
223  }
224 
225 
226  template <typename ForwardIterator>
227  void KolmogorovSmirnov::add_sum_w(ForwardIterator first,
228  ForwardIterator last)
229  {
230  while (first!=last) {
231  if ((*first).label)
232  sum_w1_ += (*first).weight;
233  else
234  sum_w2_ += (*first).weight;
235  ++first;
236  }
237  }
238 
239 }}} // of namespace theplu yat statistics
240 
241 #endif
Definition: KolmogorovSmirnov.h:50
void shuffle(void)
shuffle class labels
bool label
Definition: KolmogorovSmirnov.h:70
Kolmogorov Smirnov Test.
Definition: KolmogorovSmirnov.h:39
void add(double value, bool class_label, double weight=1.0)
add a value
double weight
Definition: KolmogorovSmirnov.h:75
void reset(void)
resets everything to zero
double value
data value
Definition: KolmogorovSmirnov.h:65
double score(void) const
Kolmogorov Smirnov statistic.
double p_value(void) const
Large-Sample Approximation.

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5