source: trunk/c++_tools/classifier/GaussianKernelFunction.cc @ 675

Last change on this file since 675 was 675, checked in by Jari Häkkinen, 17 years ago

References #83. Changing project name to yat. Compilation will fail in this revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 2.7 KB
Line 
1// $Id$
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/classifier/GaussianKernelFunction.h"
25
26#include "yat/classifier/KernelFunction.h"
27#include "yat/classifier/DataLookup1D.h"
28#include "yat/classifier/DataLookupWeighted1D.h"
29
30#include <math.h>
31
32namespace theplu {
33namespace classifier { 
34
35  GaussianKernelFunction::GaussianKernelFunction(double sigma) 
36    : KernelFunction(), sigma2_(sigma*sigma)
37  {
38  }
39
40  double GaussianKernelFunction::operator()(const DataLookup1D& x,
41                                            const DataLookup1D& y) const
42  {
43    assert(x.size()==y.size());
44    double d2 = 0;
45    for (size_t i=0; i<x.size(); i++){
46      double d = x(i)-y(i);
47      d2 += d*d;
48    }
49    return exp(-d2/sigma2_); 
50  }
51
52
53  double GaussianKernelFunction::operator()(const DataLookup1D& x,
54                                            const DataLookupWeighted1D& y) const
55  {
56    assert(x.size()==y.size());
57    double d2 = 0;
58    double normalization_factor = 0;
59    for (size_t i=0; i<x.size(); i++) {
60      // ignoring Nan with accompanied weight zero
61      if (y.weight(i)){
62        d2 += y.weight(i) * (x(i)-y.data(i)) * (x(i)-y.data(i));
63        normalization_factor += y.weight(i);
64      }
65    }
66    // to make it coherent with no weight case
67    normalization_factor /= x.size(); 
68    return exp(d2/normalization_factor/sigma2_);
69  }
70
71
72  double GaussianKernelFunction::operator()(const DataLookupWeighted1D& x,
73                                            const DataLookupWeighted1D& y) const
74  {
75    assert(x.size()==y.size());
76    double d2 = 0;
77    double normalization_factor = 0;
78    for (size_t i=0; i<x.size(); i++) {
79      // ignoring Nan with accompanied weight zero
80      if (x.weight(i) && y.weight(i)){
81        d2 += x.weight(i) * y.weight(i) * (x.data(i)-y.data(i)) * 
82          (x.data(i)-y.data(i));
83        normalization_factor += x.weight(i) * y.weight(i);
84      }
85    }
86    // to make it coherent with no weight case
87    normalization_factor /= x.size(); 
88    return exp(d2/normalization_factor/sigma2_);
89  }
90
91
92}} // of namespace svn and namespace theplu
Note: See TracBrowser for help on using the repository browser.