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