1 | // $Id: normalization_test.cc 1521 2008-09-21 17:54:19Z 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/normalizer/Centralizer.h" |
---|
25 | #include "yat/normalizer/ColumnNormalizer.h" |
---|
26 | #include "yat/utility/DataIterator.h" |
---|
27 | #include "yat/normalizer/QuantileNormalizer.h" |
---|
28 | #include "yat/normalizer/RowNormalizer.h" |
---|
29 | #include "yat/normalizer/Spearman.h" |
---|
30 | #include "yat/normalizer/Zscore.h" |
---|
31 | |
---|
32 | #include "yat/utility/DataIterator.h" |
---|
33 | #include "yat/utility/Matrix.h" |
---|
34 | #include "yat/utility/MatrixWeighted.h" |
---|
35 | #include "yat/utility/WeightIterator.h" |
---|
36 | |
---|
37 | #include <limits> |
---|
38 | #include <vector> |
---|
39 | |
---|
40 | using namespace theplu::yat; |
---|
41 | void test_centralizer(test::Suite&); |
---|
42 | void test_column_normalize(test::Suite&); |
---|
43 | void test_quantile_normalize(test::Suite&); |
---|
44 | void test_row_normalize(test::Suite&); |
---|
45 | void test_spearman(test::Suite&); |
---|
46 | void test_spearman_weighted(test::Suite&); |
---|
47 | void test_z_score(test::Suite&); |
---|
48 | |
---|
49 | int main(int argc, char* argv[]) |
---|
50 | { |
---|
51 | test::Suite suite(argc, argv); |
---|
52 | suite.err() << "testing normalizations ... " << std::endl; |
---|
53 | |
---|
54 | test_centralizer(suite); |
---|
55 | test_column_normalize(suite); |
---|
56 | test_quantile_normalize(suite); |
---|
57 | test_row_normalize(suite); |
---|
58 | test_spearman(suite); |
---|
59 | test_z_score(suite); |
---|
60 | |
---|
61 | return suite.return_value(); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | void test_centralizer(test::Suite& suite) |
---|
66 | { |
---|
67 | suite.err() << "Testing Centralizer\n"; |
---|
68 | std::vector<double> vec; |
---|
69 | vec.push_back(1); |
---|
70 | vec.push_back(2); |
---|
71 | vec.push_back(3); |
---|
72 | normalizer::Centralizer<> c; |
---|
73 | c(vec.begin(), vec.end(), vec.begin()); |
---|
74 | for (size_t i=0; i<vec.size(); ++i) |
---|
75 | suite.add(suite.equal(vec[i], static_cast<double>(i)-1.0)); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void test_column_normalize(test::Suite& suite) |
---|
80 | { |
---|
81 | using namespace normalizer; |
---|
82 | suite.err() << "Testing ColumnNormalizer\n"; |
---|
83 | |
---|
84 | utility::Matrix m(2,2); |
---|
85 | m(0,0) = 0; |
---|
86 | m(0,1) = 10; |
---|
87 | m(1,0) = 2; |
---|
88 | m(1,1) = 4; |
---|
89 | ColumnNormalizer<Centralizer<> > qn; |
---|
90 | qn(m, m); |
---|
91 | suite.err() << "Testing m(0,0)\n"; |
---|
92 | suite.add(suite.equal(m(0,0), -1)); |
---|
93 | suite.err() << "Testing m(0,1)\n"; |
---|
94 | suite.add(suite.equal(m(0,1), 3)); |
---|
95 | suite.err() << "Testing m(1,0)\n"; |
---|
96 | suite.add(suite.equal(m(1,0), 1)); |
---|
97 | suite.err() << "Testing m(1,1)\n"; |
---|
98 | suite.add(suite.equal(m(1,1), -3)); |
---|
99 | } |
---|
100 | |
---|
101 | void test_quantile_normalize(test::Suite& suite) |
---|
102 | { |
---|
103 | suite.err() << "Testing QuantileNormalizer\n"; |
---|
104 | |
---|
105 | utility::Matrix m(2,2); |
---|
106 | m(0,0) = 0; |
---|
107 | m(0,1) = 10; |
---|
108 | m(1,0) = 2; |
---|
109 | m(1,1) = 4; |
---|
110 | normalizer::QuantileNormalizer qn; |
---|
111 | qn(m, m); |
---|
112 | suite.err() << "Testing m(0,0)\n"; |
---|
113 | suite.add(suite.equal(m(0,0), 2)); |
---|
114 | suite.err() << "Testing m(0,1)\n"; |
---|
115 | suite.add(suite.equal(m(0,1), 6)); |
---|
116 | suite.err() << "Testing m(1,0)\n"; |
---|
117 | suite.add(suite.equal(m(1,0), 6)); |
---|
118 | suite.err() << "Testing m(1,1)\n"; |
---|
119 | suite.add(suite.equal(m(1,1), 2)); |
---|
120 | } |
---|
121 | |
---|
122 | void test_row_normalize(test::Suite& suite) |
---|
123 | { |
---|
124 | using namespace normalizer; |
---|
125 | suite.err() << "Testing RowNormalizer\n"; |
---|
126 | |
---|
127 | utility::Matrix m(2,3); |
---|
128 | m(0,0) = 0; |
---|
129 | m(0,1) = 10; |
---|
130 | m(1,0) = 2; |
---|
131 | m(1,1) = 4; |
---|
132 | utility::Matrix m2(m); |
---|
133 | m2.transpose(); |
---|
134 | ColumnNormalizer<Centralizer<> > cn; |
---|
135 | RowNormalizer<Centralizer<> > rn; |
---|
136 | cn(m, m); |
---|
137 | rn(m2, m2); |
---|
138 | m2.transpose(); |
---|
139 | suite.equal_range(m.begin(), m.end(), m2.begin()); |
---|
140 | } |
---|
141 | |
---|
142 | void test_spearman(test::Suite& suite) |
---|
143 | { |
---|
144 | suite.err() << "Testing Spearman\n"; |
---|
145 | normalizer::Spearman spearman; |
---|
146 | std::vector<double> vec; |
---|
147 | vec.push_back(0); |
---|
148 | vec.push_back(2); |
---|
149 | vec.push_back(3); |
---|
150 | vec.push_back(1); |
---|
151 | spearman(vec.begin(), vec.end(), vec.begin()); |
---|
152 | std::vector<double> correct; |
---|
153 | correct.push_back(1.0/8); |
---|
154 | correct.push_back(5.0/8); |
---|
155 | correct.push_back(7.0/8); |
---|
156 | correct.push_back(3.0/8); |
---|
157 | suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin())); |
---|
158 | suite.err() << "Testing Spearman with ties\n"; |
---|
159 | vec[1]=vec[2]; |
---|
160 | correct[1] = correct[2] = (correct[1]+correct[2])/2; |
---|
161 | spearman(vec.begin(), vec.end(), vec.begin()); |
---|
162 | suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin())); |
---|
163 | test_spearman_weighted(suite); |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | void test_spearman_weighted(test::Suite& suite) |
---|
168 | { |
---|
169 | suite.err() << "Testing Weighted Spearman\n"; |
---|
170 | normalizer::Spearman spearman; |
---|
171 | |
---|
172 | suite.err() << "Testing that unity weights reproduces unweighted case\n"; |
---|
173 | utility::MatrixWeighted m(1,4,0,1); |
---|
174 | utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0); |
---|
175 | m(0,0).data()=0; |
---|
176 | m(0,1).data()=2; |
---|
177 | m(0,2).data()=3; |
---|
178 | m(0,3).data()=1; |
---|
179 | std::vector<double> correct(m.columns()); |
---|
180 | std::vector<double> correct_w(m.columns(), 1.0); |
---|
181 | std::copy(utility::data_iterator(m.begin_row(0)), |
---|
182 | utility::data_iterator(m.end_row(0)), |
---|
183 | correct.begin()); |
---|
184 | spearman(correct.begin(), correct.end(), correct.begin()); |
---|
185 | spearman(m.begin_row(0), m.end_row(0), res.begin_row(0)); |
---|
186 | |
---|
187 | using utility::data_iterator; |
---|
188 | suite.add(suite.equal_range(data_iterator(res.begin_row(0)), |
---|
189 | data_iterator(res.end_row(0)), |
---|
190 | correct.begin())); |
---|
191 | using utility::weight_iterator; |
---|
192 | suite.add(suite.equal_range(weight_iterator(res.begin_row(0)), |
---|
193 | weight_iterator(res.end_row(0)), |
---|
194 | correct_w.begin())); |
---|
195 | |
---|
196 | suite.err() << "Testing rescaling of weights\n"; |
---|
197 | for (size_t i=0; i<m.columns(); ++i) { |
---|
198 | m(0,i).weight() *= 2; |
---|
199 | correct_w[i] *= 2; |
---|
200 | } |
---|
201 | spearman(m.begin_row(0), m.end_row(0), res.begin_row(0)); |
---|
202 | suite.add(suite.equal_range(data_iterator(res.begin_row(0)), |
---|
203 | data_iterator(res.end_row(0)), |
---|
204 | correct.begin())); |
---|
205 | suite.add(suite.equal_range(weight_iterator(res.begin_row(0)), |
---|
206 | weight_iterator(res.end_row(0)), |
---|
207 | correct_w.begin())); |
---|
208 | |
---|
209 | |
---|
210 | suite.err() << "Testing case with a zero weight\n"; |
---|
211 | m(0,1).data() = std::numeric_limits<double>::quiet_NaN(); |
---|
212 | m(0,1).weight() = 0.0; |
---|
213 | spearman(m.begin_row(0), m.end_row(0), res.begin_row(0)); |
---|
214 | suite.add(suite.equal(res(0,0).data(), 0.5/3)); |
---|
215 | suite.add(suite.equal(res(0,2).data(), 2.5/3)); |
---|
216 | suite.add(suite.equal(res(0,3).data(), 1.5/3)); |
---|
217 | |
---|
218 | suite.err() << "Testing case with ties\n"; |
---|
219 | m(0,0).data() = m(0,2).data(); |
---|
220 | spearman(m.begin_row(0), m.end_row(0), res.begin_row(0)); |
---|
221 | suite.add(suite.equal(res(0,0).data(), 2.0/3)); |
---|
222 | suite.add(suite.equal(res(0,2).data(), 2.0/3)); |
---|
223 | suite.add(suite.equal(res(0,3).data(), 0.5/3)); |
---|
224 | } |
---|
225 | |
---|
226 | void test_z_score(test::Suite& suite) |
---|
227 | { |
---|
228 | suite.err() << "Testing Zscore\n"; |
---|
229 | std::vector<double> vec; |
---|
230 | vec.push_back(0); |
---|
231 | vec.push_back(3.14); |
---|
232 | normalizer::Zscore zscore; |
---|
233 | zscore(vec.begin(), vec.end(), vec.begin()); |
---|
234 | for (size_t i=0; i<vec.size(); ++i) |
---|
235 | suite.add(suite.equal(vec[i], 2.0*i-1.0)); |
---|
236 | } |
---|
237 | |
---|
238 | |
---|