source: trunk/test/normalization_test.cc @ 1511

Last change on this file since 1511 was 1511, checked in by Peter, 14 years ago

refs #437 modified test to reflect new spec

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1// $Id: normalization_test.cc 1511 2008-09-19 13:18:53Z 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/utility/DataIterator.h"
27#include "yat/normalizer/QuantileNormalizer.h"
28#include "yat/normalizer/Spearman.h"
29
30#include "yat/utility/DataIterator.h"
31#include "yat/utility/Matrix.h"
32#include "yat/utility/MatrixWeighted.h"
33#include "yat/utility/WeightIterator.h"
34
35#include <limits>
36#include <vector>
37
38using namespace theplu::yat;
39void test_centralizer(test::Suite&);
40void test_column_normalize(test::Suite&);
41void test_quantile_normalize(test::Suite&);
42void test_spearman(test::Suite&);
43void test_spearman_weighted(test::Suite&);
44
45int main(int argc, char* argv[])
46{ 
47  test::Suite suite(argc, argv);
48  suite.err() << "testing normalizations ... " << std::endl;
49
50  test_centralizer(suite);
51  test_column_normalize(suite);
52  test_quantile_normalize(suite);
53  test_spearman(suite);
54
55  return suite.return_value();
56}
57
58
59void test_centralizer(test::Suite& suite)
60{
61  suite.err() << "Testing Centralizer\n";
62  std::vector<double> vec;
63  vec.push_back(1);
64  vec.push_back(2);
65  vec.push_back(3);
66  normalizer::Centralizer<> c;
67  c(vec.begin(), vec.end(), vec.begin());
68  for (size_t i=0; i<vec.size(); ++i)
69    suite.add(suite.equal(vec[i], static_cast<double>(i)-1.0));
70}
71
72
73void test_column_normalize(test::Suite& suite)
74{
75  using namespace normalizer;
76  suite.err() << "Testing ColumnNormalizer\n";
77 
78  utility::Matrix m(2,2);
79  m(0,0) = 0;
80  m(0,1) = 10;
81  m(1,0) = 2;
82  m(1,1) = 4;
83  ColumnNormalizer<Centralizer<> > qn;
84  qn(m, m);
85  suite.err() << "Testing m(0,0)\n";
86  suite.add(suite.equal(m(0,0), -1));
87  suite.err() << "Testing m(0,1)\n";
88  suite.add(suite.equal(m(0,1), 3));
89  suite.err() << "Testing m(1,0)\n";
90  suite.add(suite.equal(m(1,0), 1));
91  suite.err() << "Testing m(1,1)\n";
92  suite.add(suite.equal(m(1,1), -3));
93}
94
95void test_quantile_normalize(test::Suite& suite)
96{
97  suite.err() << "Testing QuantileNormalizer\n";
98 
99  utility::Matrix m(2,2);
100  m(0,0) = 0;
101  m(0,1) = 10;
102  m(1,0) = 2;
103  m(1,1) = 4;
104  normalizer::QuantileNormalizer qn;
105  qn(m, m);
106  suite.err() << "Testing m(0,0)\n";
107  suite.add(suite.equal(m(0,0), 2));
108  suite.err() << "Testing m(0,1)\n";
109  suite.add(suite.equal(m(0,1), 6));
110  suite.err() << "Testing m(1,0)\n";
111  suite.add(suite.equal(m(1,0), 6));
112  suite.err() << "Testing m(1,1)\n";
113  suite.add(suite.equal(m(1,1), 2));
114}
115
116void test_spearman(test::Suite& suite)
117{
118  suite.err() << "Testing Spearman\n";
119  normalizer::Spearman spearman;
120  std::vector<double> vec;
121  vec.push_back(0);
122  vec.push_back(2);
123  vec.push_back(3);
124  vec.push_back(1);
125  spearman(vec.begin(), vec.end(), vec.begin());
126  std::vector<double> correct;
127  correct.push_back(1.0/8);
128  correct.push_back(5.0/8);
129  correct.push_back(7.0/8);
130  correct.push_back(3.0/8);
131  suite.xadd(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
132  suite.err() << "Testing Spearman with ties\n";
133  vec[1]=vec[2];
134  correct[1] = correct[2] = (correct[1]+correct[2])/2;
135  suite.xadd(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
136  test_spearman_weighted(suite);
137}
138
139
140void test_spearman_weighted(test::Suite& suite)
141{
142  suite.err() << "Testing Weighted Spearman\n";
143  normalizer::Spearman spearman;
144
145  suite.err() << "Testing that unity weights reproduces unweighted case\n";
146  utility::MatrixWeighted m(1,4,0,1);
147  utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0);
148  m(0,0).data()=0;
149  m(0,1).data()=2;
150  m(0,2).data()=3;
151  m(0,3).data()=1;
152  std::vector<double> correct(m.columns());
153  std::vector<double> correct_w(m.columns(), 1.0);
154  std::copy(utility::data_iterator(m.begin_row(0)),
155            utility::data_iterator(m.end_row(0)),
156            correct.begin());
157  spearman(correct.begin(), correct.end(), correct.begin());
158  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
159
160  using utility::data_iterator;
161  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
162                               data_iterator(res.end_row(0)),
163                               correct.begin()));
164  using utility::weight_iterator;
165  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
166                               weight_iterator(res.end_row(0)),
167                               correct_w.begin()));
168
169  suite.err() << "Testing rescaling of weights\n";
170  for (size_t i=0; i<m.columns(); ++i) {
171    m(0,i).weight() *= 2;
172    correct_w[i] *= 2;
173  }   
174  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
175  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
176                               data_iterator(res.end_row(0)),
177                               correct.begin()));
178  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
179                               weight_iterator(res.end_row(0)),
180                               correct_w.begin()));
181
182 
183  suite.err() << "Testing case with a zero weight\n";
184  m(0,1).data() = std::numeric_limits<double>::quiet_NaN();
185  m(0,1).weight() = 0.0;
186  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
187  suite.xadd(suite.equal(res(0,0).data(), 0.5/3)); 
188  suite.xadd(suite.equal(res(0,2).data(), 2.5/3)); 
189  suite.xadd(suite.equal(res(0,3).data(), 1.5/3)); 
190}
Note: See TracBrowser for help on using the repository browser.