source: trunk/test/normalization_test.cc @ 1708

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

Addresses #425

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.1 KB
RevLine 
[1432]1// $Id: normalization_test.cc 1708 2009-01-13 10:09:53Z jari $
2
3/*
[1571]4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
[1708]5  Copyright (C) 2009 Jari Häkkinen
[1432]6
[1469]7  This file is part of the yat library, http://dev.thep.lu.se/yat
[1432]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
[1486]11  published by the Free Software Foundation; either version 3 of the
[1432]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
[1487]20  along with yat. If not, see <http://www.gnu.org/licenses/>.
[1432]21*/
22
23#include "Suite.h"
24
[1497]25#include "yat/normalizer/Centralizer.h"
26#include "yat/normalizer/ColumnNormalizer.h"
[1536]27#include "yat/normalizer/Gauss.h"
[1708]28#include "yat/normalizer/qQuantileNormalizer.h"
[1497]29#include "yat/normalizer/QuantileNormalizer.h"
[1520]30#include "yat/normalizer/RowNormalizer.h"
[1497]31#include "yat/normalizer/Spearman.h"
[1521]32#include "yat/normalizer/Zscore.h"
[1432]33
[1509]34#include "yat/utility/DataIterator.h"
[1571]35#include "yat/utility/FileUtil.h"
[1432]36#include "yat/utility/Matrix.h"
[1509]37#include "yat/utility/MatrixWeighted.h"
38#include "yat/utility/WeightIterator.h"
[1432]39
[1571]40#include <fstream>
[1509]41#include <limits>
[1445]42#include <vector>
43
[1432]44using namespace theplu::yat;
[1445]45void test_centralizer(test::Suite&);
[1464]46void test_column_normalize(test::Suite&);
[1536]47void test_gauss_normalize(test::Suite&);
[1708]48void test_qquantile_normalize(test::Suite&);
[1432]49void test_quantile_normalize(test::Suite&);
[1520]50void test_row_normalize(test::Suite&);
[1496]51void test_spearman(test::Suite&);
[1509]52void test_spearman_weighted(test::Suite&);
[1521]53void test_z_score(test::Suite&);
[1432]54
55int main(int argc, char* argv[])
56{ 
57  test::Suite suite(argc, argv);
58  suite.err() << "testing normalizations ... " << std::endl;
59
[1445]60  test_centralizer(suite);
[1464]61  test_column_normalize(suite);
[1708]62  test_qquantile_normalize(suite);
[1432]63  test_quantile_normalize(suite);
[1536]64  test_gauss_normalize(suite);
[1520]65  test_row_normalize(suite);
[1496]66  test_spearman(suite);
[1521]67  test_z_score(suite);
[1432]68
69  return suite.return_value();
70}
71
72
[1445]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);
[1497]80  normalizer::Centralizer<> c;
[1445]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));
[1523]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
[1445]97}
98
99
[1463]100void test_column_normalize(test::Suite& suite)
101{
[1497]102  using namespace normalizer;
[1464]103  suite.err() << "Testing ColumnNormalizer\n";
[1463]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;
[1464]110  ColumnNormalizer<Centralizer<> > qn;
[1463]111  qn(m, m);
112  suite.err() << "Testing m(0,0)\n";
[1464]113  suite.add(suite.equal(m(0,0), -1));
[1463]114  suite.err() << "Testing m(0,1)\n";
[1464]115  suite.add(suite.equal(m(0,1), 3));
[1463]116  suite.err() << "Testing m(1,0)\n";
[1464]117  suite.add(suite.equal(m(1,0), 1));
[1463]118  suite.err() << "Testing m(1,1)\n";
[1464]119  suite.add(suite.equal(m(1,1), -3));
[1463]120}
121
[1568]122
[1708]123void test_qquantile_normalize(test::Suite& suite)
[1568]124{
[1708]125  using namespace normalizer;
126
127  suite.err() << "Testing qQuantileNormalizer\n";
128  std::string data(test::filename("data/normalization_test.data"));
[1571]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);
[1708]137  utility::Matrix result(m.rows(),m.columns());
138
139  // testing number of parts boundary conditions
140  qQuantileNormalizer(m.column_const_view(0),m.rows());
141  qQuantileNormalizer(m.column_const_view(0),2);
142
143  qQuantileNormalizer qqn(m.column_const_view(0),9);  // first column as target
144  qqn(m,result);
[1568]145}
146
147
[1432]148void test_quantile_normalize(test::Suite& suite)
149{
[1445]150  suite.err() << "Testing QuantileNormalizer\n";
[1432]151 
152  utility::Matrix m(2,2);
153  m(0,0) = 0;
154  m(0,1) = 10;
155  m(1,0) = 2;
156  m(1,1) = 4;
[1497]157  normalizer::QuantileNormalizer qn;
[1447]158  qn(m, m);
[1432]159  suite.err() << "Testing m(0,0)\n";
160  suite.add(suite.equal(m(0,0), 2));
161  suite.err() << "Testing m(0,1)\n";
162  suite.add(suite.equal(m(0,1), 6));
163  suite.err() << "Testing m(1,0)\n";
164  suite.add(suite.equal(m(1,0), 6));
165  suite.err() << "Testing m(1,1)\n";
166  suite.add(suite.equal(m(1,1), 2));
167}
168
[1520]169void test_row_normalize(test::Suite& suite)
170{
171  using namespace normalizer;
172  suite.err() << "Testing RowNormalizer\n";
173 
174  utility::Matrix m(2,3);
175  m(0,0) = 0;
176  m(0,1) = 10;
177  m(1,0) = 2;
178  m(1,1) = 4;
179  utility::Matrix m2(m);
180  m2.transpose();
181  ColumnNormalizer<Centralizer<> > cn;
182  RowNormalizer<Centralizer<> > rn;
183  cn(m, m);
184  rn(m2, m2);
185  m2.transpose();
186  suite.equal_range(m.begin(), m.end(), m2.begin());
187}
188
[1496]189void test_spearman(test::Suite& suite)
190{
191  suite.err() << "Testing Spearman\n";
[1497]192  normalizer::Spearman spearman;
[1496]193  std::vector<double> vec;
194  vec.push_back(0);
195  vec.push_back(2);
196  vec.push_back(3);
197  vec.push_back(1);
198  spearman(vec.begin(), vec.end(), vec.begin());
199  std::vector<double> correct;
[1511]200  correct.push_back(1.0/8);
201  correct.push_back(5.0/8);
202  correct.push_back(7.0/8);
203  correct.push_back(3.0/8);
[1512]204  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
[1511]205  suite.err() << "Testing Spearman with ties\n";
206  vec[1]=vec[2];
207  correct[1] = correct[2] = (correct[1]+correct[2])/2;
[1512]208  spearman(vec.begin(), vec.end(), vec.begin());
209  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
[1509]210  test_spearman_weighted(suite);
[1496]211}
[1509]212
213
[1536]214void test_gauss_normalize(test::Suite& suite)
215{
216  suite.err() << "Testing Gauss\n";
217  normalizer::Gauss gauss;
218  std::vector<double> vec;
219  vec.push_back(1);
220  gauss(vec.begin(), vec.end(), vec.begin());
221  suite.add(suite.equal(vec.front(), 0));
222  vec.push_back(1);
223  gauss(vec.begin(), vec.end(), vec.begin());
224  suite.add(suite.equal(vec.front(), -vec.back()));
225
226}
227
[1509]228void test_spearman_weighted(test::Suite& suite)
229{
230  suite.err() << "Testing Weighted Spearman\n";
231  normalizer::Spearman spearman;
232
233  suite.err() << "Testing that unity weights reproduces unweighted case\n";
234  utility::MatrixWeighted m(1,4,0,1);
235  utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0);
236  m(0,0).data()=0;
237  m(0,1).data()=2;
238  m(0,2).data()=3;
239  m(0,3).data()=1;
240  std::vector<double> correct(m.columns());
241  std::vector<double> correct_w(m.columns(), 1.0);
242  std::copy(utility::data_iterator(m.begin_row(0)),
243            utility::data_iterator(m.end_row(0)),
244            correct.begin());
245  spearman(correct.begin(), correct.end(), correct.begin());
246  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
247
248  using utility::data_iterator;
249  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
250                               data_iterator(res.end_row(0)),
251                               correct.begin()));
252  using utility::weight_iterator;
253  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
254                               weight_iterator(res.end_row(0)),
255                               correct_w.begin()));
256
257  suite.err() << "Testing rescaling of weights\n";
258  for (size_t i=0; i<m.columns(); ++i) {
259    m(0,i).weight() *= 2;
260    correct_w[i] *= 2;
261  }   
262  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
263  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
264                               data_iterator(res.end_row(0)),
265                               correct.begin()));
266  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
267                               weight_iterator(res.end_row(0)),
268                               correct_w.begin()));
269
270 
271  suite.err() << "Testing case with a zero weight\n";
272  m(0,1).data() = std::numeric_limits<double>::quiet_NaN();
273  m(0,1).weight() = 0.0;
274  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
[1512]275  suite.add(suite.equal(res(0,0).data(), 0.5/3)); 
276  suite.add(suite.equal(res(0,2).data(), 2.5/3)); 
277  suite.add(suite.equal(res(0,3).data(), 1.5/3)); 
278
279  suite.err() << "Testing case with ties\n";
280  m(0,0).data() = m(0,2).data();
281  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
282  suite.add(suite.equal(res(0,0).data(), 2.0/3)); 
283  suite.add(suite.equal(res(0,2).data(), 2.0/3)); 
284  suite.add(suite.equal(res(0,3).data(), 0.5/3)); 
[1509]285}
[1521]286
287void test_z_score(test::Suite& suite)
288{
289  suite.err() << "Testing Zscore\n";
290  std::vector<double> vec;
291  vec.push_back(0);
292  vec.push_back(3.14);
293  normalizer::Zscore zscore;
294  zscore(vec.begin(), vec.end(), vec.begin());
295  for (size_t i=0; i<vec.size(); ++i)
296    suite.add(suite.equal(vec[i], 2.0*i-1.0));
[1524]297
298  std::vector<utility::DataWeight> vec2;
299  vec2.push_back(utility::DataWeight(1,1));
300  vec2.push_back(utility::DataWeight(2.13,0.5));
301  vec2.push_back(utility::DataWeight(2.13,0.5));
302  std::vector<utility::DataWeight> vec3(vec2.size());
303  zscore(vec2.begin(), vec2.end(), vec3.begin());
304  for (size_t i=0; i<vec2.size(); ++i)
305    suite.add(suite.equal(vec3[i].weight(), vec2[i].weight()));
306  suite.add(suite.equal(vec3[0].data(), -1.0));
307  suite.add(suite.equal(vec3[1].data(), 1.0));
308  suite.add(suite.equal(vec3[2].data(), 1.0));
[1521]309}
310
311
Note: See TracBrowser for help on using the repository browser.