source: trunk/test/normalization_test.cc @ 1619

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

Removing duplicate include statement.

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