source: trunk/test/kernel_test.cc @ 1251

Last change on this file since 1251 was 1251, checked in by Peter, 13 years ago

make distcheck works - fixes ticket:217

  • 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 1251 2008-04-03 05:48:24Z peter $
2
3/*
4  Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
6  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
7
8  This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "Suite.h"
27
28#include "yat/utility/Matrix.h"
29#include "yat/classifier/KernelFunction.h"
30#include "yat/classifier/PolynomialKernelFunction.h"
31#include "yat/classifier/GaussianKernelFunction.h"
32#include "yat/classifier/KernelLookup.h"
33#include "yat/classifier/Kernel_MEV.h"
34#include "yat/classifier/Kernel_SEV.h"
35#include "yat/classifier/MatrixLookup.h"
36
37#include <cassert>
38#include <cmath>
39#include <cstdlib>
40#include <fstream>
41#include <iostream>
42#include <vector>
43
44using namespace theplu::yat;
45
46bool test_MEV(const classifier::MatrixLookup& data, 
47              const classifier::KernelFunction* kf, 
48              const utility::Matrix& control, const double error_bound,
49              test::Suite& suite);
50
51bool test_SEV(const classifier::MatrixLookup& data, 
52              const classifier::KernelFunction* kf, 
53              const utility::Matrix& control, const double error_bound,
54              test::Suite& suite);
55
56
57int main(int argc, char* argv[])
58
59{ 
60  test::Suite suite(argc, argv);
61  suite.err() << "testing kernels" << std::endl;
62
63  utility::Matrix data2_core(2,3);
64  data2_core(0,0)=0;
65  data2_core(1,0)=0;
66  data2_core(0,1)=0;
67  data2_core(1,1)=1;
68  data2_core(0,2)=1;
69  data2_core(1,2)=0;
70  classifier::MatrixLookup data2(data2_core);
71  classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); 
72  classifier::Kernel_SEV kernel2(data2,*kf2);
73  assert(kernel2.size()==3);
74  if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) || 
75     kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) || 
76     kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){
77    suite.add(false);
78    suite.err() << "Found:\n";
79    for (size_t i=0; i<3; i++){
80      for (size_t j=0; j<3; j++)
81        suite.err() << kernel2(i,j) << " ";
82      suite.err() << std::endl;
83    }
84    suite.err() << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
85  }
86  classifier::KernelLookup kv2(kernel2);
87  delete kf2;
88
89  // Peter, the hardcoded number below reflects precision in files
90  // containing kernel matrices calculated in matlab.
91  double error_bound = 1e-6; 
92  std::ifstream is(test::filename("data/nm_data_centralized.txt").c_str());
93  utility::Matrix data_core(is);
94  is.close();
95
96  classifier::MatrixLookup data(data_core);
97
98  is.open(test::filename("data/nm_kernel.txt").c_str());
99  utility::Matrix kernel_matlab(is);
100  is.close();
101  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
102  suite.add(test_MEV(data,kf,kernel_matlab,error_bound, suite) &&
103            test_SEV(data,kf,kernel_matlab,error_bound, suite));
104  delete kf;
105 
106  is.open(test::filename("data/nm_kernel2.txt").c_str());
107  utility::Matrix kernel_matlab2(is);
108  is.close();
109  kf = new classifier::PolynomialKernelFunction(2); 
110  suite.add(test_MEV(data,kf,kernel_matlab2,error_bound, suite) &&
111            test_SEV(data,kf,kernel_matlab2,error_bound, suite));
112  delete kf;
113
114  // Checking that GaussianKernelFunction.
115  suite.err() << "Checking GaussianKernelFunction.\n";
116  is.open(test::filename("data/nm_kernel_gaussian.txt").c_str());
117  utility::Matrix kernel_gaussian(is);
118  is.close();
119  kf = new classifier::GaussianKernelFunction(100); 
120  suite.add(test_MEV(data,kf,kernel_gaussian,error_bound, suite));
121  suite.add(test_SEV(data,kf,kernel_gaussian,error_bound, suite));
122  delete kf;
123
124  data_core.resize(1,5);
125  for (size_t i=0; i<data_core.columns(); i++)
126    data_core(0,i)=i;
127  data = classifier::MatrixLookup(data_core);
128  classifier::PolynomialKernelFunction pkf;
129  classifier::Kernel_SEV kernel(data,pkf);
130 
131  return suite.return_value();
132}
133
134bool test_MEV(const classifier::MatrixLookup& data, 
135              const classifier::KernelFunction* kf, 
136              const utility::Matrix& control, const double error_bound,
137              test::Suite& suite)
138{
139  suite.err() << "testing Kernel_MEV" << std::endl;
140  classifier::Kernel_MEV kernel(data,*kf);
141  for(u_int i=0;i<control.rows();i++)
142    for(u_int j=0;j<control.columns();j++)
143      if (std::abs(kernel(i,j)-control(i,j))>error_bound){
144        suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): "
145                    << kernel(i,j)
146                    << "\n and control(" << i << "," << j << "): "
147                    << control(i,j) << "\n"
148                    << "difference: " << kernel(i,j)-control(i,j) << "\n"
149                    << "expected less than " << error_bound << "\n";
150        return false;
151      }
152  // checking view
153  std::vector<size_t> index_vec(3);
154  index_vec[0]=1;
155  index_vec[1]=2;
156  index_vec[2]=3;
157  utility::Index index(index_vec);
158  classifier::KernelLookup kv_raw(kernel);
159  classifier::KernelLookup kv(kv_raw,index,index);
160  if (kv.rows()!=index.size()){
161    suite.err() << "Error: KernelLookup(kernel, index)\n"
162                << "Size of KernelLookup is " << kv.rows() << "\n"
163                << "expected " << index.size() << std::endl;
164   
165    return false;
166  }
167  classifier::KernelLookup kv2(kernel);
168  if (kv2.rows()!=kernel.size()){
169    suite.err() << "Error: KernelLookup(kernel)\n" << std::endl
170           << "Size of KernelLookup is " << kv.rows() << std::endl
171           << "expected " << kernel.size() << std::endl;
172   
173    return false;
174  }
175  return true;
176}
177
178bool test_SEV(const classifier::MatrixLookup& data, 
179              const classifier::KernelFunction* kf, 
180              const utility::Matrix& control, const double error_bound,
181              test::Suite& suite)
182{
183  suite.err() << "testing Kernel_SEV" << std::endl;
184  classifier::Kernel_SEV kernel(data,*kf);
185  for(u_int i=0;i<control.rows();i++)
186    for(u_int j=0;j<control.columns();j++)
187      if (std::abs(kernel(i,j)-control(i,j))>error_bound){
188        suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): "
189                    << kernel(i,j)
190                    << "\n and control(" << i << "," << j << "): "
191                    << control(i,j) << "\n"
192                    << "difference: " << kernel(i,j)-control(i,j) << "\n"
193                    << "expected less than " << error_bound << "\n";
194        return false;
195      }
196
197  // checking view
198  std::vector<size_t> index_vec(3);
199  index_vec[0]=1;
200  index_vec[1]=2;
201  index_vec[2]=3;
202  utility::Index index(index_vec);
203  classifier::KernelLookup kv_raw(kernel);
204  classifier::KernelLookup kv(kv_raw,index, index);
205  if (kv.rows()!=index.size()){
206    suite.err() << "Error: KernelLookup(kernel, index)\n" << std::endl
207           << "Size of KernelLookup is " << kv.rows() << std::endl
208           << "expected " << index.size() << std::endl;
209   
210    return false;
211  }
212  classifier::KernelLookup kv2(kernel);
213  if (kv2.rows()!=kernel.size()){
214    suite.err() << "Error: KernelLookup(kernel)\n" << std::endl
215           << "Size of KernelLookup is " << kv.rows() << std::endl
216           << "expected " << kernel.size() << std::endl;
217   
218    return false;
219  }
220  return true;
221}
222
223
Note: See TracBrowser for help on using the repository browser.