1 | // $Id: kolmogorov_smirnov_test.cc 1687 2008-12-30 22:00:24Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008 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 "Suite.h" |
---|
23 | |
---|
24 | #include "yat/statistics/Averager.h" |
---|
25 | #include "yat/statistics/KolmogorovSmirnov.h" |
---|
26 | #include "yat/random/random.h" |
---|
27 | |
---|
28 | #include <cmath> |
---|
29 | #include <iostream> |
---|
30 | |
---|
31 | using namespace theplu::yat; |
---|
32 | |
---|
33 | void test_one_sample(test::Suite&); |
---|
34 | void test_two_sample(test::Suite&); |
---|
35 | void test_p_value(test::Suite&); |
---|
36 | void test_reset(test::Suite&); |
---|
37 | void test_ties(test::Suite&); |
---|
38 | |
---|
39 | int main(int argc, char* argv[]) |
---|
40 | { |
---|
41 | test::Suite suite(argc, argv); |
---|
42 | |
---|
43 | test_one_sample(suite); |
---|
44 | test_two_sample(suite); |
---|
45 | test_p_value(suite); |
---|
46 | test_reset(suite); |
---|
47 | test_ties(suite); |
---|
48 | |
---|
49 | return suite.return_value(); |
---|
50 | } |
---|
51 | |
---|
52 | void test_one_sample(test::Suite& suite) |
---|
53 | { |
---|
54 | std::vector<double> correct(11); |
---|
55 | for (size_t i=0; i<correct.size(); ++i) { |
---|
56 | double s1 = 1.0 - i/10.0; |
---|
57 | double s2 = 0.0-i/10.0; |
---|
58 | if (std::abs(s1)>std::abs(s2)) |
---|
59 | correct[i] = s1; |
---|
60 | else |
---|
61 | correct[i] = s2; |
---|
62 | } |
---|
63 | |
---|
64 | for (size_t i=0; i<11; ++i) { |
---|
65 | statistics::KolmogorovSmirnov ks; |
---|
66 | for (size_t j=0; j<11; ++j) { |
---|
67 | ks.add(j, i==j); |
---|
68 | } |
---|
69 | double score = ks.signed_score(); |
---|
70 | if (!suite.add(suite.equal(score, correct[i]))) { |
---|
71 | suite.err() << "signed_score(void) failed\n"; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | statistics::KolmogorovSmirnov ks; |
---|
76 | for (size_t i=0; i<11; ++i) { |
---|
77 | ks.add(i, i==0); |
---|
78 | } |
---|
79 | size_t n=110000; |
---|
80 | double p = ks.p_value(n); |
---|
81 | double p_correct = 2.0/11.0; |
---|
82 | double margin = 10*std::sqrt(p_correct*(1-p_correct)/n); |
---|
83 | if (p>p_correct+margin || p<p_correct-margin) { |
---|
84 | suite.err() << "Error: p-value: " << p << "\n" |
---|
85 | << "expected approximately: " << p_correct << "\n" |
---|
86 | << "and at most " << margin << "deviation\n"; |
---|
87 | suite.add(false); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | void test_two_sample(test::Suite& suite) |
---|
93 | { |
---|
94 | suite.err() << "testing two sample\n"; |
---|
95 | statistics::KolmogorovSmirnov ks; |
---|
96 | for (size_t i=0; i<5; ++i) |
---|
97 | ks.add(i+0.5, i<2); |
---|
98 | suite.add(suite.equal(ks.score(), 1.0)); |
---|
99 | size_t n=100000; |
---|
100 | double p = ks.p_value(n); |
---|
101 | double p_correct = 0.2; |
---|
102 | double margin=10*std::sqrt(p_correct*(1-p_correct)/n); |
---|
103 | if (std::abs(p-p_correct)>margin) { |
---|
104 | suite.add(false); |
---|
105 | suite.err() << "Error: p = " << p << "\n" |
---|
106 | << "correct p would be: " << p_correct << "\n" |
---|
107 | << "expected a difference less than " << margin << "margin\n"; |
---|
108 | suite.err() << p << std::endl; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void test_p_value(test::Suite& suite) |
---|
114 | { |
---|
115 | statistics::KolmogorovSmirnov ks; |
---|
116 | for (size_t i=0; i<100; ++i) { |
---|
117 | ks.add(i, true); |
---|
118 | ks.add(i+14.5, false); |
---|
119 | } |
---|
120 | suite.add(suite.equal(ks.score(), 0.15, 10)); |
---|
121 | |
---|
122 | statistics::Averager a; |
---|
123 | for (size_t n=0; n<100; ++n) { |
---|
124 | a.add(ks.p_value(100)); |
---|
125 | } |
---|
126 | double margin = 5 * a.standard_error(); |
---|
127 | double p_approx = ks.p_value(); |
---|
128 | if (std::abs(a.mean()-p_approx)>margin) { |
---|
129 | suite.add(false); |
---|
130 | suite.err() << "Error: unexpected large deviation between p_values\n" |
---|
131 | << "permutation p-value: " << a.mean() << "\n" |
---|
132 | << "analytical approximation: " << p_approx << "\n" |
---|
133 | << "expected deviation to be smaller than " << margin << "\n"; |
---|
134 | } |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | void test_reset(test::Suite& suite) |
---|
139 | { |
---|
140 | suite.err() << "testing reset\n"; |
---|
141 | statistics::KolmogorovSmirnov ks; |
---|
142 | ks.add(1.0, true); |
---|
143 | ks.add(2.0, false); |
---|
144 | ks.add(3.0, true); |
---|
145 | double score = ks.score(); |
---|
146 | double p = ks.p_value(); |
---|
147 | ks.reset(); |
---|
148 | ks.add(1.0, true); |
---|
149 | ks.add(2.0, false); |
---|
150 | ks.add(3.0, true); |
---|
151 | suite.add(suite.equal(ks.score(), score)); |
---|
152 | suite.add(suite.equal(ks.p_value(), p)); |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | void test_ties(test::Suite& suite) |
---|
157 | { |
---|
158 | suite.err() << "test ties" << std::endl; |
---|
159 | statistics::KolmogorovSmirnov ks; |
---|
160 | for (size_t i=0; i<5; ++i) |
---|
161 | ks.add(i, true); |
---|
162 | ks.add(0, false); |
---|
163 | suite.equal(ks.score(), 1.0-0.2); |
---|
164 | } |
---|