yat  0.21pre
KolmogorovSmirnov.h
1 #ifndef _theplu_yat_statistics_kolmogorov_smirnov_
2 #define _theplu_yat_statistics_kolmogorov_smirnov_
3 
4 // $Id: KolmogorovSmirnov.h 4260 2022-12-21 02:03:20Z peter $
5 
6 /*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2016 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 "yat/utility/concept_check.h"
27 
28 #include <boost/concept_check.hpp>
29 #include <boost/iterator/iterator_concepts.hpp>
30 
31 #include <iosfwd>
32 #include <set>
33 #include <vector>
34 
35 namespace theplu {
36 namespace yat {
37 namespace statistics {
38 
43  {
44  public:
53  struct Element
54  {
58  Element(void);
59 
63  Element(double x, bool class_label, double w=1.0);
64 
68  double value;
69 
73  bool label;
74 
78  double weight;
79 
83  bool operator<(const Element& rhs) const;
84  };
85 
89  KolmogorovSmirnov(void);
90 
94  void add(double value, bool class_label, double weight=1.0);
95 
112  template <typename ForwardIterator>
113  void add(ForwardIterator first, ForwardIterator last);
114 
130  double p_value(void) const;
131 
141  double p_value(size_t perm) const;
142 
151  void remove(double value, bool class_label, double weight=1.0);
152 
156  void reset(void);
157 
168  double score(void) const;
169 
182  // \f$ F_{\textrm{True}}(x) - F_{\textrm{False}}(x) | \f$ where
183  // \f$ F(x) = \frac{\sum_{i:x_i\le x}w_i}{ \sum w_i} \f$
184  void scores(std::vector<double>&) const;
185 
194  void shuffle(void);
195 
210  double signed_score(void) const;
211 
212  private:
213  // add weights to sum_w1 and sum_w2 respectively depending on
214  // label in element.
215  template <typename ForwardIterator>
216  void add_sum_w(ForwardIterator first, ForwardIterator last);
217 
218  mutable bool cached_;
219  mutable double score_;
220  typedef std::multiset<Element> data_w;
221  data_w data_;
222  double sum_w1_;
223  double sum_w2_;
224 
225  // using compiler generated copy and assignment
226  //KolmogorovSmirnov(const KolmogorovSmirnov&);
227  //KolmogorovSmirnov& operator=(const KolmogorovSmirnov&);
228  };
229 
235  std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
236 
237 
238  // template implementations
239 
240  template <typename ForwardIterator>
241  void KolmogorovSmirnov::add(ForwardIterator first, ForwardIterator last)
242  {
243  BOOST_CONCEPT_ASSERT((boost_concepts::ForwardTraversal<ForwardIterator>));
244  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<ForwardIterator>));
245  ForwardIterator iter(first);
246  typename data_w::const_iterator hint(data_.begin());
247  for ( ; iter!=last; ++iter)
248  if ((*iter).weight) // ignore data points with zero weight
249  hint = data_.insert(hint, *iter);
250  add_sum_w(first, last);
251  cached_=false;
252  }
253 
254 
255  template <typename ForwardIterator>
256  void KolmogorovSmirnov::add_sum_w(ForwardIterator first,
257  ForwardIterator last)
258  {
259  while (first!=last) {
260  if ((*first).label)
261  sum_w1_ += (*first).weight;
262  else
263  sum_w2_ += (*first).weight;
264  ++first;
265  }
266  }
267 
268 }}} // of namespace theplu yat statistics
269 
270 #endif
double score(void) const
Kolmogorov Smirnov statistic.
Definition: KolmogorovSmirnov.h:53
void shuffle(void)
shuffle class labels
bool label
Definition: KolmogorovSmirnov.h:73
The Department of Theoretical Physics namespace as we define it.
Kolmogorov Smirnov Test.
Definition: KolmogorovSmirnov.h:42
void add(double value, bool class_label, double weight=1.0)
add a value
double weight
Definition: KolmogorovSmirnov.h:78
void reset(void)
resets everything to zero
double p_value(void) const
Large-Sample Approximation.
void scores(std::vector< double > &) const
double value
data value
Definition: KolmogorovSmirnov.h:68

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14