1 | // $Id$ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2005 Peter Johansson |
---|
6 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
7 | Copyright (C) 2007 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "GaussianKernelFunction.h" |
---|
28 | #include "KernelFunction.h" |
---|
29 | #include "DataLookup1D.h" |
---|
30 | #include "DataLookupWeighted1D.h" |
---|
31 | |
---|
32 | #include <cassert> |
---|
33 | #include <math.h> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace classifier { |
---|
38 | |
---|
39 | GaussianKernelFunction::GaussianKernelFunction(double sigma) |
---|
40 | : KernelFunction(), sigma2_(sigma*sigma) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | double GaussianKernelFunction::operator()(const DataLookup1D& x, |
---|
45 | const DataLookup1D& y) const |
---|
46 | { |
---|
47 | assert(x.size()==y.size()); |
---|
48 | double d2 = 0; |
---|
49 | for (size_t i=0; i<x.size(); i++){ |
---|
50 | double d = x(i)-y(i); |
---|
51 | d2 += d*d; |
---|
52 | } |
---|
53 | return exp(-d2/sigma2_); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | double GaussianKernelFunction::operator()(const DataLookup1D& x, |
---|
58 | const DataLookupWeighted1D& y) const |
---|
59 | { |
---|
60 | assert(x.size()==y.size()); |
---|
61 | double d2 = 0; |
---|
62 | double normalization_factor = 0; |
---|
63 | for (size_t i=0; i<x.size(); i++) { |
---|
64 | // ignoring Nan with accompanied weight zero |
---|
65 | if (y.weight(i)){ |
---|
66 | d2 += y.weight(i) * (x(i)-y.data(i)) * (x(i)-y.data(i)); |
---|
67 | normalization_factor += y.weight(i); |
---|
68 | } |
---|
69 | } |
---|
70 | // to make it coherent with no weight case |
---|
71 | normalization_factor /= x.size(); |
---|
72 | return exp(d2/normalization_factor/sigma2_); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | double GaussianKernelFunction::operator()(const DataLookupWeighted1D& x, |
---|
77 | const DataLookupWeighted1D& y) const |
---|
78 | { |
---|
79 | assert(x.size()==y.size()); |
---|
80 | double d2 = 0; |
---|
81 | double normalization_factor = 0; |
---|
82 | for (size_t i=0; i<x.size(); i++) { |
---|
83 | // ignoring Nan with accompanied weight zero |
---|
84 | if (x.weight(i) && y.weight(i)){ |
---|
85 | d2 += x.weight(i) * y.weight(i) * (x.data(i)-y.data(i)) * |
---|
86 | (x.data(i)-y.data(i)); |
---|
87 | normalization_factor += x.weight(i) * y.weight(i); |
---|
88 | } |
---|
89 | } |
---|
90 | // to make it coherent with no weight case |
---|
91 | normalization_factor /= x.size(); |
---|
92 | return exp(d2/normalization_factor/sigma2_); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | }}} // of namespace svn, yat, and theplu |
---|