1 | // $Id$ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2005 Peter Johansson |
---|
6 | Copyright (C) 2006, 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 "GaussianKernelFunction.h" |
---|
27 | #include "KernelFunction.h" |
---|
28 | #include "DataLookup1D.h" |
---|
29 | #include "DataLookupWeighted1D.h" |
---|
30 | |
---|
31 | #include <cassert> |
---|
32 | #include <math.h> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace classifier { |
---|
37 | |
---|
38 | GaussianKernelFunction::GaussianKernelFunction(double sigma) |
---|
39 | : KernelFunction(), sigma2_(sigma*sigma) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | double GaussianKernelFunction::operator()(const DataLookup1D& x, |
---|
44 | const DataLookup1D& y) const |
---|
45 | { |
---|
46 | assert(x.size()==y.size()); |
---|
47 | double d2 = 0; |
---|
48 | for (size_t i=0; i<x.size(); i++){ |
---|
49 | double d = x(i)-y(i); |
---|
50 | d2 += d*d; |
---|
51 | } |
---|
52 | return exp(-d2/sigma2_); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | double GaussianKernelFunction::operator()(const DataLookup1D& x, |
---|
57 | const DataLookupWeighted1D& y) const |
---|
58 | { |
---|
59 | assert(x.size()==y.size()); |
---|
60 | double d2 = 0; |
---|
61 | double normalization_factor = 0; |
---|
62 | for (size_t i=0; i<x.size(); i++) { |
---|
63 | // ignoring Nan with accompanied weight zero |
---|
64 | if (y.weight(i)){ |
---|
65 | d2 += y.weight(i) * (x(i)-y.data(i)) * (x(i)-y.data(i)); |
---|
66 | normalization_factor += y.weight(i); |
---|
67 | } |
---|
68 | } |
---|
69 | // to make it coherent with no weight case |
---|
70 | normalization_factor /= x.size(); |
---|
71 | return exp(d2/normalization_factor/sigma2_); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | double GaussianKernelFunction::operator()(const DataLookupWeighted1D& x, |
---|
76 | const DataLookupWeighted1D& y) const |
---|
77 | { |
---|
78 | assert(x.size()==y.size()); |
---|
79 | double d2 = 0; |
---|
80 | double normalization_factor = 0; |
---|
81 | for (size_t i=0; i<x.size(); i++) { |
---|
82 | // ignoring Nan with accompanied weight zero |
---|
83 | if (x.weight(i) && y.weight(i)){ |
---|
84 | d2 += x.weight(i) * y.weight(i) * (x.data(i)-y.data(i)) * |
---|
85 | (x.data(i)-y.data(i)); |
---|
86 | normalization_factor += x.weight(i) * y.weight(i); |
---|
87 | } |
---|
88 | } |
---|
89 | // to make it coherent with no weight case |
---|
90 | normalization_factor /= x.size(); |
---|
91 | return exp(d2/normalization_factor/sigma2_); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | }}} // of namespace svn, yat, and theplu |
---|