source: trunk/test/normalization_test.cc @ 1536

Last change on this file since 1536 was 1536, checked in by Peter, 13 years ago

adding a Gauss normalizer

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