source: trunk/test/kernel_test.cc @ 1607

Last change on this file since 1607 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1// $Id: kernel_test.cc 1487 2008-09-10 08:41:36Z jari $
2
3/*
4  Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
6  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
7
8  This file is part of the yat library, http://dev.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 3 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with yat. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "Suite.h"
25
26#include "yat/utility/Matrix.h"
27#include "yat/classifier/KernelFunction.h"
28#include "yat/classifier/PolynomialKernelFunction.h"
29#include "yat/classifier/GaussianKernelFunction.h"
30#include "yat/classifier/KernelLookup.h"
31#include "yat/classifier/Kernel_MEV.h"
32#include "yat/classifier/Kernel_SEV.h"
33#include "yat/classifier/MatrixLookup.h"
34
35#include <cassert>
36#include <cmath>
37#include <cstddef>
38#include <cstdlib>
39#include <fstream>
40#include <iostream>
41#include <vector>
42
43using namespace theplu::yat;
44
45bool test_MEV(const classifier::MatrixLookup& data, 
46              const classifier::KernelFunction* kf, 
47              const utility::Matrix& control, const double error_bound,
48              test::Suite& suite);
49
50bool test_SEV(const classifier::MatrixLookup& data, 
51              const classifier::KernelFunction* kf, 
52              const utility::Matrix& control, const double error_bound,
53              test::Suite& suite);
54
55
56int main(int argc, char* argv[])
57
58{ 
59  test::Suite suite(argc, argv);
60  suite.err() << "testing kernels" << std::endl;
61
62  utility::Matrix data2_core(2,3);
63  data2_core(0,0)=0;
64  data2_core(1,0)=0;
65  data2_core(0,1)=0;
66  data2_core(1,1)=1;
67  data2_core(0,2)=1;
68  data2_core(1,2)=0;
69  classifier::MatrixLookup data2(data2_core);
70  classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); 
71  classifier::Kernel_SEV kernel2(data2,*kf2);
72  assert(kernel2.size()==3);
73  if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) || 
74     kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) || 
75     kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){
76    suite.add(false);
77    suite.err() << "Found:\n";
78    for (size_t i=0; i<3; i++){
79      for (size_t j=0; j<3; j++)
80        suite.err() << kernel2(i,j) << " ";
81      suite.err() << std::endl;
82    }
83    suite.err() << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
84  }
85  classifier::KernelLookup kv2(kernel2);
86  delete kf2;
87
88  // Peter, the hardcoded number below reflects precision in files
89  // containing kernel matrices calculated in matlab.
90  double error_bound = 1e-6; 
91  std::ifstream is(test::filename("data/nm_data_centralized.txt").c_str());
92  utility::Matrix data_core(is);
93  is.close();
94
95  classifier::MatrixLookup data(data_core);
96
97  is.open(test::filename("data/nm_kernel.txt").c_str());
98  utility::Matrix kernel_matlab(is);
99  is.close();
100  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
101  suite.add(test_MEV(data,kf,kernel_matlab,error_bound, suite) &&
102            test_SEV(data,kf,kernel_matlab,error_bound, suite));
103  delete kf;
104 
105  is.open(test::filename("data/nm_kernel2.txt").c_str());
106  utility::Matrix kernel_matlab2(is);
107  is.close();
108  kf = new classifier::PolynomialKernelFunction(2); 
109  suite.add(test_MEV(data,kf,kernel_matlab2,error_bound, suite) &&
110            test_SEV(data,kf,kernel_matlab2,error_bound, suite));
111  delete kf;
112
113  // Checking that GaussianKernelFunction.
114  suite.err() << "Checking GaussianKernelFunction.\n";
115  is.open(test::filename("data/nm_kernel_gaussian.txt").c_str());
116  utility::Matrix kernel_gaussian(is);
117  is.close();
118  kf = new classifier::GaussianKernelFunction(100); 
119  suite.add(test_MEV(data,kf,kernel_gaussian,error_bound, suite));
120  suite.add(test_SEV(data,kf,kernel_gaussian,error_bound, suite));
121  delete kf;
122
123  data_core.resize(1,5);
124  for (size_t i=0; i<data_core.columns(); i++)
125    data_core(0,i)=i;
126  data = classifier::MatrixLookup(data_core);
127  classifier::PolynomialKernelFunction pkf;
128  classifier::Kernel_SEV kernel(data,pkf);
129 
130  return suite.return_value();
131}
132
133bool test_MEV(const classifier::MatrixLookup& data, 
134              const classifier::KernelFunction* kf, 
135              const utility::Matrix& control, const double error_bound,
136              test::Suite& suite)
137{
138  suite.err() << "testing Kernel_MEV" << std::endl;
139  classifier::Kernel_MEV kernel(data,*kf);
140  for(size_t i=0;i<control.rows();i++)
141    for(size_t j=0;j<control.columns();j++)
142      if (std::abs(kernel(i,j)-control(i,j))>error_bound){
143        suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): "
144                    << kernel(i,j)
145                    << "\n and control(" << i << "," << j << "): "
146                    << control(i,j) << "\n"
147                    << "difference: " << kernel(i,j)-control(i,j) << "\n"
148                    << "expected less than " << error_bound << "\n";
149        return false;
150      }
151  // checking view
152  std::vector<size_t> index_vec(3);
153  index_vec[0]=1;
154  index_vec[1]=2;
155  index_vec[2]=3;
156  utility::Index index(index_vec);
157  classifier::KernelLookup kv_raw(kernel);
158  classifier::KernelLookup kv(kv_raw,index,index);
159  if (kv.rows()!=index.size()){
160    suite.err() << "Error: KernelLookup(kernel, index)\n"
161                << "Size of KernelLookup is " << kv.rows() << "\n"
162                << "expected " << index.size() << std::endl;
163   
164    return false;
165  }
166  classifier::KernelLookup kv2(kernel);
167  if (kv2.rows()!=kernel.size()){
168    suite.err() << "Error: KernelLookup(kernel)\n" << std::endl
169           << "Size of KernelLookup is " << kv.rows() << std::endl
170           << "expected " << kernel.size() << std::endl;
171   
172    return false;
173  }
174  return true;
175}
176
177bool test_SEV(const classifier::MatrixLookup& data, 
178              const classifier::KernelFunction* kf, 
179              const utility::Matrix& control, const double error_bound,
180              test::Suite& suite)
181{
182  suite.err() << "testing Kernel_SEV" << std::endl;
183  classifier::Kernel_SEV kernel(data,*kf);
184  for(size_t i=0;i<control.rows();i++)
185    for(size_t j=0;j<control.columns();j++)
186      if (std::abs(kernel(i,j)-control(i,j))>error_bound){
187        suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): "
188                    << kernel(i,j)
189                    << "\n and control(" << i << "," << j << "): "
190                    << control(i,j) << "\n"
191                    << "difference: " << kernel(i,j)-control(i,j) << "\n"
192                    << "expected less than " << error_bound << "\n";
193        return false;
194      }
195
196  // checking view
197  std::vector<size_t> index_vec(3);
198  index_vec[0]=1;
199  index_vec[1]=2;
200  index_vec[2]=3;
201  utility::Index index(index_vec);
202  classifier::KernelLookup kv_raw(kernel);
203  classifier::KernelLookup kv(kv_raw,index, index);
204  if (kv.rows()!=index.size()){
205    suite.err() << "Error: KernelLookup(kernel, index)\n" << std::endl
206           << "Size of KernelLookup is " << kv.rows() << std::endl
207           << "expected " << index.size() << std::endl;
208   
209    return false;
210  }
211  classifier::KernelLookup kv2(kernel);
212  if (kv2.rows()!=kernel.size()){
213    suite.err() << "Error: KernelLookup(kernel)\n" << std::endl
214           << "Size of KernelLookup is " << kv.rows() << std::endl
215           << "expected " << kernel.size() << std::endl;
216   
217    return false;
218  }
219  return true;
220}
221
222
Note: See TracBrowser for help on using the repository browser.