source: trunk/test/normalization_test.cc @ 1735

Last change on this file since 1735 was 1735, checked in by Jari Häkkinen, 12 years ago

Addresses #425. Taking care of some special cases. No iterative test is need.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.9 KB
Line 
1// $Id: normalization_test.cc 1735 2009-01-16 21:33:53Z jari $
2
3/*
4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009 Jari Häkkinen
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Suite.h"
24
25#include "yat/normalizer/Centralizer.h"
26#include "yat/normalizer/ColumnNormalizer.h"
27#include "yat/normalizer/Gauss.h"
28#include "yat/normalizer/qQuantileNormalizer.h"
29#include "yat/normalizer/QuantileNormalizer.h"
30#include "yat/normalizer/RowNormalizer.h"
31#include "yat/normalizer/Spearman.h"
32#include "yat/normalizer/Zscore.h"
33
34#include "yat/utility/DataIterator.h"
35#include "yat/utility/FileUtil.h"
36#include "yat/utility/Matrix.h"
37#include "yat/utility/MatrixWeighted.h"
38#include "yat/utility/WeightIterator.h"
39
40#include <fstream>
41#include <limits>
42#include <vector>
43
44using namespace theplu::yat;
45void test_centralizer(test::Suite&);
46void test_column_normalize(test::Suite&);
47void test_gauss_normalize(test::Suite&);
48void test_qquantile_normalize(test::Suite&);
49void test_quantile_normalize(test::Suite&);
50void test_row_normalize(test::Suite&);
51void test_spearman(test::Suite&);
52void test_spearman_weighted(test::Suite&);
53void test_z_score(test::Suite&);
54
55int main(int argc, char* argv[])
56{ 
57  test::Suite suite(argc, argv);
58  suite.err() << "testing normalizations ... " << std::endl;
59
60  test_centralizer(suite);
61  test_column_normalize(suite);
62  test_qquantile_normalize(suite);
63  test_quantile_normalize(suite);
64  test_gauss_normalize(suite);
65  test_row_normalize(suite);
66  test_spearman(suite);
67  test_z_score(suite);
68
69  return suite.return_value();
70}
71
72
73void test_centralizer(test::Suite& suite)
74{
75  suite.err() << "Testing Centralizer\n";
76  std::vector<double> vec;
77  vec.push_back(1);
78  vec.push_back(2);
79  vec.push_back(3);
80  normalizer::Centralizer<> c;
81  c(vec.begin(), vec.end(), vec.begin());
82  for (size_t i=0; i<vec.size(); ++i)
83    suite.add(suite.equal(vec[i], static_cast<double>(i)-1.0));
84
85  std::vector<utility::DataWeight> vec2;
86  vec2.push_back(utility::DataWeight(1,1));
87  vec2.push_back(utility::DataWeight(2,0.5));
88  vec2.push_back(utility::DataWeight(2,0.5));
89  std::vector<utility::DataWeight> vec3(vec2.size());
90  c(vec2.begin(), vec2.end(), vec3.begin());
91  for (size_t i=0; i<vec2.size(); ++i)
92    suite.add(suite.equal(vec3[i].weight(), vec2[i].weight()));
93  suite.add(suite.equal(vec3[0].data(), -0.5));
94  suite.add(suite.equal(vec3[1].data(), 0.5));
95  suite.add(suite.equal(vec3[2].data(), 0.5));
96
97}
98
99
100void test_column_normalize(test::Suite& suite)
101{
102  using namespace normalizer;
103  suite.err() << "Testing ColumnNormalizer\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  ColumnNormalizer<Centralizer<> > qn;
111  qn(m, m);
112  suite.err() << "Testing m(0,0)\n";
113  suite.add(suite.equal(m(0,0), -1));
114  suite.err() << "Testing m(0,1)\n";
115  suite.add(suite.equal(m(0,1), 3));
116  suite.err() << "Testing m(1,0)\n";
117  suite.add(suite.equal(m(1,0), 1));
118  suite.err() << "Testing m(1,1)\n";
119  suite.add(suite.equal(m(1,1), -3));
120}
121
122
123void test_qquantile_normalize(test::Suite& suite)
124{
125  using namespace normalizer;
126
127  suite.err() << "Testing qQuantileNormalizer\n";
128  std::string data(test::filename("data/normalization_test.data"));
129  if (utility::FileUtil(data.c_str()).permissions("r")) {
130    suite.add(false);
131    suite.err() << "Cannot access file " << data << '\n';
132    return;
133  }
134  std::ifstream data_stream(data.c_str());
135
136  utility::Matrix m(data_stream);
137
138  suite.err() << "testing number of parts (Q) boundary conditions\n";
139  qQuantileNormalizer(m.column_const_view(0),m.rows());
140  qQuantileNormalizer(m.column_const_view(0),3);
141
142  qQuantileNormalizer qqn(m.column_const_view(0),9);  // first column as target
143  utility::Matrix result(m.rows(),m.columns());
144  qqn(m,result);
145
146  suite.err() << "test that result can be stored in the source matrix\n";
147  qqn(m,m);
148  suite.add(result==m);
149
150  // Enough iteration will make all columns to have the same values as
151  // the target.
152  suite.err() << "Testing that q=matrix rows gives QuantileNormaliztion\n";
153  utility::Matrix m2(4,2);
154  m2(0,0) = 0; m2(0,1) = 10;
155  m2(1,0) = 2; m2(1,1) = 4;
156  m2(2,0) = 1; m2(2,1) = 0;
157  m2(3,0) = 3; m2(3,1) = 7;
158  qQuantileNormalizer qqn2(m2.column_const_view(0),m2.rows());
159  utility::Matrix result2(m2.rows(),m2.columns());
160  qqn2(m2,result2);
161  suite.add( suite.equal_fix(m2(0,0),result2(2,1),1.0e-12) &&
162             suite.equal_fix(m2(1,0),result2(3,1),1.0e-12) &&
163             suite.equal_fix(m2(2,0),result2(1,1),1.0e-12) &&
164             suite.equal_fix(m2(3,0),result2(0,1),1.0e-12) );
165}
166
167
168void test_quantile_normalize(test::Suite& suite)
169{
170  suite.err() << "Testing QuantileNormalizer\n";
171 
172  utility::Matrix m(2,2);
173  m(0,0) = 0;
174  m(0,1) = 10;
175  m(1,0) = 2;
176  m(1,1) = 4;
177  normalizer::QuantileNormalizer qn;
178  qn(m, m);
179  suite.err() << "Testing m(0,0)\n";
180  suite.add(suite.equal(m(0,0), 2));
181  suite.err() << "Testing m(0,1)\n";
182  suite.add(suite.equal(m(0,1), 6));
183  suite.err() << "Testing m(1,0)\n";
184  suite.add(suite.equal(m(1,0), 6));
185  suite.err() << "Testing m(1,1)\n";
186  suite.add(suite.equal(m(1,1), 2));
187}
188
189void test_row_normalize(test::Suite& suite)
190{
191  using namespace normalizer;
192  suite.err() << "Testing RowNormalizer\n";
193 
194  utility::Matrix m(2,3);
195  m(0,0) = 0;
196  m(0,1) = 10;
197  m(1,0) = 2;
198  m(1,1) = 4;
199  utility::Matrix m2(m);
200  m2.transpose();
201  ColumnNormalizer<Centralizer<> > cn;
202  RowNormalizer<Centralizer<> > rn;
203  cn(m, m);
204  rn(m2, m2);
205  m2.transpose();
206  suite.equal_range(m.begin(), m.end(), m2.begin());
207}
208
209void test_spearman(test::Suite& suite)
210{
211  suite.err() << "Testing Spearman\n";
212  normalizer::Spearman spearman;
213  std::vector<double> vec;
214  vec.push_back(0);
215  vec.push_back(2);
216  vec.push_back(3);
217  vec.push_back(1);
218  spearman(vec.begin(), vec.end(), vec.begin());
219  std::vector<double> correct;
220  correct.push_back(1.0/8);
221  correct.push_back(5.0/8);
222  correct.push_back(7.0/8);
223  correct.push_back(3.0/8);
224  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
225  suite.err() << "Testing Spearman with ties\n";
226  vec[1]=vec[2];
227  correct[1] = correct[2] = (correct[1]+correct[2])/2;
228  spearman(vec.begin(), vec.end(), vec.begin());
229  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
230  test_spearman_weighted(suite);
231}
232
233
234void test_gauss_normalize(test::Suite& suite)
235{
236  suite.err() << "Testing Gauss\n";
237  normalizer::Gauss gauss;
238  std::vector<double> vec;
239  vec.push_back(1);
240  gauss(vec.begin(), vec.end(), vec.begin());
241  suite.add(suite.equal(vec.front(), 0));
242  vec.push_back(1);
243  gauss(vec.begin(), vec.end(), vec.begin());
244  suite.add(suite.equal(vec.front(), -vec.back()));
245
246}
247
248void test_spearman_weighted(test::Suite& suite)
249{
250  suite.err() << "Testing Weighted Spearman\n";
251  normalizer::Spearman spearman;
252
253  suite.err() << "Testing that unity weights reproduces unweighted case\n";
254  utility::MatrixWeighted m(1,4,0,1);
255  utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0);
256  m(0,0).data()=0;
257  m(0,1).data()=2;
258  m(0,2).data()=3;
259  m(0,3).data()=1;
260  std::vector<double> correct(m.columns());
261  std::vector<double> correct_w(m.columns(), 1.0);
262  std::copy(utility::data_iterator(m.begin_row(0)),
263            utility::data_iterator(m.end_row(0)),
264            correct.begin());
265  spearman(correct.begin(), correct.end(), correct.begin());
266  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
267
268  using utility::data_iterator;
269  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
270                               data_iterator(res.end_row(0)),
271                               correct.begin()));
272  using utility::weight_iterator;
273  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
274                               weight_iterator(res.end_row(0)),
275                               correct_w.begin()));
276
277  suite.err() << "Testing rescaling of weights\n";
278  for (size_t i=0; i<m.columns(); ++i) {
279    m(0,i).weight() *= 2;
280    correct_w[i] *= 2;
281  }   
282  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
283  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
284                               data_iterator(res.end_row(0)),
285                               correct.begin()));
286  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
287                               weight_iterator(res.end_row(0)),
288                               correct_w.begin()));
289
290 
291  suite.err() << "Testing case with a zero weight\n";
292  m(0,1).data() = std::numeric_limits<double>::quiet_NaN();
293  m(0,1).weight() = 0.0;
294  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
295  suite.add(suite.equal(res(0,0).data(), 0.5/3)); 
296  suite.add(suite.equal(res(0,2).data(), 2.5/3)); 
297  suite.add(suite.equal(res(0,3).data(), 1.5/3)); 
298
299  suite.err() << "Testing case with ties\n";
300  m(0,0).data() = m(0,2).data();
301  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
302  suite.add(suite.equal(res(0,0).data(), 2.0/3)); 
303  suite.add(suite.equal(res(0,2).data(), 2.0/3)); 
304  suite.add(suite.equal(res(0,3).data(), 0.5/3)); 
305}
306
307void test_z_score(test::Suite& suite)
308{
309  suite.err() << "Testing Zscore\n";
310  std::vector<double> vec;
311  vec.push_back(0);
312  vec.push_back(3.14);
313  normalizer::Zscore zscore;
314  zscore(vec.begin(), vec.end(), vec.begin());
315  for (size_t i=0; i<vec.size(); ++i)
316    suite.add(suite.equal(vec[i], 2.0*i-1.0));
317
318  std::vector<utility::DataWeight> vec2;
319  vec2.push_back(utility::DataWeight(1,1));
320  vec2.push_back(utility::DataWeight(2.13,0.5));
321  vec2.push_back(utility::DataWeight(2.13,0.5));
322  std::vector<utility::DataWeight> vec3(vec2.size());
323  zscore(vec2.begin(), vec2.end(), vec3.begin());
324  for (size_t i=0; i<vec2.size(); ++i)
325    suite.add(suite.equal(vec3[i].weight(), vec2[i].weight()));
326  suite.add(suite.equal(vec3[0].data(), -1.0));
327  suite.add(suite.equal(vec3[1].data(), 1.0));
328  suite.add(suite.equal(vec3[2].data(), 1.0));
329}
330
331
Note: See TracBrowser for help on using the repository browser.