Logo ROOT   6.07/09
Reference Guide
SVKernelFunction.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andrzej Zemla
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : SVKernelFunction *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation *
12  * *
13  * Authors (alphabetical): *
14  * Marcin Wolter <Marcin.Wolter@cern.ch> - IFJ PAN, Krakow, Poland *
15  * Andrzej Zemla <azemla@cern.ch> - IFJ PAN, Krakow, Poland *
16  * (IFJ PAN: Henryk Niewodniczanski Inst. Nucl. Physics, Krakow, Poland) *
17  * *
18  * MultiGaussian, Product and Sum kernels included: *
19  * Adrian Bevan <adrian.bevan@cern.ch> - Queen Mary *
20  * University of London, UK *
21  * Tom Stevenson <thomas.james.stevenson@cern.ch> - Queen Mary *
22  * University of London, UK *
23  * *
24  * Copyright (c) 2005: *
25  * CERN, Switzerland *
26  * MPI-K Heidelberg, Germany *
27  * PAN, Krakow, Poland *
28  * *
29  * Redistribution and use in source and binary forms, with or without *
30  * modification, are permitted according to the terms listed in LICENSE *
31  * (http://tmva.sourceforge.net/LICENSE) *
32  **********************************************************************************/
33 
34 #include "TMVA/SVKernelFunction.h"
35 #include "TMVA/SVEvent.h"
36 #include "TMath.h"
37 #include <vector>
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// constructor
41 
43  : fGamma(0.),
44  fKernel(kRBF), // kernel, order, theta, and kappa are for backward compatibility
45  fOrder(0),
46  fTheta(0),
47  fKappa(0)
48 {
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// constructor
53 
55  : fGamma(gamma),
56  fKernel(kRBF), // kernel, order, theta, and kappa are for backward compatibility
57  fOrder(0),
58  fTheta(0),
59  fKappa(0)
60 {
61  fmGamma.clear();
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// constructor
66 
68  : fKernel(k)
69 {
70  if (k==kRBF) { fGamma = param1; }
71  else if (k==kPolynomial){
72  fOrder = param1;
73  fTheta = param2;
74  }
75  fKernelsList.clear();
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// constructor
80 
81 TMVA::SVKernelFunction::SVKernelFunction( std::vector<float> params ) :
83 {
84  fmGamma.clear();
85  for( std::vector<float>::const_iterator iter = params.begin(); iter != params.end()\
86  ; ++iter ){
87  fmGamma.push_back(*iter);
88  }
89  //fKernelsList.clear();
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// constructor
94 
95 TMVA::SVKernelFunction::SVKernelFunction(EKernelType k, std::vector<EKernelType> kernels, std::vector<Float_t> gammas, Float_t gamma, Float_t order, Float_t theta) :
96  fGamma(gamma),
97  fKernel(k),
98  fOrder(order),
99  fTheta(theta)
100 {
101  fmGamma.clear();
102  fKernelsList.clear();
103  fKernelsList = kernels;
104  fmGamma = gammas;
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// destructor
109 
111 {
112  fmGamma.clear();
113  fKernelsList.clear();
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// set old options for compatibility mode
118 
120  fKernel = k;
121  fOrder = order;
122  fTheta = theta;
123  fKappa = kappa;
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 
129 {
130  switch(fKernel) {
131  case kRBF:
132  {
133  std::vector<Float_t> *v1 = ev1->GetDataVector();
134  std::vector<Float_t> *v2 = ev2->GetDataVector();
135 
136  Float_t norm = 0;
137  for (UInt_t i = 0; i < v1->size(); i++) norm += ((*v1)[i] -(*v2)[i]) *((*v1)[i] -(*v2)[i]) ;
138 
139  return TMath::Exp(-norm*fGamma);
140  }
141  case kMultiGauss:
142  {
143  // Kernel function with a kernel parameter gamma for
144  // each input variable. Described in "An Introduction to
145  // Support Vector Machines and Other Kernel-based Learning
146  // Methods" by Cristianini and Shawe-Taylor, Section 3.5
147  std::vector<Float_t> *v1 = ev1->GetDataVector();
148  std::vector<Float_t> *v2 = ev2->GetDataVector();
149  if(fmGamma.size() != v1->size()){
150  std::cout << "Fewer gammas than input variables! #Gammas= " << fmGamma.size() << " #Input variables= " << v1->size() << std::endl;
151  std::cout << "***> abort program execution" << std::endl;
152  exit(1);
153  }
154 
155  Float_t result = 1.;
156  for (UInt_t i = 0; i < v1->size(); i++) {
157  result *= TMath::Exp( -((*v1)[i] -(*v2)[i])*((*v1)[i] -(*v2)[i])*fmGamma[i] );
158  }
159  return result;
160  }
161  case kPolynomial:
162  {
163  // Polynomial kernel of form (z.x + theta)^n
164  // it should be noted that the power is currently only integer
165  std::vector<Float_t> *v1 = ev1->GetDataVector();
166  std::vector<Float_t> *v2 = ev2->GetDataVector();
167  Float_t prod = fTheta;
168  for (UInt_t idx = 0; idx < v1->size(); idx++) prod += (*v1)[idx] * (*v2)[idx];
169 
170  Float_t result = 1.;
171  Int_t i = fOrder;
172  result = TMath::Power(prod,i);
173  return result;
174  }
175  case kLinear:
176  {
177  // This is legacy code. The linear polynomial is a special case
178  // of the polynomial with order=1 and theta=0.
179  std::vector<Float_t> *v1 = ev1->GetDataVector();
180  std::vector<Float_t> *v2 = ev2->GetDataVector();
181  Float_t prod = 0;
182  for (UInt_t i = 0; i < v1->size(); i++) prod += (*v1)[i] * (*v2)[i];
183  return prod;
184  }
185  case kSigmoidal:
186  {
187  // This kernel doesn't always result in a positive-semidefinite Gram
188  // matrix so should be used with caution and therefore not
189  // currently accessible. This is not a valid Mercer kernel
190  std::vector<Float_t> *v1 = ev1->GetDataVector();
191  std::vector<Float_t> *v2 = ev2->GetDataVector();
192  Float_t prod = 0;
193  for (UInt_t i = 0; i < v1->size(); i++) prod += ((*v1)[i] -(*v2)[i]) *((*v1)[i] -(*v2)[i]) ;
194  prod *= fKappa;
195  prod += fTheta;
196  return TMath::TanH( prod );
197  }
198  case kProd:
199  {
200  // Calculate product of kernels by looping over list of kernels
201  // and evaluating the value for each, setting kernel back to
202  // kProd before returning so it can be used again. Described in "An Introduction to // Support Vector Machines and Other Kernel-based Learning
203  // Methods" by Cristianini and Shawe-Taylor, Section 3.3.2
204  Float_t kernelVal;
205  kernelVal = 1;
206  for(UInt_t i = 0; i<fKernelsList.size(); i++){
207  fKernel = fKernelsList.at(i);
208  Float_t a = Evaluate(ev1,ev2);
209  kernelVal *= a;
210  }
211  fKernel = kProd;
212  return kernelVal;
213  }
214  case kSum:
215  {
216  // Calculate sum of kernels by looping over list of kernels
217  // and evaluating the value for each, setting kernel back to
218  // kSum before returning so it can be used again. Described in "An Introduction to // Support Vector Machines and Other Kernel-based Learning
219  // Methods" by Cristianini and Shawe-Taylor, Section 3.3.2
220  Float_t kernelVal = 0;
221  for(UInt_t i = 0; i<fKernelsList.size(); i++){
222  fKernel = fKernelsList.at(i);
223  Float_t a = Evaluate(ev1,ev2);
224  kernelVal += a;
225  }
226  fKernel = kSum;
227  return kernelVal;
228  }
229  }
230  return 0;
231 }
232 
Double_t TanH(Double_t)
Definition: TMath.h:436
float Float_t
Definition: RtypesCore.h:53
int Int_t
Definition: RtypesCore.h:41
TArc * a
Definition: textangle.C:12
void setCompatibilityParams(EKernelType k, UInt_t order, Float_t theta, Float_t kappa)
set old options for compatibility mode
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:501
std::vector< EKernelType > fKernelsList
double gamma(double x)
unsigned int UInt_t
Definition: RtypesCore.h:42
Double_t Exp(Double_t x)
Definition: TMath.h:495
std::vector< Float_t > fmGamma
double result[121]
std::vector< Float_t > * GetDataVector()
Definition: SVEvent.h:62
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
Float_t Evaluate(SVEvent *ev1, SVEvent *ev2)
SVKernelFunction()
constructor