source: trunk/test/normalization_test.cc @ 1568

Last change on this file since 1568 was 1568, 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.5 KB
Line 
1// $Id: normalization_test.cc 1568 2008-10-13 18:01:47Z jari $
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/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/Matrix.h"
36#include "yat/utility/MatrixWeighted.h"
37#include "yat/utility/WeightIterator.h"
38
39#include <limits>
40#include <vector>
41
42using namespace theplu::yat;
43void test_centralizer(test::Suite&);
44void test_column_normalize(test::Suite&);
45void test_cubicspline_normalize(test::Suite&);
46void test_gauss_normalize(test::Suite&);
47void test_quantile_normalize(test::Suite&);
48void test_row_normalize(test::Suite&);
49void test_spearman(test::Suite&);
50void test_spearman_weighted(test::Suite&);
51void test_z_score(test::Suite&);
52
53int main(int argc, char* argv[])
54{ 
55  test::Suite suite(argc, argv);
56  suite.err() << "testing normalizations ... " << std::endl;
57
58  test_centralizer(suite);
59  test_column_normalize(suite);
60  test_cubicspline_normalize(suite);
61  test_quantile_normalize(suite);
62  test_gauss_normalize(suite);
63  test_row_normalize(suite);
64  test_spearman(suite);
65  test_z_score(suite);
66
67  return suite.return_value();
68}
69
70
71void test_centralizer(test::Suite& suite)
72{
73  suite.err() << "Testing Centralizer\n";
74  std::vector<double> vec;
75  vec.push_back(1);
76  vec.push_back(2);
77  vec.push_back(3);
78  normalizer::Centralizer<> c;
79  c(vec.begin(), vec.end(), vec.begin());
80  for (size_t i=0; i<vec.size(); ++i)
81    suite.add(suite.equal(vec[i], static_cast<double>(i)-1.0));
82
83  std::vector<utility::DataWeight> vec2;
84  vec2.push_back(utility::DataWeight(1,1));
85  vec2.push_back(utility::DataWeight(2,0.5));
86  vec2.push_back(utility::DataWeight(2,0.5));
87  std::vector<utility::DataWeight> vec3(vec2.size());
88  c(vec2.begin(), vec2.end(), vec3.begin());
89  for (size_t i=0; i<vec2.size(); ++i)
90    suite.add(suite.equal(vec3[i].weight(), vec2[i].weight()));
91  suite.add(suite.equal(vec3[0].data(), -0.5));
92  suite.add(suite.equal(vec3[1].data(), 0.5));
93  suite.add(suite.equal(vec3[2].data(), 0.5));
94
95}
96
97
98void test_column_normalize(test::Suite& suite)
99{
100  using namespace normalizer;
101  suite.err() << "Testing ColumnNormalizer\n";
102 
103  utility::Matrix m(2,2);
104  m(0,0) = 0;
105  m(0,1) = 10;
106  m(1,0) = 2;
107  m(1,1) = 4;
108  ColumnNormalizer<Centralizer<> > qn;
109  qn(m, m);
110  suite.err() << "Testing m(0,0)\n";
111  suite.add(suite.equal(m(0,0), -1));
112  suite.err() << "Testing m(0,1)\n";
113  suite.add(suite.equal(m(0,1), 3));
114  suite.err() << "Testing m(1,0)\n";
115  suite.add(suite.equal(m(1,0), 1));
116  suite.err() << "Testing m(1,1)\n";
117  suite.add(suite.equal(m(1,1), -3));
118}
119
120
121void test_cubicspline_normalize(test::Suite& suite)
122{
123  suite.err() << "Testing CubicSplineNormalizer\n";
124}
125
126
127void test_quantile_normalize(test::Suite& suite)
128{
129  suite.err() << "Testing QuantileNormalizer\n";
130 
131  utility::Matrix m(2,2);
132  m(0,0) = 0;
133  m(0,1) = 10;
134  m(1,0) = 2;
135  m(1,1) = 4;
136  normalizer::QuantileNormalizer qn;
137  qn(m, m);
138  suite.err() << "Testing m(0,0)\n";
139  suite.add(suite.equal(m(0,0), 2));
140  suite.err() << "Testing m(0,1)\n";
141  suite.add(suite.equal(m(0,1), 6));
142  suite.err() << "Testing m(1,0)\n";
143  suite.add(suite.equal(m(1,0), 6));
144  suite.err() << "Testing m(1,1)\n";
145  suite.add(suite.equal(m(1,1), 2));
146}
147
148void test_row_normalize(test::Suite& suite)
149{
150  using namespace normalizer;
151  suite.err() << "Testing RowNormalizer\n";
152 
153  utility::Matrix m(2,3);
154  m(0,0) = 0;
155  m(0,1) = 10;
156  m(1,0) = 2;
157  m(1,1) = 4;
158  utility::Matrix m2(m);
159  m2.transpose();
160  ColumnNormalizer<Centralizer<> > cn;
161  RowNormalizer<Centralizer<> > rn;
162  cn(m, m);
163  rn(m2, m2);
164  m2.transpose();
165  suite.equal_range(m.begin(), m.end(), m2.begin());
166}
167
168void test_spearman(test::Suite& suite)
169{
170  suite.err() << "Testing Spearman\n";
171  normalizer::Spearman spearman;
172  std::vector<double> vec;
173  vec.push_back(0);
174  vec.push_back(2);
175  vec.push_back(3);
176  vec.push_back(1);
177  spearman(vec.begin(), vec.end(), vec.begin());
178  std::vector<double> correct;
179  correct.push_back(1.0/8);
180  correct.push_back(5.0/8);
181  correct.push_back(7.0/8);
182  correct.push_back(3.0/8);
183  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
184  suite.err() << "Testing Spearman with ties\n";
185  vec[1]=vec[2];
186  correct[1] = correct[2] = (correct[1]+correct[2])/2;
187  spearman(vec.begin(), vec.end(), vec.begin());
188  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
189  test_spearman_weighted(suite);
190}
191
192
193void test_gauss_normalize(test::Suite& suite)
194{
195  suite.err() << "Testing Gauss\n";
196  normalizer::Gauss gauss;
197  std::vector<double> vec;
198  vec.push_back(1);
199  gauss(vec.begin(), vec.end(), vec.begin());
200  suite.add(suite.equal(vec.front(), 0));
201  vec.push_back(1);
202  gauss(vec.begin(), vec.end(), vec.begin());
203  suite.add(suite.equal(vec.front(), -vec.back()));
204
205}
206
207void test_spearman_weighted(test::Suite& suite)
208{
209  suite.err() << "Testing Weighted Spearman\n";
210  normalizer::Spearman spearman;
211
212  suite.err() << "Testing that unity weights reproduces unweighted case\n";
213  utility::MatrixWeighted m(1,4,0,1);
214  utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0);
215  m(0,0).data()=0;
216  m(0,1).data()=2;
217  m(0,2).data()=3;
218  m(0,3).data()=1;
219  std::vector<double> correct(m.columns());
220  std::vector<double> correct_w(m.columns(), 1.0);
221  std::copy(utility::data_iterator(m.begin_row(0)),
222            utility::data_iterator(m.end_row(0)),
223            correct.begin());
224  spearman(correct.begin(), correct.end(), correct.begin());
225  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
226
227  using utility::data_iterator;
228  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
229                               data_iterator(res.end_row(0)),
230                               correct.begin()));
231  using utility::weight_iterator;
232  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
233                               weight_iterator(res.end_row(0)),
234                               correct_w.begin()));
235
236  suite.err() << "Testing rescaling of weights\n";
237  for (size_t i=0; i<m.columns(); ++i) {
238    m(0,i).weight() *= 2;
239    correct_w[i] *= 2;
240  }   
241  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
242  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
243                               data_iterator(res.end_row(0)),
244                               correct.begin()));
245  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
246                               weight_iterator(res.end_row(0)),
247                               correct_w.begin()));
248
249 
250  suite.err() << "Testing case with a zero weight\n";
251  m(0,1).data() = std::numeric_limits<double>::quiet_NaN();
252  m(0,1).weight() = 0.0;
253  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
254  suite.add(suite.equal(res(0,0).data(), 0.5/3)); 
255  suite.add(suite.equal(res(0,2).data(), 2.5/3)); 
256  suite.add(suite.equal(res(0,3).data(), 1.5/3)); 
257
258  suite.err() << "Testing case with ties\n";
259  m(0,0).data() = m(0,2).data();
260  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
261  suite.add(suite.equal(res(0,0).data(), 2.0/3)); 
262  suite.add(suite.equal(res(0,2).data(), 2.0/3)); 
263  suite.add(suite.equal(res(0,3).data(), 0.5/3)); 
264}
265
266void test_z_score(test::Suite& suite)
267{
268  suite.err() << "Testing Zscore\n";
269  std::vector<double> vec;
270  vec.push_back(0);
271  vec.push_back(3.14);
272  normalizer::Zscore zscore;
273  zscore(vec.begin(), vec.end(), vec.begin());
274  for (size_t i=0; i<vec.size(); ++i)
275    suite.add(suite.equal(vec[i], 2.0*i-1.0));
276
277  std::vector<utility::DataWeight> vec2;
278  vec2.push_back(utility::DataWeight(1,1));
279  vec2.push_back(utility::DataWeight(2.13,0.5));
280  vec2.push_back(utility::DataWeight(2.13,0.5));
281  std::vector<utility::DataWeight> vec3(vec2.size());
282  zscore(vec2.begin(), vec2.end(), vec3.begin());
283  for (size_t i=0; i<vec2.size(); ++i)
284    suite.add(suite.equal(vec3[i].weight(), vec2[i].weight()));
285  suite.add(suite.equal(vec3[0].data(), -1.0));
286  suite.add(suite.equal(vec3[1].data(), 1.0));
287  suite.add(suite.equal(vec3[2].data(), 1.0));
288}
289
290
Note: See TracBrowser for help on using the repository browser.