1 | // $Id: kolmogorov_smirnov.cc 3014 2013-04-04 02:43:32Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008, 2009, 2010, 2012, 2013 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | |
---|
24 | #include "Suite.h" |
---|
25 | |
---|
26 | #include "yat/random/random.h" |
---|
27 | #include "yat/statistics/Averager.h" |
---|
28 | #include "yat/statistics/KolmogorovSmirnov.h" |
---|
29 | |
---|
30 | #include <boost/concept_archetype.hpp> |
---|
31 | |
---|
32 | #include <cmath> |
---|
33 | #include <deque> |
---|
34 | #include <iostream> |
---|
35 | #include <vector> |
---|
36 | |
---|
37 | using namespace theplu::yat; |
---|
38 | using statistics::KolmogorovSmirnov; |
---|
39 | |
---|
40 | void test_one_sample(test::Suite&); |
---|
41 | void test_two_sample(test::Suite&); |
---|
42 | void test_p_value(test::Suite&); |
---|
43 | void test_shuffle(test::Suite&); |
---|
44 | void test_range(test::Suite&); |
---|
45 | void test_remove(test::Suite&); |
---|
46 | void test_reset(test::Suite&); |
---|
47 | void test_ties(test::Suite&); |
---|
48 | void test_compile(void); |
---|
49 | |
---|
50 | int main(int argc, char* argv[]) |
---|
51 | { |
---|
52 | test::Suite suite(argc, argv); |
---|
53 | |
---|
54 | test_one_sample(suite); |
---|
55 | test_two_sample(suite); |
---|
56 | test_p_value(suite); |
---|
57 | test_shuffle(suite); |
---|
58 | test_range(suite); |
---|
59 | test_reset(suite); |
---|
60 | test_ties(suite); |
---|
61 | test_compile(); |
---|
62 | test_remove(suite); |
---|
63 | |
---|
64 | return suite.return_value(); |
---|
65 | } |
---|
66 | |
---|
67 | void test_compile(void) |
---|
68 | { |
---|
69 | using statistics::KolmogorovSmirnov; |
---|
70 | // do not run compiler test |
---|
71 | if (false) { |
---|
72 | KolmogorovSmirnov ks; |
---|
73 | ks.add(boost::forward_iterator_archetype<KolmogorovSmirnov::Element>(), |
---|
74 | boost::forward_iterator_archetype<KolmogorovSmirnov::Element>()); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | void test_one_sample(test::Suite& suite) |
---|
79 | { |
---|
80 | std::vector<double> correct(11); |
---|
81 | for (size_t i=0; i<correct.size(); ++i) { |
---|
82 | double s1 = 1.0 - i/10.0; |
---|
83 | double s2 = 0.0-i/10.0; |
---|
84 | if (std::abs(s1)>std::abs(s2)) |
---|
85 | correct[i] = s1; |
---|
86 | else |
---|
87 | correct[i] = s2; |
---|
88 | } |
---|
89 | |
---|
90 | for (size_t i=0; i<11; ++i) { |
---|
91 | statistics::KolmogorovSmirnov ks; |
---|
92 | for (size_t j=0; j<11; ++j) { |
---|
93 | ks.add(j, i==j); |
---|
94 | } |
---|
95 | double score = ks.signed_score(); |
---|
96 | if (!suite.add(suite.equal(score, correct[i]))) { |
---|
97 | suite.err() << "signed_score(void) failed\n"; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | statistics::KolmogorovSmirnov ks; |
---|
102 | for (size_t i=0; i<11; ++i) { |
---|
103 | ks.add(i, i==0); |
---|
104 | } |
---|
105 | size_t n=110000; |
---|
106 | double p = ks.p_value(n); |
---|
107 | double p_correct = 2.0/11.0; |
---|
108 | double margin = 10*std::sqrt(p_correct*(1-p_correct)/n); |
---|
109 | if (p>p_correct+margin || p<p_correct-margin) { |
---|
110 | suite.err() << "Error: p-value: " << p << "\n" |
---|
111 | << "expected approximately: " << p_correct << "\n" |
---|
112 | << "and at most " << margin << "deviation\n"; |
---|
113 | suite.add(false); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | void test_two_sample(test::Suite& suite) |
---|
119 | { |
---|
120 | suite.err() << "testing two sample\n"; |
---|
121 | statistics::KolmogorovSmirnov ks; |
---|
122 | for (size_t i=0; i<5; ++i) |
---|
123 | ks.add(i+0.5, i<2); |
---|
124 | suite.add(suite.equal(ks.score(), 1.0)); |
---|
125 | size_t n=100000; |
---|
126 | double p = ks.p_value(n); |
---|
127 | double p_correct = 0.2; |
---|
128 | double margin=10*std::sqrt(p_correct*(1-p_correct)/n); |
---|
129 | if (!suite.equal_fix(p, p_correct, margin) ) { |
---|
130 | suite.add(false); |
---|
131 | suite.err() << "Error: p = " << p << "\n" |
---|
132 | << "correct p would be: " << p_correct << "\n" |
---|
133 | << "expected a difference less than " << margin << "margin\n"; |
---|
134 | suite.err() << p << std::endl; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | void test_p_value(test::Suite& suite) |
---|
140 | { |
---|
141 | statistics::KolmogorovSmirnov ks; |
---|
142 | for (size_t i=0; i<100; ++i) { |
---|
143 | ks.add(i, true); |
---|
144 | ks.add(i+14.5, false); |
---|
145 | } |
---|
146 | suite.add(suite.equal(ks.score(), 0.15, 10)); |
---|
147 | |
---|
148 | statistics::Averager a; |
---|
149 | for (size_t n=0; n<100; ++n) { |
---|
150 | a.add(ks.p_value(100)); |
---|
151 | } |
---|
152 | double margin = 5 * a.standard_error(); |
---|
153 | double p_approx = ks.p_value(); |
---|
154 | if (!suite.equal_fix(a.mean(), p_approx, margin) ) { |
---|
155 | suite.add(false); |
---|
156 | suite.err() << "Error: unexpected large deviation between p_values\n" |
---|
157 | << "permutation p-value: " << a.mean() << "\n" |
---|
158 | << "analytical approximation: " << p_approx << "\n" |
---|
159 | << "expected deviation to be smaller than " << margin << "\n"; |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | void test_range(test::Suite& suite) |
---|
166 | { |
---|
167 | suite.err() << "testing range" << std::endl; |
---|
168 | std::deque<bool> labels; |
---|
169 | for (size_t i=0; i<10; ++i) { |
---|
170 | labels.push_back(i<5); |
---|
171 | } |
---|
172 | std::vector<statistics::KolmogorovSmirnov::Element> data; |
---|
173 | statistics::KolmogorovSmirnov::Element elem; |
---|
174 | elem.weight = 1.0; |
---|
175 | for (size_t i=0; i<10; ++i) { |
---|
176 | elem.value = i; |
---|
177 | elem.label = labels[i]; |
---|
178 | data.push_back(elem); |
---|
179 | } |
---|
180 | statistics::KolmogorovSmirnov ks; |
---|
181 | ks.add(data.begin(), data.end()); |
---|
182 | suite.add(suite.equal(ks.score(), 1.0)); |
---|
183 | |
---|
184 | // testing that adding a range gives same result as adding elements |
---|
185 | // sequentially |
---|
186 | statistics::KolmogorovSmirnov ks2; |
---|
187 | for (size_t i=0; i<data.size(); ++i) |
---|
188 | ks2.add(data[i].value, data[i].label, data[i].weight); |
---|
189 | suite.add(suite.equal(ks2.signed_score(), ks.signed_score())); |
---|
190 | |
---|
191 | theplu::yat::random::random_shuffle(labels.begin(), labels.end()); |
---|
192 | for (size_t i=0; i<data.size(); ++i) { |
---|
193 | data[i].label = labels[i]; |
---|
194 | } |
---|
195 | ks.reset(); |
---|
196 | ks.add(data.begin(), data.end()); |
---|
197 | ks2.reset(); |
---|
198 | for (size_t i=0; i<data.size(); ++i) |
---|
199 | ks2.add(data[i].value, data[i].label, data[i].weight); |
---|
200 | suite.add(suite.equal(ks2.signed_score(), ks.signed_score())); |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | void test_remove(test::Suite& suite) |
---|
205 | { |
---|
206 | suite.out() << "test remove\n"; |
---|
207 | KolmogorovSmirnov ks; |
---|
208 | ks.add(0, true); |
---|
209 | ks.add(1, true); |
---|
210 | ks.add(2, false); |
---|
211 | ks.add(2, true); |
---|
212 | ks.add(3, false); |
---|
213 | double score = ks.score(); |
---|
214 | double x = 0; |
---|
215 | ks.add(x, false); |
---|
216 | |
---|
217 | try { |
---|
218 | ks.remove(x, false); |
---|
219 | } |
---|
220 | catch (std::runtime_error& e) { |
---|
221 | suite.out() << "what(): " << e.what() << "\n"; |
---|
222 | suite.add(false); |
---|
223 | } |
---|
224 | suite.add(suite.equal(score, ks.score())); |
---|
225 | |
---|
226 | try { |
---|
227 | ks.remove(1,false); |
---|
228 | suite.add(false); |
---|
229 | suite.out() << "error: missing exception\n"; |
---|
230 | } |
---|
231 | catch (std::runtime_error& e) { |
---|
232 | suite.add(true); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | void test_reset(test::Suite& suite) |
---|
238 | { |
---|
239 | suite.err() << "testing reset\n"; |
---|
240 | statistics::KolmogorovSmirnov ks; |
---|
241 | ks.add(1.0, true); |
---|
242 | ks.add(2.0, false); |
---|
243 | ks.add(3.0, true); |
---|
244 | double score = ks.score(); |
---|
245 | double p = ks.p_value(); |
---|
246 | ks.reset(); |
---|
247 | ks.add(1.0, true); |
---|
248 | ks.add(2.0, false); |
---|
249 | ks.add(3.0, true); |
---|
250 | suite.add(suite.equal(ks.score(), score)); |
---|
251 | suite.add(suite.equal(ks.p_value(), p)); |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | void test_ties(test::Suite& suite) |
---|
256 | { |
---|
257 | suite.err() << "test ties" << std::endl; |
---|
258 | statistics::KolmogorovSmirnov ks; |
---|
259 | for (size_t i=0; i<5; ++i) |
---|
260 | ks.add(i, true); |
---|
261 | ks.add(0, false); |
---|
262 | suite.add(suite.equal(ks.score(), 1.0-0.2)); |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | void test_shuffle(test::Suite& suite) |
---|
267 | { |
---|
268 | suite.err() << "testing shuffle" << std::endl; |
---|
269 | statistics::KolmogorovSmirnov ks; |
---|
270 | for (size_t i=0; i<10; ++i) |
---|
271 | ks.add(i, i<5); |
---|
272 | ks.shuffle(); |
---|
273 | } |
---|