2636 |
10 Nov 11 |
peter |
// $Id$ |
2636 |
10 Nov 11 |
peter |
2 |
|
2636 |
10 Nov 11 |
peter |
3 |
/* |
2919 |
19 Dec 12 |
peter |
Copyright (C) 2011, 2012 Peter Johansson |
2636 |
10 Nov 11 |
peter |
5 |
|
2636 |
10 Nov 11 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
2636 |
10 Nov 11 |
peter |
7 |
|
2636 |
10 Nov 11 |
peter |
The yat library is free software; you can redistribute it and/or |
2636 |
10 Nov 11 |
peter |
modify it under the terms of the GNU General Public License as |
2636 |
10 Nov 11 |
peter |
published by the Free Software Foundation; either version 3 of the |
2636 |
10 Nov 11 |
peter |
License, or (at your option) any later version. |
2636 |
10 Nov 11 |
peter |
12 |
|
2636 |
10 Nov 11 |
peter |
The yat library is distributed in the hope that it will be useful, |
2636 |
10 Nov 11 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2636 |
10 Nov 11 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2636 |
10 Nov 11 |
peter |
General Public License for more details. |
2636 |
10 Nov 11 |
peter |
17 |
|
2636 |
10 Nov 11 |
peter |
You should have received a copy of the GNU General Public License |
2636 |
10 Nov 11 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
2636 |
10 Nov 11 |
peter |
20 |
*/ |
2636 |
10 Nov 11 |
peter |
21 |
|
2881 |
18 Nov 12 |
peter |
22 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
23 |
|
2636 |
10 Nov 11 |
peter |
24 |
#include "Suite.h" |
2636 |
10 Nov 11 |
peter |
25 |
|
2636 |
10 Nov 11 |
peter |
26 |
#include "yat/statistics/Spearman.h" |
2642 |
10 Nov 11 |
peter |
27 |
#include "yat/statistics/utility.h" |
2636 |
10 Nov 11 |
peter |
28 |
|
2636 |
10 Nov 11 |
peter |
29 |
#include <gsl/gsl_cdf.h> |
2636 |
10 Nov 11 |
peter |
30 |
|
2636 |
10 Nov 11 |
peter |
31 |
#include <cmath> |
2645 |
14 Nov 11 |
peter |
32 |
#include <iomanip> |
2636 |
10 Nov 11 |
peter |
33 |
|
2636 |
10 Nov 11 |
peter |
34 |
using namespace theplu::yat; |
2636 |
10 Nov 11 |
peter |
35 |
using statistics::Spearman; |
2636 |
10 Nov 11 |
peter |
36 |
|
2636 |
10 Nov 11 |
peter |
37 |
void test_add(test::Suite&); |
2636 |
10 Nov 11 |
peter |
38 |
void test_p(test::Suite&); |
2636 |
10 Nov 11 |
peter |
39 |
void test_p_exact(test::Suite&); |
2646 |
14 Nov 11 |
peter |
40 |
void test_p_exact_with_ties(test::Suite&); |
2636 |
10 Nov 11 |
peter |
41 |
void test_score(test::Suite&); |
2636 |
10 Nov 11 |
peter |
42 |
void test_ties(test::Suite&); |
2636 |
10 Nov 11 |
peter |
43 |
|
2636 |
10 Nov 11 |
peter |
44 |
int main(int argc, char* argv[]) |
2642 |
10 Nov 11 |
peter |
45 |
{ |
2636 |
10 Nov 11 |
peter |
46 |
test::Suite suite(argc, argv); |
2636 |
10 Nov 11 |
peter |
47 |
|
2636 |
10 Nov 11 |
peter |
48 |
test_add(suite); |
2636 |
10 Nov 11 |
peter |
49 |
test_p(suite); |
2636 |
10 Nov 11 |
peter |
50 |
test_p_exact(suite); |
2646 |
14 Nov 11 |
peter |
51 |
test_p_exact_with_ties(suite); |
2636 |
10 Nov 11 |
peter |
52 |
test_score(suite); |
2636 |
10 Nov 11 |
peter |
53 |
test_ties(suite); |
2636 |
10 Nov 11 |
peter |
54 |
return suite.return_value(); |
2636 |
10 Nov 11 |
peter |
55 |
} |
2636 |
10 Nov 11 |
peter |
56 |
|
2636 |
10 Nov 11 |
peter |
57 |
|
2636 |
10 Nov 11 |
peter |
58 |
void test_add(test::Suite& suite) |
2636 |
10 Nov 11 |
peter |
59 |
{ |
2642 |
10 Nov 11 |
peter |
60 |
suite.out() << YAT_TEST_PROLOGUE; |
2636 |
10 Nov 11 |
peter |
61 |
Spearman spearman; |
2636 |
10 Nov 11 |
peter |
62 |
spearman.add(0,0); |
2636 |
10 Nov 11 |
peter |
63 |
spearman.add(1,1); |
2636 |
10 Nov 11 |
peter |
64 |
double score = spearman.score(); |
2636 |
10 Nov 11 |
peter |
65 |
if (!suite.add(suite.equal(score, 1.0))) |
2636 |
10 Nov 11 |
peter |
66 |
suite.err() << "score not 1.0\n"; |
2636 |
10 Nov 11 |
peter |
67 |
} |
2636 |
10 Nov 11 |
peter |
68 |
|
2636 |
10 Nov 11 |
peter |
69 |
|
2636 |
10 Nov 11 |
peter |
70 |
void test_p(test::Suite& suite) |
2636 |
10 Nov 11 |
peter |
71 |
{ |
2642 |
10 Nov 11 |
peter |
72 |
suite.out() << YAT_TEST_PROLOGUE; |
2636 |
10 Nov 11 |
peter |
73 |
Spearman spearman; |
2636 |
10 Nov 11 |
peter |
74 |
for (size_t i=0; i<4; ++i) |
2636 |
10 Nov 11 |
peter |
75 |
spearman.add(i,i); |
2636 |
10 Nov 11 |
peter |
76 |
suite.out() << "score:\n"; |
2636 |
10 Nov 11 |
peter |
77 |
double correct_score = 1.0; |
2636 |
10 Nov 11 |
peter |
78 |
suite.add(suite.equal(spearman.score(), correct_score)); |
2636 |
10 Nov 11 |
peter |
79 |
suite.out() << "p-value:\n"; |
2642 |
10 Nov 11 |
peter |
80 |
double correct = statistics::pearson_p_value(1, 4); |
2636 |
10 Nov 11 |
peter |
81 |
suite.out() << "p_right:\n"; |
2642 |
10 Nov 11 |
peter |
82 |
suite.add(suite.equal(spearman.p_right(), correct, 5)); |
2636 |
10 Nov 11 |
peter |
83 |
suite.out() << "p_value equals 2*p_right\n"; |
2636 |
10 Nov 11 |
peter |
84 |
suite.add(suite.equal(2*spearman.p_right(), spearman.p_value())); |
2636 |
10 Nov 11 |
peter |
85 |
} |
2636 |
10 Nov 11 |
peter |
86 |
|
2636 |
10 Nov 11 |
peter |
87 |
|
2636 |
10 Nov 11 |
peter |
88 |
void test_p_exact(test::Suite& suite) |
2636 |
10 Nov 11 |
peter |
89 |
{ |
2642 |
10 Nov 11 |
peter |
90 |
suite.out() << YAT_TEST_PROLOGUE; |
2636 |
10 Nov 11 |
peter |
91 |
Spearman spearman; |
2636 |
10 Nov 11 |
peter |
92 |
for (size_t i=0; i<4; ++i) |
2636 |
10 Nov 11 |
peter |
93 |
spearman.add(i,i); |
2636 |
10 Nov 11 |
peter |
94 |
suite.out() << "p_right with r=1.0\n"; |
2636 |
10 Nov 11 |
peter |
95 |
suite.add(suite.equal(spearman.p_right(true), 1.0/24)); |
2645 |
14 Nov 11 |
peter |
96 |
|
2636 |
10 Nov 11 |
peter |
97 |
spearman.reset(); |
2636 |
10 Nov 11 |
peter |
98 |
spearman.add(0,1); |
2636 |
10 Nov 11 |
peter |
99 |
spearman.add(2,2); |
2636 |
10 Nov 11 |
peter |
100 |
spearman.add(3,4); |
2636 |
10 Nov 11 |
peter |
101 |
spearman.add(10,3); |
2636 |
10 Nov 11 |
peter |
102 |
suite.out() << "p_right with r=0.8\n"; |
2636 |
10 Nov 11 |
peter |
103 |
suite.add(suite.equal(spearman.p_right(true), 4.0/24)); |
2642 |
10 Nov 11 |
peter |
104 |
|
2642 |
10 Nov 11 |
peter |
105 |
suite.out() << "comparing exact and approimative\n"; |
2645 |
14 Nov 11 |
peter |
106 |
std::vector<double> correct; |
2642 |
10 Nov 11 |
peter |
107 |
Spearman spearman2; |
2642 |
10 Nov 11 |
peter |
108 |
spearman2.add(0, 1000); |
2642 |
10 Nov 11 |
peter |
109 |
spearman2.add(1000, 500); |
2645 |
14 Nov 11 |
peter |
110 |
std::vector<double> margin(11,0.01); |
2645 |
14 Nov 11 |
peter |
111 |
margin[3] = 0.35; |
2645 |
14 Nov 11 |
peter |
112 |
margin[4] = 0.12; |
2645 |
14 Nov 11 |
peter |
113 |
margin[6] = 0.02; |
2645 |
14 Nov 11 |
peter |
114 |
margin[7] = 0.02; |
2645 |
14 Nov 11 |
peter |
115 |
using std::setw; |
2645 |
14 Nov 11 |
peter |
116 |
suite.out() << std::left; |
2645 |
14 Nov 11 |
peter |
117 |
suite.out() << setw(3) << "n" |
2645 |
14 Nov 11 |
peter |
118 |
<< setw(14) << "score" |
2645 |
14 Nov 11 |
peter |
119 |
<< setw(10) << "exact" |
2645 |
14 Nov 11 |
peter |
120 |
<< setw(10) << "approx" |
2645 |
14 Nov 11 |
peter |
121 |
<< setw(10) << "difference" |
2645 |
14 Nov 11 |
peter |
122 |
<< "\n"; |
2645 |
14 Nov 11 |
peter |
123 |
for (size_t i=3; i<margin.size(); ++i) { |
2642 |
10 Nov 11 |
peter |
124 |
spearman2.add(i,i); |
2642 |
10 Nov 11 |
peter |
125 |
double exact = spearman2.p_value(true); |
2642 |
10 Nov 11 |
peter |
126 |
double approx = spearman2.p_value(false); |
2642 |
10 Nov 11 |
peter |
127 |
double diff = exact - approx; |
2645 |
14 Nov 11 |
peter |
128 |
suite.out() << std::setw(3) << i |
2645 |
14 Nov 11 |
peter |
129 |
<< std::setw(14) << spearman2.score() |
2645 |
14 Nov 11 |
peter |
130 |
<< std::setw(10) << exact |
2645 |
14 Nov 11 |
peter |
131 |
<< std::setw(10) << approx |
2645 |
14 Nov 11 |
peter |
132 |
<< std::setw(10) << diff << "\n"; |
2645 |
14 Nov 11 |
peter |
133 |
suite.add(suite.equal_fix(exact, approx, margin[i])); |
2642 |
10 Nov 11 |
peter |
134 |
} |
2636 |
10 Nov 11 |
peter |
135 |
} |
2636 |
10 Nov 11 |
peter |
136 |
|
2636 |
10 Nov 11 |
peter |
137 |
|
2646 |
14 Nov 11 |
peter |
138 |
void test_p_exact_with_ties(test::Suite& suite) |
2646 |
14 Nov 11 |
peter |
139 |
{ |
2646 |
14 Nov 11 |
peter |
140 |
suite.out() << YAT_TEST_PROLOGUE; |
2646 |
14 Nov 11 |
peter |
141 |
Spearman spearman; |
2646 |
14 Nov 11 |
peter |
142 |
spearman.add(0,0); |
2646 |
14 Nov 11 |
peter |
143 |
spearman.add(1,1); |
2646 |
14 Nov 11 |
peter |
144 |
spearman.add(3,1); |
2646 |
14 Nov 11 |
peter |
145 |
spearman.add(3,1); |
2646 |
14 Nov 11 |
peter |
146 |
suite.add(suite.equal(spearman.p_value(true), 0.25)); |
2646 |
14 Nov 11 |
peter |
147 |
} |
2646 |
14 Nov 11 |
peter |
148 |
|
2646 |
14 Nov 11 |
peter |
149 |
|
2636 |
10 Nov 11 |
peter |
150 |
void test_score(test::Suite& suite) |
2636 |
10 Nov 11 |
peter |
151 |
{ |
2642 |
10 Nov 11 |
peter |
152 |
suite.out() << YAT_TEST_PROLOGUE; |
2636 |
10 Nov 11 |
peter |
153 |
Spearman spearman; |
2636 |
10 Nov 11 |
peter |
154 |
spearman.add(7.1, 2.8); |
2636 |
10 Nov 11 |
peter |
155 |
spearman.add(7.2, 2.9); |
2636 |
10 Nov 11 |
peter |
156 |
spearman.add(8.3, 2.6); |
2636 |
10 Nov 11 |
peter |
157 |
spearman.add(11.4, 5.0); |
2636 |
10 Nov 11 |
peter |
158 |
double correct = 1.0 - 6.0*(4.0+1.0+1.0+0.0)/(4*15); |
2636 |
10 Nov 11 |
peter |
159 |
suite.add(suite.equal(spearman.score(), correct)); |
2636 |
10 Nov 11 |
peter |
160 |
} |
2636 |
10 Nov 11 |
peter |
161 |
|
2636 |
10 Nov 11 |
peter |
162 |
void test_ties(test::Suite& suite) |
2636 |
10 Nov 11 |
peter |
163 |
{ |
2642 |
10 Nov 11 |
peter |
164 |
suite.out() << YAT_TEST_PROLOGUE; |
2636 |
10 Nov 11 |
peter |
165 |
Spearman spearman; |
2636 |
10 Nov 11 |
peter |
166 |
spearman.add(7.1, 2.8); |
2636 |
10 Nov 11 |
peter |
167 |
spearman.add(7.1, 2.9); |
2636 |
10 Nov 11 |
peter |
168 |
spearman.add(7.2, 2.8); |
2636 |
10 Nov 11 |
peter |
169 |
spearman.add(8.3, 2.6); |
2636 |
10 Nov 11 |
peter |
170 |
spearman.add(9.4, 3.5); |
2636 |
10 Nov 11 |
peter |
171 |
spearman.add(10.5, 4.6); |
2636 |
10 Nov 11 |
peter |
172 |
spearman.add(11.4, 5.0); |
2636 |
10 Nov 11 |
peter |
173 |
suite.out() << "Spearman:score\n"; |
2638 |
10 Nov 11 |
peter |
174 |
double correct = 0.700; |
2636 |
10 Nov 11 |
peter |
175 |
suite.add(suite.equal(spearman.score(), correct)); |
2636 |
10 Nov 11 |
peter |
176 |
suite.out() << "Spearman:p_right\n"; |
2642 |
10 Nov 11 |
peter |
177 |
correct = 0.0400; |
2642 |
10 Nov 11 |
peter |
178 |
suite.add(suite.equal_fix(spearman.p_right(), correct, 10e-4)); |
2636 |
10 Nov 11 |
peter |
179 |
suite.out() << "Spearman:p_left\n"; |
2642 |
10 Nov 11 |
peter |
180 |
suite.add(suite.equal_fix(spearman.p_left(), 1.0-correct, 10e-4)); |
2636 |
10 Nov 11 |
peter |
181 |
suite.out() << "Spearman:p_value\n"; |
2642 |
10 Nov 11 |
peter |
182 |
suite.add(suite.equal_fix(spearman.p_value(), 2*correct, 2*10e-4)); |
2636 |
10 Nov 11 |
peter |
183 |
} |