1 | #ifndef theplu_yat_statistics_kolmogorov_smirnov_one_sample |
---|
2 | #define theplu_yat_statistics_kolmogorov_smirnov_one_sample |
---|
3 | |
---|
4 | // $Id: KolmogorovSmirnovOneSample.h 2998 2013-03-14 08:08:54Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2013 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 <iosfwd> |
---|
26 | |
---|
27 | #include <map> |
---|
28 | |
---|
29 | namespace theplu { |
---|
30 | namespace yat { |
---|
31 | namespace statistics { |
---|
32 | |
---|
33 | /** |
---|
34 | \brief Kolmogorov Smirnov Test for one class |
---|
35 | |
---|
36 | Class can be used to test if data follows standard uniform |
---|
37 | distribution. Other distributions are not supported directly, but |
---|
38 | it is possible to check against other distribution by tranforming |
---|
39 | the data. If, for example, X is standard exponentially |
---|
40 | distributed log(X) is standard uniform. Therefore, rather than |
---|
41 | testing if X is exponential it is equivalent to test if log(X) is |
---|
42 | standard uniform. |
---|
43 | |
---|
44 | \since New in yat 0.11 |
---|
45 | */ |
---|
46 | class KolmogorovSmirnovOneSample |
---|
47 | { |
---|
48 | public: |
---|
49 | /** |
---|
50 | \brief Constructor |
---|
51 | */ |
---|
52 | KolmogorovSmirnovOneSample(void); |
---|
53 | |
---|
54 | /** |
---|
55 | \brief add a value |
---|
56 | |
---|
57 | \a value should be a value in range [0, 1]. |
---|
58 | */ |
---|
59 | void add(double value, double weight=1.0); |
---|
60 | |
---|
61 | /** |
---|
62 | \brief Large-Sample Approximation |
---|
63 | |
---|
64 | This analytical approximation of p-value can be used when all |
---|
65 | weight equal unity and sample size \a n is large. The p-value |
---|
66 | is calcuated as \f$ P = \displaystyle - 2 \sum_{k=1}^{\infty} |
---|
67 | (-1)^ke^{-2k^2s^2}\f$, where s is the scaled score: |
---|
68 | |
---|
69 | \f$ s = \sqrt n \f$ score(). |
---|
70 | */ |
---|
71 | double p_value(void) const; |
---|
72 | |
---|
73 | /** |
---|
74 | \brief Remove a data point |
---|
75 | |
---|
76 | \throw utility::runtime_error if no data point exist with \a |
---|
77 | value and \a weight. |
---|
78 | */ |
---|
79 | void remove(double value, double weight=1.0); |
---|
80 | |
---|
81 | /** |
---|
82 | \brief resets everything to zero |
---|
83 | */ |
---|
84 | void reset(void); |
---|
85 | |
---|
86 | /** |
---|
87 | \brief Kolmogorov Smirnov statistic |
---|
88 | |
---|
89 | \f$ sup_x | F(x) - x | \f$ where is the empirical cumulative |
---|
90 | distribution \f$ F(x) = \frac{\sum_{i:x_i\le x}w_i}{ \sum w_i} |
---|
91 | \f$ |
---|
92 | */ |
---|
93 | double score(void) const; |
---|
94 | |
---|
95 | /** |
---|
96 | Same as score() but keeping the sign, in other words, |
---|
97 | abs(signed_score())==score() |
---|
98 | |
---|
99 | A positive score implies that values \em on \em average are |
---|
100 | smaller 0.5. |
---|
101 | */ |
---|
102 | double signed_score(void) const; |
---|
103 | |
---|
104 | private: |
---|
105 | mutable double score_; |
---|
106 | mutable bool cached_; |
---|
107 | std::multimap<double, double> data_; |
---|
108 | double sum_w_; |
---|
109 | |
---|
110 | friend std::ostream& operator<<(std::ostream&, |
---|
111 | const KolmogorovSmirnovOneSample&); |
---|
112 | |
---|
113 | // using compiler generated copy and assignment |
---|
114 | //KolmogorovSmirnovOneSample(const KolmogorovSmirnovOneSample&); |
---|
115 | //KolmogorovSmirnovOneSample& operator=(const KolmogorovSmirnovOneSample&); |
---|
116 | }; |
---|
117 | |
---|
118 | }}} // of namespace theplu yat statistics |
---|
119 | |
---|
120 | #endif |
---|