source: trunk/test/kernel_test.cc @ 781

Last change on this file since 781 was 781, checked in by Peter, 15 years ago

changing name to ROCScore and also added some cassert includes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1// $Id: kernel_test.cc 781 2007-03-05 19:44:03Z peter $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/utility/matrix.h"
25#include "yat/classifier/KernelFunction.h"
26#include "yat/classifier/PolynomialKernelFunction.h"
27#include "yat/classifier/GaussianKernelFunction.h"
28#include "yat/classifier/KernelLookup.h"
29#include "yat/classifier/Kernel_MEV.h"
30#include "yat/classifier/Kernel_SEV.h"
31
32#include <cassert>
33#include <cmath>
34#include <cstdlib>
35#include <fstream>
36#include <iostream>
37#include <vector>
38
39using namespace theplu::yat;
40
41bool test_MEV(const classifier::MatrixLookup& data, 
42              const classifier::KernelFunction* kf, 
43              const utility::matrix& control, const double error_bound,
44              std::ostream* error);
45
46bool test_SEV(const classifier::MatrixLookup& data, 
47              const classifier::KernelFunction* kf, 
48              const utility::matrix& control, const double error_bound,
49              std::ostream* error);
50
51
52int main(const int argc,const char* argv[])
53
54{ 
55  std::ostream* error;
56  if (argc>1 && argv[1]==std::string("-v"))
57    error = &std::cerr;
58  else {
59    error = new std::ofstream("/dev/null");
60    if (argc>1)
61      std::cout << "kernel_test -v : for printing extra information\n";
62  }
63  *error << "testing kernels" << std::endl;
64  bool ok = true;
65
66  utility::matrix data2_core(2,3);
67  data2_core(0,0)=0;
68  data2_core(1,0)=0;
69  data2_core(0,1)=0;
70  data2_core(1,1)=1;
71  data2_core(0,2)=1;
72  data2_core(1,2)=0;
73  classifier::MatrixLookup data2(data2_core);
74  classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); 
75  classifier::Kernel_SEV kernel2(data2,*kf2);
76  assert(kernel2.size()==3);
77  if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) || 
78     kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) || 
79     kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){
80    ok = false;
81    *error << "Found:\n";
82    for (size_t i=0; i<3; i++){
83      for (size_t j=0; j<3; j++)
84        *error << kernel2(i,j) << " ";
85      *error << std::endl;
86    }
87    *error << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
88  }
89  classifier::KernelLookup kv2(kernel2);
90  delete kf2;
91
92  // Peter, the hardcoded number below should be changed.
93  double error_bound = 1e-8; 
94  std::ifstream is("data/nm_data_centralized.txt");
95  utility::matrix data_core(is);
96  is.close();
97
98  classifier::MatrixLookup data(data_core);
99
100  is.open("data/nm_kernel.txt");
101  utility::matrix kernel_matlab(is);
102  is.close();
103  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
104  ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound, error)
105        & test_SEV(data,kf,kernel_matlab,error_bound, error));
106  delete kf;
107 
108  is.open("data/nm_kernel2.txt");
109  utility::matrix kernel_matlab2(is);
110  is.close();
111  kf = new classifier::PolynomialKernelFunction(2); 
112  ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound, error)
113        & test_SEV(data,kf,kernel_matlab2,error_bound, error));
114  delete kf;
115
116  // Checking that a GaussianKernelFunction object can be built.
117  kf = new classifier::GaussianKernelFunction(); 
118  delete kf;
119
120  data_core = utility::matrix(1,5);
121  for (size_t i=0; i<data_core.columns(); i++)
122    data_core(0,i)=i;
123  data = classifier::MatrixLookup(data_core);
124  classifier::PolynomialKernelFunction pkf;
125  classifier::Kernel_SEV kernel(data,pkf);
126 
127
128  if (error!=&std::cerr)
129    delete error;
130
131  if (ok=true) 
132    return 0;
133  return -1;
134}
135
136bool test_MEV(const classifier::MatrixLookup& data, 
137              const classifier::KernelFunction* kf, 
138              const utility::matrix& control, const double error_bound,
139              std::ostream* error)
140{
141  *error << "testing Kernel_MEV" << std::endl;
142  classifier::Kernel_MEV kernel(data,*kf);
143  for(u_int i=0;i<control.rows();i++)
144    for(u_int j=0;j<control.columns();j++)
145      if (fabs(kernel(i,j)-control(i,j))>error_bound)
146        return false;
147
148  // checking view
149  std::vector<size_t> index(3);
150  index[0]=1;
151  index[1]=2;
152  index[2]=3;
153  classifier::KernelLookup kv_raw(kernel);
154  classifier::KernelLookup kv(kv_raw,index,index);
155  if (kv.rows()!=index.size()){
156    *error << "Error: KernelLookup(kernel, index)\n" << std::endl
157           << "Size of KernelLookup is " << kv.rows() << std::endl
158           << "expected " << index.size() << std::endl;
159   
160    return false;
161  }
162  classifier::KernelLookup kv2(kernel);
163  if (kv2.rows()!=kernel.size()){
164    *error << "Error: KernelLookup(kernel)\n" << std::endl
165           << "Size of KernelLookup is " << kv.rows() << std::endl
166           << "expected " << kernel.size() << std::endl;
167   
168    return false;
169  }
170
171  return true;
172}
173
174bool test_SEV(const classifier::MatrixLookup& data, 
175              const classifier::KernelFunction* kf, 
176              const utility::matrix& control, const double error_bound,
177              std::ostream* error)
178{
179  *error << "testing Kernel_SEV" << std::endl;
180  classifier::Kernel_SEV kernel(data,*kf);
181  for(u_int i=0;i<control.rows();i++)
182    for(u_int j=0;j<control.columns();j++)
183      if (fabs(kernel(i,j)-control(i,j))>error_bound)
184        return false;
185
186  // checking view
187  std::vector<size_t> index(3);
188  index[0]=1;
189  index[1]=2;
190  index[2]=3;
191  classifier::KernelLookup kv_raw(kernel);
192  classifier::KernelLookup kv(kv_raw,index, index);
193  if (kv.rows()!=index.size()){
194    *error << "Error: KernelLookup(kernel, index)\n" << std::endl
195           << "Size of KernelLookup is " << kv.rows() << std::endl
196           << "expected " << index.size() << std::endl;
197   
198    return false;
199  }
200  classifier::KernelLookup kv2(kernel);
201  if (kv2.rows()!=kernel.size()){
202    *error << "Error: KernelLookup(kernel)\n" << std::endl
203           << "Size of KernelLookup is " << kv.rows() << std::endl
204           << "expected " << kernel.size() << std::endl;
205   
206    return false;
207  }
208  return true;
209}
210
211
Note: See TracBrowser for help on using the repository browser.