1432 |
25 Aug 08 |
peter |
// $Id$ |
1432 |
25 Aug 08 |
peter |
2 |
|
1432 |
25 Aug 08 |
peter |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2008, 2009 Jari Häkkinen, Peter Johansson |
4207 |
26 Aug 22 |
peter |
Copyright (C) 2010, 2012, 2014, 2016, 2020, 2022 Peter Johansson |
1432 |
25 Aug 08 |
peter |
6 |
|
1469 |
02 Sep 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
1432 |
25 Aug 08 |
peter |
8 |
|
1432 |
25 Aug 08 |
peter |
The yat library is free software; you can redistribute it and/or |
1432 |
25 Aug 08 |
peter |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
1432 |
25 Aug 08 |
peter |
License, or (at your option) any later version. |
1432 |
25 Aug 08 |
peter |
13 |
|
1432 |
25 Aug 08 |
peter |
The yat library is distributed in the hope that it will be useful, |
1432 |
25 Aug 08 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1432 |
25 Aug 08 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1432 |
25 Aug 08 |
peter |
General Public License for more details. |
1432 |
25 Aug 08 |
peter |
18 |
|
1432 |
25 Aug 08 |
peter |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
1432 |
25 Aug 08 |
peter |
21 |
*/ |
1432 |
25 Aug 08 |
peter |
22 |
|
4252 |
18 Nov 22 |
peter |
// avoid deprecation warning |
4252 |
18 Nov 22 |
peter |
24 |
#define YAT_DEPRECATE |
4252 |
18 Nov 22 |
peter |
25 |
|
2881 |
18 Nov 12 |
peter |
26 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
27 |
|
1432 |
25 Aug 08 |
peter |
28 |
#include "Suite.h" |
1432 |
25 Aug 08 |
peter |
29 |
|
1497 |
12 Sep 08 |
peter |
30 |
#include "yat/normalizer/Centralizer.h" |
1497 |
12 Sep 08 |
peter |
31 |
#include "yat/normalizer/ColumnNormalizer.h" |
1536 |
25 Sep 08 |
peter |
32 |
#include "yat/normalizer/Gauss.h" |
1708 |
13 Jan 09 |
jari |
33 |
#include "yat/normalizer/qQuantileNormalizer.h" |
1497 |
12 Sep 08 |
peter |
34 |
#include "yat/normalizer/QuantileNormalizer.h" |
4185 |
04 Jul 22 |
peter |
35 |
#include "yat/normalizer/QuantileNormalizer2.h" |
1520 |
21 Sep 08 |
peter |
36 |
#include "yat/normalizer/RowNormalizer.h" |
1497 |
12 Sep 08 |
peter |
37 |
#include "yat/normalizer/Spearman.h" |
3957 |
26 Jul 20 |
peter |
38 |
#include "yat/normalizer/UnityScaler.h" |
1521 |
21 Sep 08 |
peter |
39 |
#include "yat/normalizer/Zscore.h" |
1432 |
25 Aug 08 |
peter |
40 |
|
4188 |
18 Jul 22 |
peter |
41 |
#include "yat/random/random.h" |
4188 |
18 Jul 22 |
peter |
42 |
|
1509 |
17 Sep 08 |
peter |
43 |
#include "yat/utility/DataIterator.h" |
1571 |
13 Oct 08 |
jari |
44 |
#include "yat/utility/FileUtil.h" |
1432 |
25 Aug 08 |
peter |
45 |
#include "yat/utility/Matrix.h" |
4185 |
04 Jul 22 |
peter |
46 |
#include "yat/utility/MatrixConstView.h" |
1509 |
17 Sep 08 |
peter |
47 |
#include "yat/utility/MatrixWeighted.h" |
3957 |
26 Jul 20 |
peter |
48 |
#include "yat/utility/Vector.h" |
1509 |
17 Sep 08 |
peter |
49 |
#include "yat/utility/WeightIterator.h" |
3541 |
23 Dec 16 |
peter |
50 |
#include "yat/utility/WeightedIteratorArchetype.h" |
1432 |
25 Aug 08 |
peter |
51 |
|
2146 |
15 Jan 10 |
peter |
52 |
#include <boost/concept_archetype.hpp> |
3281 |
08 Jul 14 |
peter |
53 |
#include <boost/iterator/iterator_archetypes.hpp> |
2146 |
15 Jan 10 |
peter |
54 |
|
3957 |
26 Jul 20 |
peter |
55 |
#include <algorithm> |
1784 |
08 Feb 09 |
peter |
56 |
#include <climits> |
1571 |
13 Oct 08 |
jari |
57 |
#include <fstream> |
3281 |
08 Jul 14 |
peter |
58 |
#include <iterator> |
1509 |
17 Sep 08 |
peter |
59 |
#include <limits> |
1445 |
27 Aug 08 |
peter |
60 |
#include <vector> |
1445 |
27 Aug 08 |
peter |
61 |
|
1432 |
25 Aug 08 |
peter |
62 |
using namespace theplu::yat; |
1445 |
27 Aug 08 |
peter |
63 |
void test_centralizer(test::Suite&); |
1464 |
01 Sep 08 |
peter |
64 |
void test_column_normalize(test::Suite&); |
1536 |
25 Sep 08 |
peter |
65 |
void test_gauss_normalize(test::Suite&); |
1708 |
13 Jan 09 |
jari |
66 |
void test_qquantile_normalize(test::Suite&); |
1737 |
19 Jan 09 |
peter |
67 |
void test_qquantile_normalize_weighted(test::Suite&); |
1432 |
25 Aug 08 |
peter |
68 |
void test_quantile_normalize(test::Suite&); |
4185 |
04 Jul 22 |
peter |
69 |
void test_quantile_normalizer2(test::Suite&); |
1520 |
21 Sep 08 |
peter |
70 |
void test_row_normalize(test::Suite&); |
1496 |
12 Sep 08 |
peter |
71 |
void test_spearman(test::Suite&); |
1509 |
17 Sep 08 |
peter |
72 |
void test_spearman_weighted(test::Suite&); |
3957 |
26 Jul 20 |
peter |
73 |
void test_unity_scaler(test::Suite&); |
1521 |
21 Sep 08 |
peter |
74 |
void test_z_score(test::Suite&); |
1432 |
25 Aug 08 |
peter |
75 |
|
1432 |
25 Aug 08 |
peter |
76 |
int main(int argc, char* argv[]) |
3281 |
08 Jul 14 |
peter |
77 |
{ |
1432 |
25 Aug 08 |
peter |
78 |
test::Suite suite(argc, argv); |
1432 |
25 Aug 08 |
peter |
79 |
suite.err() << "testing normalizations ... " << std::endl; |
1432 |
25 Aug 08 |
peter |
80 |
|
1445 |
27 Aug 08 |
peter |
81 |
test_centralizer(suite); |
1464 |
01 Sep 08 |
peter |
82 |
test_column_normalize(suite); |
1708 |
13 Jan 09 |
jari |
83 |
test_qquantile_normalize(suite); |
1737 |
19 Jan 09 |
peter |
84 |
test_qquantile_normalize_weighted(suite); |
1432 |
25 Aug 08 |
peter |
85 |
test_quantile_normalize(suite); |
4185 |
04 Jul 22 |
peter |
86 |
test_quantile_normalizer2(suite); |
1536 |
25 Sep 08 |
peter |
87 |
test_gauss_normalize(suite); |
1520 |
21 Sep 08 |
peter |
88 |
test_row_normalize(suite); |
1496 |
12 Sep 08 |
peter |
89 |
test_spearman(suite); |
3957 |
26 Jul 20 |
peter |
90 |
test_unity_scaler(suite); |
1521 |
21 Sep 08 |
peter |
91 |
test_z_score(suite); |
1432 |
25 Aug 08 |
peter |
92 |
|
1432 |
25 Aug 08 |
peter |
93 |
return suite.return_value(); |
1432 |
25 Aug 08 |
peter |
94 |
} |
1432 |
25 Aug 08 |
peter |
95 |
|
1432 |
25 Aug 08 |
peter |
96 |
|
1445 |
27 Aug 08 |
peter |
97 |
void test_centralizer(test::Suite& suite) |
1445 |
27 Aug 08 |
peter |
98 |
{ |
1445 |
27 Aug 08 |
peter |
99 |
suite.err() << "Testing Centralizer\n"; |
1445 |
27 Aug 08 |
peter |
100 |
std::vector<double> vec; |
1445 |
27 Aug 08 |
peter |
101 |
vec.push_back(1); |
1445 |
27 Aug 08 |
peter |
102 |
vec.push_back(2); |
1445 |
27 Aug 08 |
peter |
103 |
vec.push_back(3); |
1497 |
12 Sep 08 |
peter |
104 |
normalizer::Centralizer<> c; |
1445 |
27 Aug 08 |
peter |
105 |
c(vec.begin(), vec.end(), vec.begin()); |
1445 |
27 Aug 08 |
peter |
106 |
for (size_t i=0; i<vec.size(); ++i) |
1445 |
27 Aug 08 |
peter |
107 |
suite.add(suite.equal(vec[i], static_cast<double>(i)-1.0)); |
1523 |
23 Sep 08 |
peter |
108 |
|
1523 |
23 Sep 08 |
peter |
109 |
std::vector<utility::DataWeight> vec2; |
1523 |
23 Sep 08 |
peter |
110 |
vec2.push_back(utility::DataWeight(1,1)); |
1523 |
23 Sep 08 |
peter |
111 |
vec2.push_back(utility::DataWeight(2,0.5)); |
1523 |
23 Sep 08 |
peter |
112 |
vec2.push_back(utility::DataWeight(2,0.5)); |
1523 |
23 Sep 08 |
peter |
113 |
std::vector<utility::DataWeight> vec3(vec2.size()); |
1523 |
23 Sep 08 |
peter |
114 |
c(vec2.begin(), vec2.end(), vec3.begin()); |
1523 |
23 Sep 08 |
peter |
115 |
for (size_t i=0; i<vec2.size(); ++i) |
1523 |
23 Sep 08 |
peter |
116 |
suite.add(suite.equal(vec3[i].weight(), vec2[i].weight())); |
1523 |
23 Sep 08 |
peter |
117 |
suite.add(suite.equal(vec3[0].data(), -0.5)); |
1523 |
23 Sep 08 |
peter |
118 |
suite.add(suite.equal(vec3[1].data(), 0.5)); |
1523 |
23 Sep 08 |
peter |
119 |
suite.add(suite.equal(vec3[2].data(), 0.5)); |
1523 |
23 Sep 08 |
peter |
120 |
|
2146 |
15 Jan 10 |
peter |
// compile test should not be run |
2146 |
15 Jan 10 |
peter |
122 |
if (false) { |
3543 |
23 Dec 16 |
peter |
123 |
typedef test::DataIterator<boost::forward_traversal_tag> trait; |
3543 |
23 Dec 16 |
peter |
124 |
trait::unweighted_const_iterator unweighted_const_iterator; |
3543 |
23 Dec 16 |
peter |
125 |
trait::unweighted_iterator unweighted_mutable_iterator; |
3543 |
23 Dec 16 |
peter |
126 |
trait::weighted_const_iterator weighted_const_iterator; |
3543 |
23 Dec 16 |
peter |
127 |
trait::weighted_iterator weighted_mutable_iterator; |
3281 |
08 Jul 14 |
peter |
128 |
|
3541 |
23 Dec 16 |
peter |
129 |
c(unweighted_const_iterator, unweighted_const_iterator, |
3541 |
23 Dec 16 |
peter |
130 |
unweighted_mutable_iterator); |
3541 |
23 Dec 16 |
peter |
131 |
c(unweighted_const_iterator, unweighted_const_iterator, |
3541 |
23 Dec 16 |
peter |
132 |
weighted_mutable_iterator); |
3541 |
23 Dec 16 |
peter |
133 |
c(weighted_const_iterator, weighted_const_iterator, |
3541 |
23 Dec 16 |
peter |
134 |
unweighted_mutable_iterator); |
3541 |
23 Dec 16 |
peter |
135 |
c(weighted_const_iterator, weighted_const_iterator, |
3541 |
23 Dec 16 |
peter |
136 |
weighted_mutable_iterator); |
3953 |
22 Jul 20 |
peter |
137 |
|
3953 |
22 Jul 20 |
peter |
138 |
statistics::Average average; |
3953 |
22 Jul 20 |
peter |
139 |
normalizer::Centralizer<statistics::Average> c2(average); |
3953 |
22 Jul 20 |
peter |
140 |
test::avoid_compiler_warning(c2); |
2146 |
15 Jan 10 |
peter |
141 |
} |
1445 |
27 Aug 08 |
peter |
142 |
} |
1445 |
27 Aug 08 |
peter |
143 |
|
1445 |
27 Aug 08 |
peter |
144 |
|
1463 |
01 Sep 08 |
peter |
145 |
void test_column_normalize(test::Suite& suite) |
1463 |
01 Sep 08 |
peter |
146 |
{ |
1497 |
12 Sep 08 |
peter |
147 |
using namespace normalizer; |
1464 |
01 Sep 08 |
peter |
148 |
suite.err() << "Testing ColumnNormalizer\n"; |
3281 |
08 Jul 14 |
peter |
149 |
|
1463 |
01 Sep 08 |
peter |
150 |
utility::Matrix m(2,2); |
1463 |
01 Sep 08 |
peter |
151 |
m(0,0) = 0; |
1463 |
01 Sep 08 |
peter |
152 |
m(0,1) = 10; |
1463 |
01 Sep 08 |
peter |
153 |
m(1,0) = 2; |
1463 |
01 Sep 08 |
peter |
154 |
m(1,1) = 4; |
1464 |
01 Sep 08 |
peter |
155 |
ColumnNormalizer<Centralizer<> > qn; |
1463 |
01 Sep 08 |
peter |
156 |
qn(m, m); |
1463 |
01 Sep 08 |
peter |
157 |
suite.err() << "Testing m(0,0)\n"; |
1464 |
01 Sep 08 |
peter |
158 |
suite.add(suite.equal(m(0,0), -1)); |
1463 |
01 Sep 08 |
peter |
159 |
suite.err() << "Testing m(0,1)\n"; |
1464 |
01 Sep 08 |
peter |
160 |
suite.add(suite.equal(m(0,1), 3)); |
1463 |
01 Sep 08 |
peter |
161 |
suite.err() << "Testing m(1,0)\n"; |
1464 |
01 Sep 08 |
peter |
162 |
suite.add(suite.equal(m(1,0), 1)); |
1463 |
01 Sep 08 |
peter |
163 |
suite.err() << "Testing m(1,1)\n"; |
1464 |
01 Sep 08 |
peter |
164 |
suite.add(suite.equal(m(1,1), -3)); |
2147 |
17 Jan 10 |
peter |
165 |
|
2147 |
17 Jan 10 |
peter |
166 |
if (false) { // do not run compile tests |
3543 |
23 Dec 16 |
peter |
167 |
test::container2d_archetype<double> container2d; |
3543 |
23 Dec 16 |
peter |
168 |
test::mutable_container2d_archetype<double> mutable_container2d; |
2147 |
17 Jan 10 |
peter |
169 |
qn(container2d, mutable_container2d); |
2147 |
17 Jan 10 |
peter |
170 |
} |
1463 |
01 Sep 08 |
peter |
171 |
} |
1463 |
01 Sep 08 |
peter |
172 |
|
1568 |
13 Oct 08 |
jari |
173 |
|
1708 |
13 Jan 09 |
jari |
174 |
void test_qquantile_normalize(test::Suite& suite) |
1568 |
13 Oct 08 |
jari |
175 |
{ |
1708 |
13 Jan 09 |
jari |
176 |
using namespace normalizer; |
1708 |
13 Jan 09 |
jari |
177 |
|
1708 |
13 Jan 09 |
jari |
178 |
suite.err() << "Testing qQuantileNormalizer\n"; |
1708 |
13 Jan 09 |
jari |
179 |
std::string data(test::filename("data/normalization_test.data")); |
1571 |
13 Oct 08 |
jari |
180 |
if (utility::FileUtil(data.c_str()).permissions("r")) { |
1571 |
13 Oct 08 |
jari |
181 |
suite.add(false); |
1571 |
13 Oct 08 |
jari |
182 |
suite.err() << "Cannot access file " << data << '\n'; |
1571 |
13 Oct 08 |
jari |
183 |
return; |
1571 |
13 Oct 08 |
jari |
184 |
} |
1571 |
13 Oct 08 |
jari |
185 |
std::ifstream data_stream(data.c_str()); |
1571 |
13 Oct 08 |
jari |
186 |
|
1571 |
13 Oct 08 |
jari |
187 |
utility::Matrix m(data_stream); |
1708 |
13 Jan 09 |
jari |
188 |
|
1719 |
14 Jan 09 |
jari |
189 |
suite.err() << "testing number of parts (Q) boundary conditions\n"; |
1736 |
19 Jan 09 |
peter |
190 |
qQuantileNormalizer(m.begin_column(0), m.end_column(0), m.rows()); |
1736 |
19 Jan 09 |
peter |
191 |
qQuantileNormalizer(m.begin_column(0), m.end_column(0), 3); |
1708 |
13 Jan 09 |
jari |
192 |
|
1736 |
19 Jan 09 |
peter |
// first column as target |
3543 |
23 Dec 16 |
peter |
194 |
qQuantileNormalizer qqn(m.begin_column(0), m.end_column(0) ,9); |
1736 |
19 Jan 09 |
peter |
195 |
ColumnNormalizer<qQuantileNormalizer> cn(qqn); |
1719 |
14 Jan 09 |
jari |
196 |
utility::Matrix result(m.rows(),m.columns()); |
1736 |
19 Jan 09 |
peter |
197 |
cn(m, result); |
1719 |
14 Jan 09 |
jari |
198 |
|
1775 |
05 Feb 09 |
peter |
199 |
suite.err() << "test that result can be stored in the source matrix..."; |
1736 |
19 Jan 09 |
peter |
200 |
cn(m,m); |
1775 |
05 Feb 09 |
peter |
201 |
if (suite.add(result==m)) |
1775 |
05 Feb 09 |
peter |
202 |
suite.err() << " ok.\n"; |
3543 |
23 Dec 16 |
peter |
203 |
else |
1775 |
05 Feb 09 |
peter |
204 |
suite.err() << " failed.\n"; |
1719 |
14 Jan 09 |
jari |
205 |
|
1719 |
14 Jan 09 |
jari |
// Enough iteration will make all columns to have the same values as |
1719 |
14 Jan 09 |
jari |
// the target. |
1740 |
21 Jan 09 |
peter |
208 |
suite.err() << "Testing that q=matrix rows gives QuantileNormalization\n"; |
1719 |
14 Jan 09 |
jari |
209 |
utility::Matrix m2(4,2); |
1719 |
14 Jan 09 |
jari |
210 |
m2(0,0) = 0; m2(0,1) = 10; |
1719 |
14 Jan 09 |
jari |
211 |
m2(1,0) = 2; m2(1,1) = 4; |
1719 |
14 Jan 09 |
jari |
212 |
m2(2,0) = 1; m2(2,1) = 0; |
1719 |
14 Jan 09 |
jari |
213 |
m2(3,0) = 3; m2(3,1) = 7; |
1736 |
19 Jan 09 |
peter |
214 |
qQuantileNormalizer qqn2(m2.begin_column(0), m2.end_column(0), m2.rows()); |
1736 |
19 Jan 09 |
peter |
215 |
ColumnNormalizer<qQuantileNormalizer> cn2(qqn2); |
1719 |
14 Jan 09 |
jari |
216 |
utility::Matrix result2(m2.rows(),m2.columns()); |
1736 |
19 Jan 09 |
peter |
217 |
cn2(m2,result2); |
1719 |
14 Jan 09 |
jari |
218 |
suite.add( suite.equal_fix(m2(0,0),result2(2,1),1.0e-12) && |
1719 |
14 Jan 09 |
jari |
219 |
suite.equal_fix(m2(1,0),result2(3,1),1.0e-12) && |
1719 |
14 Jan 09 |
jari |
220 |
suite.equal_fix(m2(2,0),result2(1,1),1.0e-12) && |
1719 |
14 Jan 09 |
jari |
221 |
suite.equal_fix(m2(3,0),result2(0,1),1.0e-12) ); |
2150 |
17 Jan 10 |
peter |
// compile test should not be run |
2150 |
17 Jan 10 |
peter |
223 |
if (false) { |
2150 |
17 Jan 10 |
peter |
224 |
qQuantileNormalizer qqn3(boost::forward_iterator_archetype<double>(), |
2150 |
17 Jan 10 |
peter |
225 |
boost::forward_iterator_archetype<double>(), |
2150 |
17 Jan 10 |
peter |
226 |
100); |
3543 |
23 Dec 16 |
peter |
227 |
qqn3(boost::random_access_iterator_archetype<double>(), |
2150 |
17 Jan 10 |
peter |
228 |
boost::random_access_iterator_archetype<double>(), |
2150 |
17 Jan 10 |
peter |
229 |
boost::mutable_random_access_iterator_archetype<double>()); |
2151 |
17 Jan 10 |
peter |
230 |
using utility::DataWeight; |
2151 |
17 Jan 10 |
peter |
231 |
qQuantileNormalizer qqn4(boost::forward_iterator_archetype<DataWeight>(), |
2151 |
17 Jan 10 |
peter |
232 |
boost::forward_iterator_archetype<DataWeight>(), |
2151 |
17 Jan 10 |
peter |
233 |
100); |
3543 |
23 Dec 16 |
peter |
234 |
qqn4(boost::random_access_iterator_archetype<DataWeight>(), |
2151 |
17 Jan 10 |
peter |
235 |
boost::random_access_iterator_archetype<DataWeight>(), |
2151 |
17 Jan 10 |
peter |
236 |
boost::mutable_random_access_iterator_archetype<DataWeight>()); |
3543 |
23 Dec 16 |
peter |
237 |
qqn4(boost::random_access_iterator_archetype<DataWeight>(), |
2158 |
18 Jan 10 |
peter |
238 |
boost::random_access_iterator_archetype<DataWeight>(), |
2158 |
18 Jan 10 |
peter |
239 |
boost::mutable_random_access_iterator_archetype<double>()); |
3547 |
30 Dec 16 |
peter |
240 |
|
3547 |
30 Dec 16 |
peter |
// test against boost iterator concepts |
3547 |
30 Dec 16 |
peter |
242 |
typedef test::DataIterator<boost::forward_traversal_tag> trait1; |
3547 |
30 Dec 16 |
peter |
243 |
trait1::unweighted_const_iterator input; |
3547 |
30 Dec 16 |
peter |
244 |
trait1::weighted_const_iterator weighted_input; |
3547 |
30 Dec 16 |
peter |
245 |
qQuantileNormalizer qqn5(input, input, 100); |
3547 |
30 Dec 16 |
peter |
246 |
qQuantileNormalizer qqn6(weighted_input, weighted_input, 100); |
3547 |
30 Dec 16 |
peter |
247 |
|
3547 |
30 Dec 16 |
peter |
248 |
typedef test::DataIterator<boost::random_access_traversal_tag> trait2; |
3547 |
30 Dec 16 |
peter |
249 |
trait2::unweighted_const_iterator const_iterator; |
3547 |
30 Dec 16 |
peter |
250 |
trait2::unweighted_iterator iterator; |
3547 |
30 Dec 16 |
peter |
251 |
trait2::weighted_const_iterator const_weighted_iterator; |
3547 |
30 Dec 16 |
peter |
252 |
trait2::weighted_iterator weighted_iterator; |
3547 |
30 Dec 16 |
peter |
253 |
qqn5(const_iterator, const_iterator, iterator); |
3547 |
30 Dec 16 |
peter |
254 |
qqn5(const_iterator, const_iterator, weighted_iterator); |
3547 |
30 Dec 16 |
peter |
255 |
qqn5(const_weighted_iterator, const_weighted_iterator, iterator); |
3547 |
30 Dec 16 |
peter |
256 |
qqn5(const_iterator, const_iterator, weighted_iterator); |
2150 |
17 Jan 10 |
peter |
257 |
} |
1568 |
13 Oct 08 |
jari |
258 |
} |
1568 |
13 Oct 08 |
jari |
259 |
|
1568 |
13 Oct 08 |
jari |
260 |
|
1737 |
19 Jan 09 |
peter |
261 |
void test_qquantile_normalize_weighted(test::Suite& suite) |
1737 |
19 Jan 09 |
peter |
262 |
{ |
1737 |
19 Jan 09 |
peter |
263 |
using namespace normalizer; |
1737 |
19 Jan 09 |
peter |
264 |
|
1737 |
19 Jan 09 |
peter |
265 |
suite.err() << "Testing qQuantileNormalizer weighted\n"; |
1737 |
19 Jan 09 |
peter |
266 |
|
1773 |
05 Feb 09 |
peter |
// test with unweighted target and source |
1773 |
05 Feb 09 |
peter |
268 |
std::vector<double> target; |
1774 |
05 Feb 09 |
peter |
269 |
target.reserve(1000); |
1773 |
05 Feb 09 |
peter |
270 |
while (target.size()<1000) |
1773 |
05 Feb 09 |
peter |
271 |
target.push_back(target.size()); |
1773 |
05 Feb 09 |
peter |
272 |
qQuantileNormalizer qQN(target.begin(), target.end(), 4); |
1773 |
05 Feb 09 |
peter |
273 |
std::vector<double> source; |
1773 |
05 Feb 09 |
peter |
274 |
while (source.size()<10) |
1773 |
05 Feb 09 |
peter |
275 |
source.push_back(source.size()*10); |
1774 |
05 Feb 09 |
peter |
276 |
std::vector<double> result(source.size()); |
3543 |
23 Dec 16 |
peter |
277 |
|
1774 |
05 Feb 09 |
peter |
278 |
qQN(source.begin(), source.end(), result.begin()); |
3543 |
23 Dec 16 |
peter |
279 |
|
1774 |
05 Feb 09 |
peter |
280 |
using utility::DataWeight; |
1778 |
06 Feb 09 |
peter |
281 |
suite.err() << "Testing with unweighted target and weighted source\n"; |
1774 |
05 Feb 09 |
peter |
282 |
std::vector<utility::DataWeight> src_w(source.size(), DataWeight(0, 1)); |
1773 |
05 Feb 09 |
peter |
283 |
std::copy(source.begin(), source.end(), |
1773 |
05 Feb 09 |
peter |
284 |
utility::data_iterator(src_w.begin())); |
1773 |
05 Feb 09 |
peter |
285 |
|
1774 |
05 Feb 09 |
peter |
286 |
std::vector<utility::DataWeight> result_w(src_w.size()); |
1774 |
05 Feb 09 |
peter |
287 |
qQN(src_w.begin(), src_w.end(), result_w.begin()); |
1778 |
06 Feb 09 |
peter |
288 |
suite.add(suite.equal_range(result.begin(), result.end(), |
1774 |
05 Feb 09 |
peter |
289 |
utility::data_iterator(result_w.begin()))); |
1775 |
05 Feb 09 |
peter |
290 |
|
1778 |
06 Feb 09 |
peter |
291 |
suite.err() << "Testing with missing value in source\n"; |
1775 |
05 Feb 09 |
peter |
// adding a missing value |
1954 |
07 May 09 |
jari |
293 |
std::vector<utility::DataWeight>::iterator MWi=src_w.begin(); |
1954 |
07 May 09 |
jari |
294 |
MWi+=5; |
1954 |
07 May 09 |
jari |
295 |
src_w.insert(MWi, DataWeight(std::numeric_limits<double>::quiet_NaN(), 0.0)); |
1778 |
06 Feb 09 |
peter |
296 |
std::vector<utility::DataWeight> result_w2(src_w.size()); |
1778 |
06 Feb 09 |
peter |
297 |
qQN(src_w.begin(), src_w.end(), result_w2.begin()); |
1954 |
07 May 09 |
jari |
// excluding missing value from comparison in suite.equal_range |
1954 |
07 May 09 |
jari |
299 |
MWi=result_w2.begin(); |
1954 |
07 May 09 |
jari |
300 |
MWi+=5; |
1954 |
07 May 09 |
jari |
301 |
result_w2.erase(MWi); |
3543 |
23 Dec 16 |
peter |
302 |
suite.add(suite.equal_range(utility::data_iterator(result_w.begin()), |
1784 |
08 Feb 09 |
peter |
303 |
utility::data_iterator(result_w.end()), |
1954 |
07 May 09 |
jari |
304 |
utility::data_iterator(result_w2.begin()))); |
1784 |
08 Feb 09 |
peter |
305 |
|
1784 |
08 Feb 09 |
peter |
306 |
suite.err() << "testing with weighted target" << std::endl; |
1784 |
08 Feb 09 |
peter |
307 |
std::vector<utility::DataWeight> target_w(target.size()+1, DataWeight(0, 1)); |
1784 |
08 Feb 09 |
peter |
308 |
target_w[0] = DataWeight(5.3, 0); |
1784 |
08 Feb 09 |
peter |
309 |
std::copy(target.begin(), target.end(), |
1784 |
08 Feb 09 |
peter |
310 |
utility::data_iterator(target_w.begin()+1)); |
1784 |
08 Feb 09 |
peter |
311 |
qQuantileNormalizer qQNw(target_w.begin(), target_w.end(), 4); |
1784 |
08 Feb 09 |
peter |
312 |
std::vector<utility::DataWeight> result_w3(src_w.size()); |
1784 |
08 Feb 09 |
peter |
313 |
qQNw(src_w.begin(), src_w.end(), result_w3.begin()); |
1954 |
07 May 09 |
jari |
// excluding missing value from comparison in suite.equal_range |
1954 |
07 May 09 |
jari |
315 |
MWi=result_w3.begin(); |
1954 |
07 May 09 |
jari |
316 |
MWi+=5; |
1954 |
07 May 09 |
jari |
317 |
result_w3.erase(MWi); |
3543 |
23 Dec 16 |
peter |
318 |
suite.add(suite.equal_range(utility::data_iterator(result_w3.begin()), |
1784 |
08 Feb 09 |
peter |
319 |
utility::data_iterator(result_w3.end()), |
1954 |
07 May 09 |
jari |
320 |
utility::data_iterator(result_w2.begin()))); |
3543 |
23 Dec 16 |
peter |
321 |
|
1737 |
19 Jan 09 |
peter |
322 |
} |
1737 |
19 Jan 09 |
peter |
323 |
|
1737 |
19 Jan 09 |
peter |
324 |
|
1432 |
25 Aug 08 |
peter |
325 |
void test_quantile_normalize(test::Suite& suite) |
1432 |
25 Aug 08 |
peter |
326 |
{ |
1445 |
27 Aug 08 |
peter |
327 |
suite.err() << "Testing QuantileNormalizer\n"; |
1432 |
25 Aug 08 |
peter |
328 |
utility::Matrix m(2,2); |
1432 |
25 Aug 08 |
peter |
329 |
m(0,0) = 0; |
1432 |
25 Aug 08 |
peter |
330 |
m(0,1) = 10; |
1432 |
25 Aug 08 |
peter |
331 |
m(1,0) = 2; |
1432 |
25 Aug 08 |
peter |
332 |
m(1,1) = 4; |
1497 |
12 Sep 08 |
peter |
333 |
normalizer::QuantileNormalizer qn; |
1447 |
28 Aug 08 |
peter |
334 |
qn(m, m); |
1432 |
25 Aug 08 |
peter |
335 |
suite.err() << "Testing m(0,0)\n"; |
1432 |
25 Aug 08 |
peter |
336 |
suite.add(suite.equal(m(0,0), 2)); |
1432 |
25 Aug 08 |
peter |
337 |
suite.err() << "Testing m(0,1)\n"; |
1432 |
25 Aug 08 |
peter |
338 |
suite.add(suite.equal(m(0,1), 6)); |
1432 |
25 Aug 08 |
peter |
339 |
suite.err() << "Testing m(1,0)\n"; |
1432 |
25 Aug 08 |
peter |
340 |
suite.add(suite.equal(m(1,0), 6)); |
1432 |
25 Aug 08 |
peter |
341 |
suite.err() << "Testing m(1,1)\n"; |
1432 |
25 Aug 08 |
peter |
342 |
suite.add(suite.equal(m(1,1), 2)); |
3342 |
06 Nov 14 |
peter |
343 |
|
3342 |
06 Nov 14 |
peter |
// testing with landscape-shaped data matrix |
3342 |
06 Nov 14 |
peter |
345 |
m.resize(2, 10); |
3342 |
06 Nov 14 |
peter |
346 |
qn(m, m); |
1432 |
25 Aug 08 |
peter |
347 |
} |
1432 |
25 Aug 08 |
peter |
348 |
|
4185 |
04 Jul 22 |
peter |
349 |
|
4188 |
18 Jul 22 |
peter |
350 |
void test_quantile_normalizer2_different_size(test::Suite& suite) |
4188 |
18 Jul 22 |
peter |
351 |
{ |
4188 |
18 Jul 22 |
peter |
352 |
suite.out() << YAT_TEST_PROLOGUE; |
4188 |
18 Jul 22 |
peter |
353 |
utility::Matrix X(100,10); |
4188 |
18 Jul 22 |
peter |
354 |
for (size_t row=0; row<X.rows(); ++row) |
4188 |
18 Jul 22 |
peter |
355 |
X.row_view(row).all(row); |
4188 |
18 Jul 22 |
peter |
356 |
for (size_t col=0; col<X.columns(); ++col) |
4188 |
18 Jul 22 |
peter |
357 |
random::random_shuffle(X.begin_column(col), X.end_column(col)); |
4188 |
18 Jul 22 |
peter |
358 |
normalizer::QuantileNormalizer2 normalizer; |
4188 |
18 Jul 22 |
peter |
359 |
normalizer.train(X); |
4188 |
18 Jul 22 |
peter |
360 |
|
4188 |
18 Jul 22 |
peter |
361 |
utility::Vector y(50); |
4188 |
18 Jul 22 |
peter |
362 |
for (size_t i=0; i<y.size(); ++i) |
4188 |
18 Jul 22 |
peter |
363 |
y(i) = 0.5 * (2*i + 2*i+1.0); |
4188 |
18 Jul 22 |
peter |
364 |
|
4188 |
18 Jul 22 |
peter |
365 |
utility::Vector z(y.size()); |
4188 |
18 Jul 22 |
peter |
366 |
for (size_t i=0; i<z.size(); ++i) |
4188 |
18 Jul 22 |
peter |
367 |
z(i) = i; |
4188 |
18 Jul 22 |
peter |
368 |
normalizer.normalize(z, z); |
4188 |
18 Jul 22 |
peter |
369 |
|
4188 |
18 Jul 22 |
peter |
370 |
suite.add(suite.equal_range(z.begin(), z.end(), y.begin())); |
4189 |
18 Jul 22 |
peter |
371 |
|
4189 |
18 Jul 22 |
peter |
372 |
utility::Matrix Y(y.size(), 1); |
4189 |
18 Jul 22 |
peter |
373 |
for (size_t i=0; i<y.size(); ++i) |
4189 |
18 Jul 22 |
peter |
374 |
Y(i, 0) = y(i); |
4189 |
18 Jul 22 |
peter |
375 |
utility::Matrix Z(z.size(), 1); |
4189 |
18 Jul 22 |
peter |
376 |
for (size_t i=0; i<z.size(); ++i) |
4189 |
18 Jul 22 |
peter |
377 |
Z(i, 0) = i; |
4189 |
18 Jul 22 |
peter |
378 |
normalizer.normalize(Z, Z); |
4189 |
18 Jul 22 |
peter |
379 |
|
4189 |
18 Jul 22 |
peter |
380 |
suite.add(suite.equal_matrix(Z, Y)); |
4188 |
18 Jul 22 |
peter |
381 |
} |
4188 |
18 Jul 22 |
peter |
382 |
|
4188 |
18 Jul 22 |
peter |
383 |
|
4185 |
04 Jul 22 |
peter |
384 |
void test_quantile_normalizer2(test::Suite& suite) |
4185 |
04 Jul 22 |
peter |
385 |
{ |
4185 |
04 Jul 22 |
peter |
386 |
suite.out() << "Testing QuantileNormalizer2\n"; |
4185 |
04 Jul 22 |
peter |
387 |
std::ifstream is(test::filename("data/nm_data_centralized.txt")); |
4185 |
04 Jul 22 |
peter |
388 |
assert(is.good()); |
4185 |
04 Jul 22 |
peter |
389 |
const utility::Matrix X(is); |
4185 |
04 Jul 22 |
peter |
390 |
is.close(); |
4185 |
04 Jul 22 |
peter |
391 |
utility::MatrixConstView X_train(X, 0, 0, X.rows(), 54); |
4185 |
04 Jul 22 |
peter |
392 |
utility::MatrixConstView X_test(X, 0, 54, X.rows(), 10); |
4185 |
04 Jul 22 |
peter |
393 |
|
4185 |
04 Jul 22 |
peter |
394 |
normalizer::QuantileNormalizer2 normalizer; |
4185 |
04 Jul 22 |
peter |
395 |
utility::Matrix X_train2(X_train.rows(), X_train.columns()); |
4185 |
04 Jul 22 |
peter |
396 |
normalizer(X_train, X_train2); |
4185 |
04 Jul 22 |
peter |
397 |
if (isnull(X_train2)) { |
4185 |
04 Jul 22 |
peter |
398 |
suite.add(false); |
4185 |
04 Jul 22 |
peter |
399 |
suite.err() << "all values zero in X_train2\n"; |
4185 |
04 Jul 22 |
peter |
400 |
} |
4185 |
04 Jul 22 |
peter |
401 |
for (size_t i=0; i<X_train2.columns(); ++i) { |
4185 |
04 Jul 22 |
peter |
402 |
std::sort(X_train2.begin_column(i), X_train2.end_column(i)); |
4185 |
04 Jul 22 |
peter |
403 |
if (!suite.equal_range(X_train2.begin_column(0), X_train2.end_column(0), |
4185 |
04 Jul 22 |
peter |
404 |
X_train2.begin_column(i))) { |
4185 |
04 Jul 22 |
peter |
405 |
suite.add(false); |
4185 |
04 Jul 22 |
peter |
406 |
suite.err() << "Column " << i << " not equal column 0\n"; |
4185 |
04 Jul 22 |
peter |
407 |
} |
4185 |
04 Jul 22 |
peter |
408 |
} |
4185 |
04 Jul 22 |
peter |
409 |
|
4185 |
04 Jul 22 |
peter |
410 |
utility::Matrix X_test2(X_test.rows(), X_test.columns()); |
4185 |
04 Jul 22 |
peter |
411 |
normalizer.normalize(X_test, X_test2); |
4185 |
04 Jul 22 |
peter |
412 |
if (isnull(X_test2)) { |
4185 |
04 Jul 22 |
peter |
413 |
suite.add(false); |
4185 |
04 Jul 22 |
peter |
414 |
suite.err() << "all values zero in X_test2\n"; |
4185 |
04 Jul 22 |
peter |
415 |
} |
4185 |
04 Jul 22 |
peter |
416 |
for (size_t i=0; i<X_test2.columns(); ++i) { |
4185 |
04 Jul 22 |
peter |
417 |
std::sort(X_test2.begin_column(i), X_test2.end_column(i)); |
4185 |
04 Jul 22 |
peter |
418 |
if (!suite.equal_range(X_train2.begin_column(0), X_train2.end_column(0), |
4185 |
04 Jul 22 |
peter |
419 |
X_test2.begin_column(i))) { |
4185 |
04 Jul 22 |
peter |
420 |
suite.add(false); |
4185 |
04 Jul 22 |
peter |
421 |
suite.err() << "Column " << i << " not equal column 0\n"; |
4185 |
04 Jul 22 |
peter |
422 |
} |
4185 |
04 Jul 22 |
peter |
423 |
} |
4185 |
04 Jul 22 |
peter |
424 |
|
4185 |
04 Jul 22 |
peter |
425 |
utility::Matrix Y(X.rows(), X.columns()); |
4185 |
04 Jul 22 |
peter |
426 |
for (size_t i=0; i<X.columns(); ++i) { |
4185 |
04 Jul 22 |
peter |
427 |
auto y = Y.column_view(i); |
4185 |
04 Jul 22 |
peter |
428 |
normalizer.normalize(X.column_const_view(i), y); |
4185 |
04 Jul 22 |
peter |
429 |
std::sort(y.begin(), y.end()); |
4185 |
04 Jul 22 |
peter |
430 |
if (!suite.equal_range(X_train2.begin_column(0), X_train2.end_column(0), |
4185 |
04 Jul 22 |
peter |
431 |
y.begin())) { |
4185 |
04 Jul 22 |
peter |
432 |
suite.add(false); |
4185 |
04 Jul 22 |
peter |
433 |
suite.err() << "Column " << i << " not equal column 0\n"; |
4185 |
04 Jul 22 |
peter |
434 |
} |
4185 |
04 Jul 22 |
peter |
435 |
} |
4185 |
04 Jul 22 |
peter |
436 |
if (isnull(Y)) { |
4185 |
04 Jul 22 |
peter |
437 |
suite.add(false); |
4185 |
04 Jul 22 |
peter |
438 |
suite.err() << "all values zero in Y\n"; |
4185 |
04 Jul 22 |
peter |
439 |
} |
4188 |
18 Jul 22 |
peter |
440 |
|
4188 |
18 Jul 22 |
peter |
// test when template (train) and data (test) are of different size |
4188 |
18 Jul 22 |
peter |
442 |
test_quantile_normalizer2_different_size(suite); |
4189 |
18 Jul 22 |
peter |
443 |
|
4189 |
18 Jul 22 |
peter |
444 |
normalizer::QuantileNormalizer2 normalizer2(normalizer); |
4189 |
18 Jul 22 |
peter |
445 |
normalizer::QuantileNormalizer2 normalizer3(std::move(normalizer2)); |
4189 |
18 Jul 22 |
peter |
446 |
normalizer3 = normalizer; |
4189 |
18 Jul 22 |
peter |
447 |
normalizer = std::move(normalizer3); |
4185 |
04 Jul 22 |
peter |
448 |
} |
4185 |
04 Jul 22 |
peter |
449 |
|
4185 |
04 Jul 22 |
peter |
450 |
|
1520 |
21 Sep 08 |
peter |
451 |
void test_row_normalize(test::Suite& suite) |
1520 |
21 Sep 08 |
peter |
452 |
{ |
1520 |
21 Sep 08 |
peter |
453 |
using namespace normalizer; |
1520 |
21 Sep 08 |
peter |
454 |
suite.err() << "Testing RowNormalizer\n"; |
3543 |
23 Dec 16 |
peter |
455 |
|
1520 |
21 Sep 08 |
peter |
456 |
utility::Matrix m(2,3); |
1520 |
21 Sep 08 |
peter |
457 |
m(0,0) = 0; |
1520 |
21 Sep 08 |
peter |
458 |
m(0,1) = 10; |
1520 |
21 Sep 08 |
peter |
459 |
m(1,0) = 2; |
1520 |
21 Sep 08 |
peter |
460 |
m(1,1) = 4; |
1520 |
21 Sep 08 |
peter |
461 |
utility::Matrix m2(m); |
1520 |
21 Sep 08 |
peter |
462 |
m2.transpose(); |
1520 |
21 Sep 08 |
peter |
463 |
ColumnNormalizer<Centralizer<> > cn; |
1520 |
21 Sep 08 |
peter |
464 |
RowNormalizer<Centralizer<> > rn; |
1520 |
21 Sep 08 |
peter |
465 |
cn(m, m); |
1520 |
21 Sep 08 |
peter |
466 |
rn(m2, m2); |
1520 |
21 Sep 08 |
peter |
467 |
m2.transpose(); |
1520 |
21 Sep 08 |
peter |
468 |
suite.equal_range(m.begin(), m.end(), m2.begin()); |
2147 |
17 Jan 10 |
peter |
469 |
if (false) { // do not run compile tests |
3543 |
23 Dec 16 |
peter |
470 |
test::container2d_archetype<double> container2d; |
3543 |
23 Dec 16 |
peter |
471 |
test::mutable_container2d_archetype<double> mutable_container2d; |
2147 |
17 Jan 10 |
peter |
472 |
rn(container2d, mutable_container2d); |
2147 |
17 Jan 10 |
peter |
473 |
} |
1520 |
21 Sep 08 |
peter |
474 |
} |
1520 |
21 Sep 08 |
peter |
475 |
|
1496 |
12 Sep 08 |
peter |
476 |
void test_spearman(test::Suite& suite) |
1496 |
12 Sep 08 |
peter |
477 |
{ |
1496 |
12 Sep 08 |
peter |
478 |
suite.err() << "Testing Spearman\n"; |
1497 |
12 Sep 08 |
peter |
479 |
normalizer::Spearman spearman; |
1496 |
12 Sep 08 |
peter |
480 |
std::vector<double> vec; |
1496 |
12 Sep 08 |
peter |
481 |
vec.push_back(0); |
1496 |
12 Sep 08 |
peter |
482 |
vec.push_back(2); |
1496 |
12 Sep 08 |
peter |
483 |
vec.push_back(3); |
1496 |
12 Sep 08 |
peter |
484 |
vec.push_back(1); |
1496 |
12 Sep 08 |
peter |
485 |
spearman(vec.begin(), vec.end(), vec.begin()); |
1496 |
12 Sep 08 |
peter |
486 |
std::vector<double> correct; |
1511 |
19 Sep 08 |
peter |
487 |
correct.push_back(1.0/8); |
1511 |
19 Sep 08 |
peter |
488 |
correct.push_back(5.0/8); |
1511 |
19 Sep 08 |
peter |
489 |
correct.push_back(7.0/8); |
1511 |
19 Sep 08 |
peter |
490 |
correct.push_back(3.0/8); |
1512 |
19 Sep 08 |
peter |
491 |
suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin())); |
1511 |
19 Sep 08 |
peter |
492 |
suite.err() << "Testing Spearman with ties\n"; |
1511 |
19 Sep 08 |
peter |
493 |
vec[1]=vec[2]; |
1511 |
19 Sep 08 |
peter |
494 |
correct[1] = correct[2] = (correct[1]+correct[2])/2; |
1512 |
19 Sep 08 |
peter |
495 |
spearman(vec.begin(), vec.end(), vec.begin()); |
1512 |
19 Sep 08 |
peter |
496 |
suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin())); |
1509 |
17 Sep 08 |
peter |
497 |
test_spearman_weighted(suite); |
3543 |
23 Dec 16 |
peter |
498 |
|
1496 |
12 Sep 08 |
peter |
499 |
} |
1509 |
17 Sep 08 |
peter |
500 |
|
1509 |
17 Sep 08 |
peter |
501 |
|
1536 |
25 Sep 08 |
peter |
502 |
void test_gauss_normalize(test::Suite& suite) |
1536 |
25 Sep 08 |
peter |
503 |
{ |
1536 |
25 Sep 08 |
peter |
504 |
suite.err() << "Testing Gauss\n"; |
1536 |
25 Sep 08 |
peter |
505 |
normalizer::Gauss gauss; |
1536 |
25 Sep 08 |
peter |
506 |
std::vector<double> vec; |
1536 |
25 Sep 08 |
peter |
507 |
vec.push_back(1); |
1536 |
25 Sep 08 |
peter |
508 |
gauss(vec.begin(), vec.end(), vec.begin()); |
1536 |
25 Sep 08 |
peter |
509 |
suite.add(suite.equal(vec.front(), 0)); |
1536 |
25 Sep 08 |
peter |
510 |
vec.push_back(1); |
1536 |
25 Sep 08 |
peter |
511 |
gauss(vec.begin(), vec.end(), vec.begin()); |
1536 |
25 Sep 08 |
peter |
512 |
suite.add(suite.equal(vec.front(), -vec.back())); |
2148 |
17 Jan 10 |
peter |
// compile test should not be run |
2148 |
17 Jan 10 |
peter |
514 |
if (false) { |
3544 |
23 Dec 16 |
peter |
515 |
typedef test::DataIterator<boost::random_access_traversal_tag> trait; |
3544 |
23 Dec 16 |
peter |
516 |
trait::unweighted_const_iterator readable_iterator; |
3544 |
23 Dec 16 |
peter |
517 |
trait::unweighted_iterator writable_iterator; |
3544 |
23 Dec 16 |
peter |
518 |
trait::weighted_const_iterator weighted_readable_iterator; |
3544 |
23 Dec 16 |
peter |
519 |
trait::weighted_iterator weighted_writable_iterator; |
3290 |
12 Jul 14 |
peter |
520 |
|
3290 |
12 Jul 14 |
peter |
521 |
gauss(readable_iterator, readable_iterator, writable_iterator); |
3290 |
12 Jul 14 |
peter |
522 |
gauss(readable_iterator, readable_iterator, weighted_writable_iterator); |
3290 |
12 Jul 14 |
peter |
523 |
gauss(weighted_readable_iterator, weighted_readable_iterator, |
3290 |
12 Jul 14 |
peter |
524 |
writable_iterator); |
3290 |
12 Jul 14 |
peter |
525 |
gauss(weighted_readable_iterator, weighted_readable_iterator, |
3290 |
12 Jul 14 |
peter |
526 |
weighted_writable_iterator); |
2148 |
17 Jan 10 |
peter |
527 |
} |
1536 |
25 Sep 08 |
peter |
528 |
} |
1536 |
25 Sep 08 |
peter |
529 |
|
1509 |
17 Sep 08 |
peter |
530 |
void test_spearman_weighted(test::Suite& suite) |
1509 |
17 Sep 08 |
peter |
531 |
{ |
1509 |
17 Sep 08 |
peter |
532 |
suite.err() << "Testing Weighted Spearman\n"; |
1509 |
17 Sep 08 |
peter |
533 |
normalizer::Spearman spearman; |
1509 |
17 Sep 08 |
peter |
534 |
|
1509 |
17 Sep 08 |
peter |
535 |
suite.err() << "Testing that unity weights reproduces unweighted case\n"; |
1509 |
17 Sep 08 |
peter |
536 |
utility::MatrixWeighted m(1,4,0,1); |
1509 |
17 Sep 08 |
peter |
537 |
utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0); |
1509 |
17 Sep 08 |
peter |
538 |
m(0,0).data()=0; |
1509 |
17 Sep 08 |
peter |
539 |
m(0,1).data()=2; |
1509 |
17 Sep 08 |
peter |
540 |
m(0,2).data()=3; |
1509 |
17 Sep 08 |
peter |
541 |
m(0,3).data()=1; |
1509 |
17 Sep 08 |
peter |
542 |
std::vector<double> correct(m.columns()); |
1509 |
17 Sep 08 |
peter |
543 |
std::vector<double> correct_w(m.columns(), 1.0); |
1509 |
17 Sep 08 |
peter |
544 |
std::copy(utility::data_iterator(m.begin_row(0)), |
1509 |
17 Sep 08 |
peter |
545 |
utility::data_iterator(m.end_row(0)), |
1509 |
17 Sep 08 |
peter |
546 |
correct.begin()); |
1509 |
17 Sep 08 |
peter |
547 |
spearman(correct.begin(), correct.end(), correct.begin()); |
1509 |
17 Sep 08 |
peter |
548 |
spearman(m.begin_row(0), m.end_row(0), res.begin_row(0)); |
1509 |
17 Sep 08 |
peter |
549 |
|
1509 |
17 Sep 08 |
peter |
550 |
using utility::data_iterator; |
1509 |
17 Sep 08 |
peter |
551 |
suite.add(suite.equal_range(data_iterator(res.begin_row(0)), |
1509 |
17 Sep 08 |
peter |
552 |
data_iterator(res.end_row(0)), |
1509 |
17 Sep 08 |
peter |
553 |
correct.begin())); |
1509 |
17 Sep 08 |
peter |
554 |
using utility::weight_iterator; |
1509 |
17 Sep 08 |
peter |
555 |
suite.add(suite.equal_range(weight_iterator(res.begin_row(0)), |
1509 |
17 Sep 08 |
peter |
556 |
weight_iterator(res.end_row(0)), |
1509 |
17 Sep 08 |
peter |
557 |
correct_w.begin())); |
1509 |
17 Sep 08 |
peter |
558 |
|
1509 |
17 Sep 08 |
peter |
559 |
suite.err() << "Testing rescaling of weights\n"; |
1509 |
17 Sep 08 |
peter |
560 |
for (size_t i=0; i<m.columns(); ++i) { |
1509 |
17 Sep 08 |
peter |
561 |
m(0,i).weight() *= 2; |
1509 |
17 Sep 08 |
peter |
562 |
correct_w[i] *= 2; |
3543 |
23 Dec 16 |
peter |
563 |
} |
1509 |
17 Sep 08 |
peter |
564 |
spearman(m.begin_row(0), m.end_row(0), res.begin_row(0)); |
1509 |
17 Sep 08 |
peter |
565 |
suite.add(suite.equal_range(data_iterator(res.begin_row(0)), |
1509 |
17 Sep 08 |
peter |
566 |
data_iterator(res.end_row(0)), |
1509 |
17 Sep 08 |
peter |
567 |
correct.begin())); |
1509 |
17 Sep 08 |
peter |
568 |
suite.add(suite.equal_range(weight_iterator(res.begin_row(0)), |
1509 |
17 Sep 08 |
peter |
569 |
weight_iterator(res.end_row(0)), |
1509 |
17 Sep 08 |
peter |
570 |
correct_w.begin())); |
1509 |
17 Sep 08 |
peter |
571 |
|
3543 |
23 Dec 16 |
peter |
572 |
|
1509 |
17 Sep 08 |
peter |
573 |
suite.err() << "Testing case with a zero weight\n"; |
1509 |
17 Sep 08 |
peter |
574 |
m(0,1).data() = std::numeric_limits<double>::quiet_NaN(); |
1509 |
17 Sep 08 |
peter |
575 |
m(0,1).weight() = 0.0; |
1509 |
17 Sep 08 |
peter |
576 |
spearman(m.begin_row(0), m.end_row(0), res.begin_row(0)); |
3543 |
23 Dec 16 |
peter |
577 |
suite.add(suite.equal(res(0,0).data(), 0.5/3)); |
3543 |
23 Dec 16 |
peter |
578 |
suite.add(suite.equal(res(0,2).data(), 2.5/3)); |
3543 |
23 Dec 16 |
peter |
579 |
suite.add(suite.equal(res(0,3).data(), 1.5/3)); |
1512 |
19 Sep 08 |
peter |
580 |
|
1512 |
19 Sep 08 |
peter |
581 |
suite.err() << "Testing case with ties\n"; |
1512 |
19 Sep 08 |
peter |
582 |
m(0,0).data() = m(0,2).data(); |
1512 |
19 Sep 08 |
peter |
583 |
spearman(m.begin_row(0), m.end_row(0), res.begin_row(0)); |
3543 |
23 Dec 16 |
peter |
584 |
suite.add(suite.equal(res(0,0).data(), 2.0/3)); |
3543 |
23 Dec 16 |
peter |
585 |
suite.add(suite.equal(res(0,2).data(), 2.0/3)); |
3543 |
23 Dec 16 |
peter |
586 |
suite.add(suite.equal(res(0,3).data(), 0.5/3)); |
3543 |
23 Dec 16 |
peter |
// do not run compile test |
2149 |
17 Jan 10 |
peter |
588 |
if (false) { |
3543 |
23 Dec 16 |
peter |
589 |
typedef test::DataIterator<boost::random_access_traversal_tag> trait; |
3543 |
23 Dec 16 |
peter |
590 |
trait::unweighted_const_iterator unweighted_const_iterator; |
3543 |
23 Dec 16 |
peter |
591 |
trait::unweighted_iterator unweighted_mutable_iterator; |
3543 |
23 Dec 16 |
peter |
592 |
trait::weighted_const_iterator weighted_const_iterator; |
3543 |
23 Dec 16 |
peter |
593 |
trait::weighted_iterator weighted_mutable_iterator; |
3289 |
12 Jul 14 |
peter |
594 |
|
3543 |
23 Dec 16 |
peter |
595 |
spearman(unweighted_const_iterator, unweighted_const_iterator, |
3543 |
23 Dec 16 |
peter |
596 |
unweighted_mutable_iterator); |
3543 |
23 Dec 16 |
peter |
597 |
spearman(unweighted_const_iterator, unweighted_const_iterator, |
3543 |
23 Dec 16 |
peter |
598 |
weighted_mutable_iterator); |
3543 |
23 Dec 16 |
peter |
599 |
spearman(weighted_const_iterator, weighted_const_iterator, |
3543 |
23 Dec 16 |
peter |
600 |
unweighted_mutable_iterator); |
3543 |
23 Dec 16 |
peter |
601 |
spearman(weighted_const_iterator, weighted_const_iterator, |
3543 |
23 Dec 16 |
peter |
602 |
weighted_mutable_iterator); |
2149 |
17 Jan 10 |
peter |
603 |
} |
1509 |
17 Sep 08 |
peter |
604 |
} |
1521 |
21 Sep 08 |
peter |
605 |
|
3543 |
23 Dec 16 |
peter |
606 |
|
3957 |
26 Jul 20 |
peter |
607 |
void test_unity_scaler(test::Suite& suite) |
3957 |
26 Jul 20 |
peter |
608 |
{ |
3957 |
26 Jul 20 |
peter |
609 |
suite.err() << "Testing UnityScaler\n"; |
3957 |
26 Jul 20 |
peter |
610 |
utility::Vector source(4); |
3957 |
26 Jul 20 |
peter |
611 |
for (size_t i=0; i<4; ++i) |
3957 |
26 Jul 20 |
peter |
612 |
source(i) = 1.0 + i; |
3957 |
26 Jul 20 |
peter |
613 |
normalizer::UnityScaler normalizer; |
3957 |
26 Jul 20 |
peter |
614 |
utility::Vector result(source.size()); |
3957 |
26 Jul 20 |
peter |
615 |
normalizer(source.begin(), source.end(), result.begin()); |
3957 |
26 Jul 20 |
peter |
616 |
{ |
3957 |
26 Jul 20 |
peter |
617 |
if (!suite.equal(sum(result), 1.0)) { |
3957 |
26 Jul 20 |
peter |
618 |
suite.add(false); |
3957 |
26 Jul 20 |
peter |
619 |
suite.err() << "incorrect sum: " << sum(result) << "\n"; |
3957 |
26 Jul 20 |
peter |
620 |
} |
3957 |
26 Jul 20 |
peter |
621 |
utility::Vector expected = source * (1.0 / sum(source)); |
3957 |
26 Jul 20 |
peter |
622 |
if (!suite.equal_range(result.begin(), result.end(), expected.begin())) { |
3957 |
26 Jul 20 |
peter |
623 |
suite.add(false); |
3957 |
26 Jul 20 |
peter |
624 |
suite.err() << "unweighted normalizer failed\n"; |
3957 |
26 Jul 20 |
peter |
625 |
suite.err() << "result: "; |
3957 |
26 Jul 20 |
peter |
626 |
std::copy(result.begin(), result.end(), |
3957 |
26 Jul 20 |
peter |
627 |
std::ostream_iterator<double>(suite.err(), " ")); |
3957 |
26 Jul 20 |
peter |
628 |
} |
3957 |
26 Jul 20 |
peter |
629 |
} |
3957 |
26 Jul 20 |
peter |
630 |
|
3957 |
26 Jul 20 |
peter |
// normlize unweighted -> weighted |
3957 |
26 Jul 20 |
peter |
632 |
std::vector<utility::DataWeight> wresult(source.size()); |
3957 |
26 Jul 20 |
peter |
633 |
wresult[1].weight() = 0; |
3957 |
26 Jul 20 |
peter |
634 |
normalizer(source.begin(), source.end(), wresult.begin()); |
3957 |
26 Jul 20 |
peter |
// we expect data to same as in the unweighted result and resulting |
3957 |
26 Jul 20 |
peter |
// weights being 1 |
3957 |
26 Jul 20 |
peter |
637 |
for (size_t i=0; i<wresult.size(); ++i) { |
3957 |
26 Jul 20 |
peter |
638 |
if (!suite.add(suite.equal(wresult[i].data(), result(i)))) |
3957 |
26 Jul 20 |
peter |
639 |
suite.err() << "normalize unweighted to weighted failed: " |
3957 |
26 Jul 20 |
peter |
640 |
<< "incorrect data\n"; |
3957 |
26 Jul 20 |
peter |
641 |
if (!suite.add(suite.equal(wresult[i].weight(), 1.0))) |
3957 |
26 Jul 20 |
peter |
642 |
suite.err() << "normalize unweighted to weighted failed: " |
3957 |
26 Jul 20 |
peter |
643 |
<< "incorrect weight\n"; |
3957 |
26 Jul 20 |
peter |
644 |
} |
3957 |
26 Jul 20 |
peter |
645 |
|
3957 |
26 Jul 20 |
peter |
// create weighted source identical to unweighted one and with unity |
3957 |
26 Jul 20 |
peter |
// weights |
3957 |
26 Jul 20 |
peter |
648 |
std::vector<utility::DataWeight> wsource(source.size()); |
3957 |
26 Jul 20 |
peter |
649 |
for (size_t i=0; i<wsource.size(); ++i) { |
3957 |
26 Jul 20 |
peter |
650 |
wsource[i].data() = source(i); |
3957 |
26 Jul 20 |
peter |
651 |
wsource[i].weight() = 1.0; |
3957 |
26 Jul 20 |
peter |
652 |
} |
3957 |
26 Jul 20 |
peter |
653 |
|
3957 |
26 Jul 20 |
peter |
// normalize from weighted into unweighted is an odd case, but at |
3957 |
26 Jul 20 |
peter |
// least test that it compiles and that source with unity weights |
3957 |
26 Jul 20 |
peter |
// reproduce the unweighted behaviour. |
3957 |
26 Jul 20 |
peter |
657 |
{ |
3957 |
26 Jul 20 |
peter |
658 |
utility::Vector result2(source.size()); |
3957 |
26 Jul 20 |
peter |
659 |
normalizer(wsource.begin(), wsource.end(), result2.begin()); |
3957 |
26 Jul 20 |
peter |
660 |
if (!suite.equal_range(result2.begin(), result2.end(), result.begin())) { |
3957 |
26 Jul 20 |
peter |
661 |
suite.add(false); |
3957 |
26 Jul 20 |
peter |
662 |
suite.err() << "normalizing weighted into unweighted failed\n"; |
3957 |
26 Jul 20 |
peter |
663 |
} |
3957 |
26 Jul 20 |
peter |
664 |
} |
3957 |
26 Jul 20 |
peter |
665 |
|
3957 |
26 Jul 20 |
peter |
// test that we get the same result when normalizing into a weighted |
3957 |
26 Jul 20 |
peter |
// container. |
3957 |
26 Jul 20 |
peter |
668 |
{ |
3957 |
26 Jul 20 |
peter |
669 |
std::vector<utility::DataWeight> |
3957 |
26 Jul 20 |
peter |
670 |
wresult2(wresult.size(), utility::DataWeight(2.13, 0.12)); |
3957 |
26 Jul 20 |
peter |
671 |
normalizer(wsource.begin(), wsource.end(), wresult2.begin()); |
3957 |
26 Jul 20 |
peter |
672 |
for (size_t i=0; i<wresult.size(); ++i) { |
3957 |
26 Jul 20 |
peter |
673 |
suite.add(suite.equal(wresult2[i].data(), wresult[i].data())); |
3957 |
26 Jul 20 |
peter |
674 |
suite.add(suite.equal(wresult2[i].weight(), wresult[i].weight())); |
3957 |
26 Jul 20 |
peter |
675 |
} |
3957 |
26 Jul 20 |
peter |
676 |
} |
3957 |
26 Jul 20 |
peter |
677 |
|
3957 |
26 Jul 20 |
peter |
678 |
|
3957 |
26 Jul 20 |
peter |
// test that if we double all weights, sum(w x) is still 1.0 |
3957 |
26 Jul 20 |
peter |
680 |
{ |
3957 |
26 Jul 20 |
peter |
681 |
std::vector<utility::DataWeight> wsource2(wsource); |
3957 |
26 Jul 20 |
peter |
682 |
for (size_t i=0; i<wsource2.size(); ++i) |
3957 |
26 Jul 20 |
peter |
683 |
wsource2[i].weight() *= 2.0; |
3957 |
26 Jul 20 |
peter |
684 |
wsource2[1].data() = std::numeric_limits<double>::quiet_NaN(); |
3957 |
26 Jul 20 |
peter |
685 |
wsource2[1].weight() = 0.0; |
3957 |
26 Jul 20 |
peter |
686 |
std::vector<utility::DataWeight> wresult2(wsource2.size()); |
3957 |
26 Jul 20 |
peter |
687 |
normalizer(wsource2.begin(), wsource2.end(), wresult2.begin()); |
3957 |
26 Jul 20 |
peter |
688 |
double wx = 0; |
3957 |
26 Jul 20 |
peter |
689 |
for (size_t i=0; i<wresult2.size(); ++i) |
3957 |
26 Jul 20 |
peter |
690 |
if (wresult2[i].weight()) |
3957 |
26 Jul 20 |
peter |
691 |
wx += wresult2[i].weight() * wresult2[i].data(); |
3957 |
26 Jul 20 |
peter |
692 |
if (!suite.add(suite.equal(wx, 1.0))) |
3957 |
26 Jul 20 |
peter |
693 |
suite.err() << "incorrect wx\n"; |
3957 |
26 Jul 20 |
peter |
694 |
} |
3957 |
26 Jul 20 |
peter |
695 |
} |
3957 |
26 Jul 20 |
peter |
696 |
|
3957 |
26 Jul 20 |
peter |
697 |
|
1521 |
21 Sep 08 |
peter |
698 |
void test_z_score(test::Suite& suite) |
1521 |
21 Sep 08 |
peter |
699 |
{ |
1521 |
21 Sep 08 |
peter |
700 |
suite.err() << "Testing Zscore\n"; |
1521 |
21 Sep 08 |
peter |
701 |
std::vector<double> vec; |
1521 |
21 Sep 08 |
peter |
702 |
vec.push_back(0); |
1521 |
21 Sep 08 |
peter |
703 |
vec.push_back(3.14); |
1521 |
21 Sep 08 |
peter |
704 |
normalizer::Zscore zscore; |
1521 |
21 Sep 08 |
peter |
705 |
zscore(vec.begin(), vec.end(), vec.begin()); |
1521 |
21 Sep 08 |
peter |
706 |
for (size_t i=0; i<vec.size(); ++i) |
1521 |
21 Sep 08 |
peter |
707 |
suite.add(suite.equal(vec[i], 2.0*i-1.0)); |
1524 |
23 Sep 08 |
peter |
708 |
|
1524 |
23 Sep 08 |
peter |
709 |
std::vector<utility::DataWeight> vec2; |
1524 |
23 Sep 08 |
peter |
710 |
vec2.push_back(utility::DataWeight(1,1)); |
1524 |
23 Sep 08 |
peter |
711 |
vec2.push_back(utility::DataWeight(2.13,0.5)); |
1524 |
23 Sep 08 |
peter |
712 |
vec2.push_back(utility::DataWeight(2.13,0.5)); |
1524 |
23 Sep 08 |
peter |
713 |
std::vector<utility::DataWeight> vec3(vec2.size()); |
1524 |
23 Sep 08 |
peter |
714 |
zscore(vec2.begin(), vec2.end(), vec3.begin()); |
1524 |
23 Sep 08 |
peter |
715 |
for (size_t i=0; i<vec2.size(); ++i) |
1524 |
23 Sep 08 |
peter |
716 |
suite.add(suite.equal(vec3[i].weight(), vec2[i].weight())); |
1524 |
23 Sep 08 |
peter |
717 |
suite.add(suite.equal(vec3[0].data(), -1.0)); |
1524 |
23 Sep 08 |
peter |
718 |
suite.add(suite.equal(vec3[1].data(), 1.0)); |
1524 |
23 Sep 08 |
peter |
719 |
suite.add(suite.equal(vec3[2].data(), 1.0)); |
2157 |
18 Jan 10 |
peter |
// compile test should not be run |
2157 |
18 Jan 10 |
peter |
721 |
if (false) { |
3543 |
23 Dec 16 |
peter |
722 |
typedef test::DataIterator<boost::forward_traversal_tag> trait; |
3543 |
23 Dec 16 |
peter |
723 |
trait::unweighted_const_iterator unweighted_const_iterator; |
3543 |
23 Dec 16 |
peter |
724 |
trait::unweighted_iterator unweighted_mutable_iterator; |
3543 |
23 Dec 16 |
peter |
725 |
trait::weighted_const_iterator weighted_const_iterator; |
3543 |
23 Dec 16 |
peter |
726 |
trait::weighted_iterator weighted_mutable_iterator; |
3541 |
23 Dec 16 |
peter |
727 |
|
3541 |
23 Dec 16 |
peter |
728 |
zscore(unweighted_const_iterator, unweighted_const_iterator, |
3541 |
23 Dec 16 |
peter |
729 |
unweighted_mutable_iterator); |
3541 |
23 Dec 16 |
peter |
730 |
zscore(unweighted_const_iterator, unweighted_const_iterator, |
3541 |
23 Dec 16 |
peter |
731 |
weighted_mutable_iterator); |
3541 |
23 Dec 16 |
peter |
732 |
zscore(weighted_const_iterator, weighted_const_iterator, |
3541 |
23 Dec 16 |
peter |
733 |
unweighted_mutable_iterator); |
3541 |
23 Dec 16 |
peter |
734 |
zscore(weighted_const_iterator, weighted_const_iterator, |
3541 |
23 Dec 16 |
peter |
735 |
weighted_mutable_iterator); |
2157 |
18 Jan 10 |
peter |
736 |
} |
1521 |
21 Sep 08 |
peter |
737 |
} |