source: trunk/test/normalization_test.cc @ 1520

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

adding a RowNormalizer?

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