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 "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 |
---|