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