source: trunk/test/normalization_test.cc @ 1571

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

Addresses #425.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1// $Id: normalization_test.cc 1571 2008-10-13 21:18:54Z jari $
2
3/*
4  Copyright (C) 2008 Jari Häkkinen, 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/normalizer/CubicSplineNormalizer.h"
27#include "yat/normalizer/Gauss.h"
28#include "yat/normalizer/QuantileNormalizer.h"
29#include "yat/normalizer/RowNormalizer.h"
30#include "yat/normalizer/Spearman.h"
31#include "yat/normalizer/Zscore.h"
32#include "yat/utility/DataIterator.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_cubicspline_normalize(test::Suite&);
48void test_gauss_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_cubicspline_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_cubicspline_normalize(test::Suite& suite)
124{
125  suite.err() << "Testing CubicSplineNormalizer\n";
126  std::string data(test::filename("data/cubicspline.data"));
127  if (utility::FileUtil(data.c_str()).permissions("r")) {
128    suite.add(false);
129    suite.err() << "Cannot access file " << data << '\n';
130    return;
131  }
132  std::ifstream data_stream(data.c_str());
133
134  utility::Matrix m(data_stream);
135}
136
137
138void test_quantile_normalize(test::Suite& suite)
139{
140  suite.err() << "Testing QuantileNormalizer\n";
141 
142  utility::Matrix m(2,2);
143  m(0,0) = 0;
144  m(0,1) = 10;
145  m(1,0) = 2;
146  m(1,1) = 4;
147  normalizer::QuantileNormalizer qn;
148  qn(m, m);
149  suite.err() << "Testing m(0,0)\n";
150  suite.add(suite.equal(m(0,0), 2));
151  suite.err() << "Testing m(0,1)\n";
152  suite.add(suite.equal(m(0,1), 6));
153  suite.err() << "Testing m(1,0)\n";
154  suite.add(suite.equal(m(1,0), 6));
155  suite.err() << "Testing m(1,1)\n";
156  suite.add(suite.equal(m(1,1), 2));
157}
158
159void test_row_normalize(test::Suite& suite)
160{
161  using namespace normalizer;
162  suite.err() << "Testing RowNormalizer\n";
163 
164  utility::Matrix m(2,3);
165  m(0,0) = 0;
166  m(0,1) = 10;
167  m(1,0) = 2;
168  m(1,1) = 4;
169  utility::Matrix m2(m);
170  m2.transpose();
171  ColumnNormalizer<Centralizer<> > cn;
172  RowNormalizer<Centralizer<> > rn;
173  cn(m, m);
174  rn(m2, m2);
175  m2.transpose();
176  suite.equal_range(m.begin(), m.end(), m2.begin());
177}
178
179void test_spearman(test::Suite& suite)
180{
181  suite.err() << "Testing Spearman\n";
182  normalizer::Spearman spearman;
183  std::vector<double> vec;
184  vec.push_back(0);
185  vec.push_back(2);
186  vec.push_back(3);
187  vec.push_back(1);
188  spearman(vec.begin(), vec.end(), vec.begin());
189  std::vector<double> correct;
190  correct.push_back(1.0/8);
191  correct.push_back(5.0/8);
192  correct.push_back(7.0/8);
193  correct.push_back(3.0/8);
194  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
195  suite.err() << "Testing Spearman with ties\n";
196  vec[1]=vec[2];
197  correct[1] = correct[2] = (correct[1]+correct[2])/2;
198  spearman(vec.begin(), vec.end(), vec.begin());
199  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
200  test_spearman_weighted(suite);
201}
202
203
204void test_gauss_normalize(test::Suite& suite)
205{
206  suite.err() << "Testing Gauss\n";
207  normalizer::Gauss gauss;
208  std::vector<double> vec;
209  vec.push_back(1);
210  gauss(vec.begin(), vec.end(), vec.begin());
211  suite.add(suite.equal(vec.front(), 0));
212  vec.push_back(1);
213  gauss(vec.begin(), vec.end(), vec.begin());
214  suite.add(suite.equal(vec.front(), -vec.back()));
215
216}
217
218void test_spearman_weighted(test::Suite& suite)
219{
220  suite.err() << "Testing Weighted Spearman\n";
221  normalizer::Spearman spearman;
222
223  suite.err() << "Testing that unity weights reproduces unweighted case\n";
224  utility::MatrixWeighted m(1,4,0,1);
225  utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0);
226  m(0,0).data()=0;
227  m(0,1).data()=2;
228  m(0,2).data()=3;
229  m(0,3).data()=1;
230  std::vector<double> correct(m.columns());
231  std::vector<double> correct_w(m.columns(), 1.0);
232  std::copy(utility::data_iterator(m.begin_row(0)),
233            utility::data_iterator(m.end_row(0)),
234            correct.begin());
235  spearman(correct.begin(), correct.end(), correct.begin());
236  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
237
238  using utility::data_iterator;
239  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
240                               data_iterator(res.end_row(0)),
241                               correct.begin()));
242  using utility::weight_iterator;
243  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
244                               weight_iterator(res.end_row(0)),
245                               correct_w.begin()));
246
247  suite.err() << "Testing rescaling of weights\n";
248  for (size_t i=0; i<m.columns(); ++i) {
249    m(0,i).weight() *= 2;
250    correct_w[i] *= 2;
251  }   
252  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
253  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
254                               data_iterator(res.end_row(0)),
255                               correct.begin()));
256  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
257                               weight_iterator(res.end_row(0)),
258                               correct_w.begin()));
259
260 
261  suite.err() << "Testing case with a zero weight\n";
262  m(0,1).data() = std::numeric_limits<double>::quiet_NaN();
263  m(0,1).weight() = 0.0;
264  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
265  suite.add(suite.equal(res(0,0).data(), 0.5/3)); 
266  suite.add(suite.equal(res(0,2).data(), 2.5/3)); 
267  suite.add(suite.equal(res(0,3).data(), 1.5/3)); 
268
269  suite.err() << "Testing case with ties\n";
270  m(0,0).data() = m(0,2).data();
271  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
272  suite.add(suite.equal(res(0,0).data(), 2.0/3)); 
273  suite.add(suite.equal(res(0,2).data(), 2.0/3)); 
274  suite.add(suite.equal(res(0,3).data(), 0.5/3)); 
275}
276
277void test_z_score(test::Suite& suite)
278{
279  suite.err() << "Testing Zscore\n";
280  std::vector<double> vec;
281  vec.push_back(0);
282  vec.push_back(3.14);
283  normalizer::Zscore zscore;
284  zscore(vec.begin(), vec.end(), vec.begin());
285  for (size_t i=0; i<vec.size(); ++i)
286    suite.add(suite.equal(vec[i], 2.0*i-1.0));
287
288  std::vector<utility::DataWeight> vec2;
289  vec2.push_back(utility::DataWeight(1,1));
290  vec2.push_back(utility::DataWeight(2.13,0.5));
291  vec2.push_back(utility::DataWeight(2.13,0.5));
292  std::vector<utility::DataWeight> vec3(vec2.size());
293  zscore(vec2.begin(), vec2.end(), vec3.begin());
294  for (size_t i=0; i<vec2.size(); ++i)
295    suite.add(suite.equal(vec3[i].weight(), vec2[i].weight()));
296  suite.add(suite.equal(vec3[0].data(), -1.0));
297  suite.add(suite.equal(vec3[1].data(), 1.0));
298  suite.add(suite.equal(vec3[2].data(), 1.0));
299}
300
301
Note: See TracBrowser for help on using the repository browser.