1 | #ifndef _theplu_yat_statistics_kolmogorov_smirnov_ |
---|
2 | #define _theplu_yat_statistics_kolmogorov_smirnov_ |
---|
3 | |
---|
4 | // $Id: KolmogorovSmirnov.h 1595 2008-10-22 21:58:26Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include <set> |
---|
26 | #include <vector> |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace statistics { |
---|
31 | |
---|
32 | /** |
---|
33 | \brief Kolmogow Smirnov Test |
---|
34 | */ |
---|
35 | class KolmogorovSmirnov |
---|
36 | { |
---|
37 | public: |
---|
38 | /** |
---|
39 | \brief Constructor |
---|
40 | */ |
---|
41 | KolmogorovSmirnov(void); |
---|
42 | |
---|
43 | /** |
---|
44 | \brief add a value |
---|
45 | */ |
---|
46 | void add(double value, bool class_label, double weight=1.0); |
---|
47 | |
---|
48 | /** |
---|
49 | \brief Large-Sample Approximation |
---|
50 | |
---|
51 | This analytical approximation of p-value can be used when all |
---|
52 | weight equal unity and sample sizes \a n and \a m are |
---|
53 | large. The p-value is calcuated as \f$ P = \displaystyle - 2 |
---|
54 | \sum_{k=1}^{\infty} (-1)^ke^{-2k^2s^2}\f$, where s is the |
---|
55 | scaled score: |
---|
56 | |
---|
57 | \f$ s = \sqrt\frac{nm}{n+m} \f$ score(). |
---|
58 | |
---|
59 | \since New in yat 0.5 |
---|
60 | |
---|
61 | Following Hollander and Wolfe |
---|
62 | */ |
---|
63 | double p_value(void) const; |
---|
64 | |
---|
65 | /** |
---|
66 | \brief p-value |
---|
67 | |
---|
68 | Performs a permutation test using \a perm label randomizations |
---|
69 | and calculate how often a score equal or larger than score() is |
---|
70 | obtained. |
---|
71 | */ |
---|
72 | double p_value(size_t perm) const; |
---|
73 | |
---|
74 | /** |
---|
75 | \brief resets everything to zero |
---|
76 | */ |
---|
77 | void reset(void); |
---|
78 | |
---|
79 | /** |
---|
80 | \brief Kolmogorov Smirnov statistic |
---|
81 | |
---|
82 | \f$ sup_x | F_1(x) - F_2(x) | \f$ where |
---|
83 | \f$ F(x) = \frac {\sum_{i:x_i\le x}w_i }{ \sum w_i }\f$ |
---|
84 | */ |
---|
85 | double score(void) const; |
---|
86 | |
---|
87 | /** |
---|
88 | Same as score() but keeping the sign, in other words, |
---|
89 | abs(signed_score())==score() |
---|
90 | |
---|
91 | \since New in yat 0.5 |
---|
92 | */ |
---|
93 | double signed_score(void) const; |
---|
94 | |
---|
95 | private: |
---|
96 | void scores(std::vector<double>&) const; |
---|
97 | |
---|
98 | mutable bool cached_; |
---|
99 | mutable double score_; |
---|
100 | typedef std::pair<std::pair<double,double>, bool> trip; |
---|
101 | typedef std::set<trip, std::greater<trip> > data_w; |
---|
102 | data_w data_; |
---|
103 | double sum_w1_; |
---|
104 | double sum_w2_; |
---|
105 | |
---|
106 | friend std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&); |
---|
107 | |
---|
108 | // Copy not allowed |
---|
109 | KolmogorovSmirnov(const KolmogorovSmirnov&); |
---|
110 | KolmogorovSmirnov& operator=(const KolmogorovSmirnov&); |
---|
111 | }; |
---|
112 | |
---|
113 | /** |
---|
114 | \brief output operator |
---|
115 | */ |
---|
116 | std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&); |
---|
117 | |
---|
118 | |
---|
119 | }}} // of namespace theplu yat statistics |
---|
120 | |
---|
121 | #endif |
---|