41 |
26 Feb 04 |
peter |
// $Id$ |
41 |
26 Feb 04 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2012 Peter Johansson |
41 |
26 Feb 04 |
peter |
8 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
10 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
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 |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
15 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
20 |
|
675 |
10 Oct 06 |
jari |
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/>. |
675 |
10 Oct 06 |
jari |
23 |
*/ |
675 |
10 Oct 06 |
jari |
24 |
|
2881 |
18 Nov 12 |
peter |
25 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
26 |
|
1238 |
15 Mar 08 |
peter |
27 |
#include "Suite.h" |
1238 |
15 Mar 08 |
peter |
28 |
|
1121 |
22 Feb 08 |
peter |
29 |
#include "yat/utility/Matrix.h" |
675 |
10 Oct 06 |
jari |
30 |
#include "yat/classifier/KernelFunction.h" |
675 |
10 Oct 06 |
jari |
31 |
#include "yat/classifier/PolynomialKernelFunction.h" |
675 |
10 Oct 06 |
jari |
32 |
#include "yat/classifier/GaussianKernelFunction.h" |
675 |
10 Oct 06 |
jari |
33 |
#include "yat/classifier/KernelLookup.h" |
675 |
10 Oct 06 |
jari |
34 |
#include "yat/classifier/Kernel_MEV.h" |
675 |
10 Oct 06 |
jari |
35 |
#include "yat/classifier/Kernel_SEV.h" |
1133 |
23 Feb 08 |
peter |
36 |
#include "yat/classifier/MatrixLookup.h" |
675 |
10 Oct 06 |
jari |
37 |
|
781 |
05 Mar 07 |
peter |
38 |
#include <cassert> |
196 |
27 Oct 04 |
jari |
39 |
#include <cmath> |
1261 |
08 Apr 08 |
peter |
40 |
#include <cstddef> |
446 |
15 Dec 05 |
peter |
41 |
#include <cstdlib> |
42 |
26 Feb 04 |
jari |
42 |
#include <fstream> |
41 |
26 Feb 04 |
peter |
43 |
#include <iostream> |
446 |
15 Dec 05 |
peter |
44 |
#include <vector> |
41 |
26 Feb 04 |
peter |
45 |
|
680 |
11 Oct 06 |
jari |
46 |
using namespace theplu::yat; |
41 |
26 Feb 04 |
peter |
47 |
|
4200 |
19 Aug 22 |
peter |
48 |
bool test_MEV(const classifier::MatrixLookup& data, |
4200 |
19 Aug 22 |
peter |
49 |
const classifier::KernelFunction* kf, |
1121 |
22 Feb 08 |
peter |
50 |
const utility::Matrix& control, const double error_bound, |
1238 |
15 Mar 08 |
peter |
51 |
test::Suite& suite); |
513 |
18 Feb 06 |
peter |
52 |
|
4200 |
19 Aug 22 |
peter |
53 |
bool test_SEV(const classifier::MatrixLookup& data, |
4200 |
19 Aug 22 |
peter |
54 |
const classifier::KernelFunction* kf, |
1121 |
22 Feb 08 |
peter |
55 |
const utility::Matrix& control, const double error_bound, |
1238 |
15 Mar 08 |
peter |
56 |
test::Suite& suite); |
513 |
18 Feb 06 |
peter |
57 |
|
513 |
18 Feb 06 |
peter |
58 |
|
1238 |
15 Mar 08 |
peter |
59 |
int main(int argc, char* argv[]) |
513 |
18 Feb 06 |
peter |
60 |
|
4200 |
19 Aug 22 |
peter |
61 |
{ |
1238 |
15 Mar 08 |
peter |
62 |
test::Suite suite(argc, argv); |
1238 |
15 Mar 08 |
peter |
63 |
suite.err() << "testing kernels" << std::endl; |
513 |
18 Feb 06 |
peter |
64 |
|
1121 |
22 Feb 08 |
peter |
65 |
utility::Matrix data2_core(2,3); |
527 |
01 Mar 06 |
peter |
66 |
data2_core(0,0)=0; |
527 |
01 Mar 06 |
peter |
67 |
data2_core(1,0)=0; |
527 |
01 Mar 06 |
peter |
68 |
data2_core(0,1)=0; |
527 |
01 Mar 06 |
peter |
69 |
data2_core(1,1)=1; |
527 |
01 Mar 06 |
peter |
70 |
data2_core(0,2)=1; |
527 |
01 Mar 06 |
peter |
71 |
data2_core(1,2)=0; |
527 |
01 Mar 06 |
peter |
72 |
classifier::MatrixLookup data2(data2_core); |
4200 |
19 Aug 22 |
peter |
73 |
classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); |
527 |
01 Mar 06 |
peter |
74 |
classifier::Kernel_SEV kernel2(data2,*kf2); |
527 |
01 Mar 06 |
peter |
75 |
assert(kernel2.size()==3); |
4200 |
19 Aug 22 |
peter |
76 |
if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) || |
4200 |
19 Aug 22 |
peter |
77 |
kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) || |
527 |
01 Mar 06 |
peter |
78 |
kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){ |
1238 |
15 Mar 08 |
peter |
79 |
suite.add(false); |
1238 |
15 Mar 08 |
peter |
80 |
suite.err() << "Found:\n"; |
527 |
01 Mar 06 |
peter |
81 |
for (size_t i=0; i<3; i++){ |
527 |
01 Mar 06 |
peter |
82 |
for (size_t j=0; j<3; j++) |
1238 |
15 Mar 08 |
peter |
83 |
suite.err() << kernel2(i,j) << " "; |
1238 |
15 Mar 08 |
peter |
84 |
suite.err() << std::endl; |
527 |
01 Mar 06 |
peter |
85 |
} |
1238 |
15 Mar 08 |
peter |
86 |
suite.err() << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl; |
527 |
01 Mar 06 |
peter |
87 |
} |
527 |
01 Mar 06 |
peter |
88 |
classifier::KernelLookup kv2(kernel2); |
527 |
01 Mar 06 |
peter |
89 |
delete kf2; |
527 |
01 Mar 06 |
peter |
90 |
|
1238 |
15 Mar 08 |
peter |
// Peter, the hardcoded number below reflects precision in files |
1238 |
15 Mar 08 |
peter |
// containing kernel matrices calculated in matlab. |
4200 |
19 Aug 22 |
peter |
93 |
double error_bound = 1e-6; |
1251 |
03 Apr 08 |
peter |
94 |
std::ifstream is(test::filename("data/nm_data_centralized.txt").c_str()); |
1121 |
22 Feb 08 |
peter |
95 |
utility::Matrix data_core(is); |
513 |
18 Feb 06 |
peter |
96 |
is.close(); |
513 |
18 Feb 06 |
peter |
97 |
|
527 |
01 Mar 06 |
peter |
98 |
classifier::MatrixLookup data(data_core); |
527 |
01 Mar 06 |
peter |
99 |
|
1251 |
03 Apr 08 |
peter |
100 |
is.open(test::filename("data/nm_kernel.txt").c_str()); |
1121 |
22 Feb 08 |
peter |
101 |
utility::Matrix kernel_matlab(is); |
513 |
18 Feb 06 |
peter |
102 |
is.close(); |
4200 |
19 Aug 22 |
peter |
103 |
classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); |
1238 |
15 Mar 08 |
peter |
104 |
suite.add(test_MEV(data,kf,kernel_matlab,error_bound, suite) && |
1238 |
15 Mar 08 |
peter |
105 |
test_SEV(data,kf,kernel_matlab,error_bound, suite)); |
513 |
18 Feb 06 |
peter |
106 |
delete kf; |
4200 |
19 Aug 22 |
peter |
107 |
|
1251 |
03 Apr 08 |
peter |
108 |
is.open(test::filename("data/nm_kernel2.txt").c_str()); |
1121 |
22 Feb 08 |
peter |
109 |
utility::Matrix kernel_matlab2(is); |
513 |
18 Feb 06 |
peter |
110 |
is.close(); |
4200 |
19 Aug 22 |
peter |
111 |
kf = new classifier::PolynomialKernelFunction(2); |
1238 |
15 Mar 08 |
peter |
112 |
suite.add(test_MEV(data,kf,kernel_matlab2,error_bound, suite) && |
1238 |
15 Mar 08 |
peter |
113 |
test_SEV(data,kf,kernel_matlab2,error_bound, suite)); |
513 |
18 Feb 06 |
peter |
114 |
delete kf; |
513 |
18 Feb 06 |
peter |
115 |
|
814 |
16 Mar 07 |
peter |
// Checking that GaussianKernelFunction. |
1238 |
15 Mar 08 |
peter |
117 |
suite.err() << "Checking GaussianKernelFunction.\n"; |
1251 |
03 Apr 08 |
peter |
118 |
is.open(test::filename("data/nm_kernel_gaussian.txt").c_str()); |
1121 |
22 Feb 08 |
peter |
119 |
utility::Matrix kernel_gaussian(is); |
814 |
16 Mar 07 |
peter |
120 |
is.close(); |
4200 |
19 Aug 22 |
peter |
121 |
kf = new classifier::GaussianKernelFunction(100); |
1238 |
15 Mar 08 |
peter |
122 |
suite.add(test_MEV(data,kf,kernel_gaussian,error_bound, suite)); |
1238 |
15 Mar 08 |
peter |
123 |
suite.add(test_SEV(data,kf,kernel_gaussian,error_bound, suite)); |
513 |
18 Feb 06 |
peter |
124 |
delete kf; |
513 |
18 Feb 06 |
peter |
125 |
|
1098 |
18 Feb 08 |
jari |
126 |
data_core.resize(1,5); |
536 |
03 Mar 06 |
peter |
127 |
for (size_t i=0; i<data_core.columns(); i++) |
536 |
03 Mar 06 |
peter |
128 |
data_core(0,i)=i; |
536 |
03 Mar 06 |
peter |
129 |
data = classifier::MatrixLookup(data_core); |
536 |
03 Mar 06 |
peter |
130 |
classifier::PolynomialKernelFunction pkf; |
536 |
03 Mar 06 |
peter |
131 |
classifier::Kernel_SEV kernel(data,pkf); |
4200 |
19 Aug 22 |
peter |
132 |
|
1238 |
15 Mar 08 |
peter |
133 |
return suite.return_value(); |
513 |
18 Feb 06 |
peter |
134 |
} |
513 |
18 Feb 06 |
peter |
135 |
|
4200 |
19 Aug 22 |
peter |
136 |
bool test_MEV(const classifier::MatrixLookup& data, |
4200 |
19 Aug 22 |
peter |
137 |
const classifier::KernelFunction* kf, |
1121 |
22 Feb 08 |
peter |
138 |
const utility::Matrix& control, const double error_bound, |
1238 |
15 Mar 08 |
peter |
139 |
test::Suite& suite) |
307 |
03 May 05 |
peter |
140 |
{ |
1238 |
15 Mar 08 |
peter |
141 |
suite.err() << "testing Kernel_MEV" << std::endl; |
513 |
18 Feb 06 |
peter |
142 |
classifier::Kernel_MEV kernel(data,*kf); |
1261 |
08 Apr 08 |
peter |
143 |
for(size_t i=0;i<control.rows();i++) |
1261 |
08 Apr 08 |
peter |
144 |
for(size_t j=0;j<control.columns();j++) |
1704 |
08 Jan 09 |
peter |
145 |
if (!suite.equal_fix(kernel(i,j),control(i,j), error_bound) ){ |
1238 |
15 Mar 08 |
peter |
146 |
suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): " |
1238 |
15 Mar 08 |
peter |
147 |
<< kernel(i,j) |
1238 |
15 Mar 08 |
peter |
148 |
<< "\n and control(" << i << "," << j << "): " |
1238 |
15 Mar 08 |
peter |
149 |
<< control(i,j) << "\n" |
1238 |
15 Mar 08 |
peter |
150 |
<< "difference: " << kernel(i,j)-control(i,j) << "\n" |
1238 |
15 Mar 08 |
peter |
151 |
<< "expected less than " << error_bound << "\n"; |
307 |
03 May 05 |
peter |
152 |
return false; |
1238 |
15 Mar 08 |
peter |
153 |
} |
446 |
15 Dec 05 |
peter |
// checking view |
1134 |
23 Feb 08 |
peter |
155 |
std::vector<size_t> index_vec(3); |
1134 |
23 Feb 08 |
peter |
156 |
index_vec[0]=1; |
1134 |
23 Feb 08 |
peter |
157 |
index_vec[1]=2; |
1134 |
23 Feb 08 |
peter |
158 |
index_vec[2]=3; |
1134 |
23 Feb 08 |
peter |
159 |
utility::Index index(index_vec); |
1134 |
23 Feb 08 |
peter |
160 |
classifier::KernelLookup kv_raw(kernel); |
482 |
02 Jan 06 |
peter |
161 |
classifier::KernelLookup kv(kv_raw,index,index); |
463 |
16 Dec 05 |
peter |
162 |
if (kv.rows()!=index.size()){ |
1238 |
15 Mar 08 |
peter |
163 |
suite.err() << "Error: KernelLookup(kernel, index)\n" |
1238 |
15 Mar 08 |
peter |
164 |
<< "Size of KernelLookup is " << kv.rows() << "\n" |
1238 |
15 Mar 08 |
peter |
165 |
<< "expected " << index.size() << std::endl; |
4200 |
19 Aug 22 |
peter |
166 |
|
463 |
16 Dec 05 |
peter |
167 |
return false; |
463 |
16 Dec 05 |
peter |
168 |
} |
475 |
22 Dec 05 |
peter |
169 |
classifier::KernelLookup kv2(kernel); |
463 |
16 Dec 05 |
peter |
170 |
if (kv2.rows()!=kernel.size()){ |
1238 |
15 Mar 08 |
peter |
171 |
suite.err() << "Error: KernelLookup(kernel)\n" << std::endl |
475 |
22 Dec 05 |
peter |
172 |
<< "Size of KernelLookup is " << kv.rows() << std::endl |
463 |
16 Dec 05 |
peter |
173 |
<< "expected " << kernel.size() << std::endl; |
4200 |
19 Aug 22 |
peter |
174 |
|
463 |
16 Dec 05 |
peter |
175 |
return false; |
463 |
16 Dec 05 |
peter |
176 |
} |
307 |
03 May 05 |
peter |
177 |
return true; |
307 |
03 May 05 |
peter |
178 |
} |
307 |
03 May 05 |
peter |
179 |
|
4200 |
19 Aug 22 |
peter |
180 |
bool test_SEV(const classifier::MatrixLookup& data, |
4200 |
19 Aug 22 |
peter |
181 |
const classifier::KernelFunction* kf, |
1121 |
22 Feb 08 |
peter |
182 |
const utility::Matrix& control, const double error_bound, |
1238 |
15 Mar 08 |
peter |
183 |
test::Suite& suite) |
307 |
03 May 05 |
peter |
184 |
{ |
1238 |
15 Mar 08 |
peter |
185 |
suite.err() << "testing Kernel_SEV" << std::endl; |
453 |
15 Dec 05 |
peter |
186 |
classifier::Kernel_SEV kernel(data,*kf); |
1261 |
08 Apr 08 |
peter |
187 |
for(size_t i=0;i<control.rows();i++) |
1261 |
08 Apr 08 |
peter |
188 |
for(size_t j=0;j<control.columns();j++) |
1704 |
08 Jan 09 |
peter |
189 |
if (!suite.equal_fix(kernel(i,j), control(i,j), error_bound) ){ |
1238 |
15 Mar 08 |
peter |
190 |
suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): " |
1238 |
15 Mar 08 |
peter |
191 |
<< kernel(i,j) |
1238 |
15 Mar 08 |
peter |
192 |
<< "\n and control(" << i << "," << j << "): " |
1238 |
15 Mar 08 |
peter |
193 |
<< control(i,j) << "\n" |
1238 |
15 Mar 08 |
peter |
194 |
<< "difference: " << kernel(i,j)-control(i,j) << "\n" |
1238 |
15 Mar 08 |
peter |
195 |
<< "expected less than " << error_bound << "\n"; |
307 |
03 May 05 |
peter |
196 |
return false; |
1238 |
15 Mar 08 |
peter |
197 |
} |
307 |
03 May 05 |
peter |
198 |
|
446 |
15 Dec 05 |
peter |
// checking view |
1134 |
23 Feb 08 |
peter |
200 |
std::vector<size_t> index_vec(3); |
1134 |
23 Feb 08 |
peter |
201 |
index_vec[0]=1; |
1134 |
23 Feb 08 |
peter |
202 |
index_vec[1]=2; |
1134 |
23 Feb 08 |
peter |
203 |
index_vec[2]=3; |
1134 |
23 Feb 08 |
peter |
204 |
utility::Index index(index_vec); |
679 |
11 Oct 06 |
markus |
205 |
classifier::KernelLookup kv_raw(kernel); |
679 |
11 Oct 06 |
markus |
206 |
classifier::KernelLookup kv(kv_raw,index, index); |
463 |
16 Dec 05 |
peter |
207 |
if (kv.rows()!=index.size()){ |
1238 |
15 Mar 08 |
peter |
208 |
suite.err() << "Error: KernelLookup(kernel, index)\n" << std::endl |
475 |
22 Dec 05 |
peter |
209 |
<< "Size of KernelLookup is " << kv.rows() << std::endl |
463 |
16 Dec 05 |
peter |
210 |
<< "expected " << index.size() << std::endl; |
4200 |
19 Aug 22 |
peter |
211 |
|
463 |
16 Dec 05 |
peter |
212 |
return false; |
463 |
16 Dec 05 |
peter |
213 |
} |
475 |
22 Dec 05 |
peter |
214 |
classifier::KernelLookup kv2(kernel); |
463 |
16 Dec 05 |
peter |
215 |
if (kv2.rows()!=kernel.size()){ |
1238 |
15 Mar 08 |
peter |
216 |
suite.err() << "Error: KernelLookup(kernel)\n" << std::endl |
475 |
22 Dec 05 |
peter |
217 |
<< "Size of KernelLookup is " << kv.rows() << std::endl |
463 |
16 Dec 05 |
peter |
218 |
<< "expected " << kernel.size() << std::endl; |
4200 |
19 Aug 22 |
peter |
219 |
|
463 |
16 Dec 05 |
peter |
220 |
return false; |
463 |
16 Dec 05 |
peter |
221 |
} |
307 |
03 May 05 |
peter |
222 |
return true; |
307 |
03 May 05 |
peter |
223 |
} |