33 |
22 Jan 04 |
peter |
// $Id$ |
33 |
22 Jan 04 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
831 |
27 Mar 07 |
peter |
Copyright (C) 2005 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2012 Peter Johansson |
33 |
22 Jan 04 |
peter |
10 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
33 |
22 Jan 04 |
peter |
12 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
17 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
22 |
|
675 |
10 Oct 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
675 |
10 Oct 06 |
jari |
25 |
*/ |
675 |
10 Oct 06 |
jari |
26 |
|
2881 |
18 Nov 12 |
peter |
27 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
28 |
|
680 |
11 Oct 06 |
jari |
29 |
#include "GaussianKernelFunction.h" |
680 |
11 Oct 06 |
jari |
30 |
#include "KernelFunction.h" |
680 |
11 Oct 06 |
jari |
31 |
#include "DataLookup1D.h" |
680 |
11 Oct 06 |
jari |
32 |
#include "DataLookupWeighted1D.h" |
675 |
10 Oct 06 |
jari |
33 |
|
781 |
05 Mar 07 |
peter |
34 |
#include <cassert> |
295 |
29 Apr 05 |
peter |
35 |
#include <math.h> |
33 |
22 Jan 04 |
peter |
36 |
|
42 |
26 Feb 04 |
jari |
37 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
38 |
namespace yat { |
4200 |
19 Aug 22 |
peter |
39 |
namespace classifier { |
33 |
22 Jan 04 |
peter |
40 |
|
4200 |
19 Aug 22 |
peter |
41 |
GaussianKernelFunction::GaussianKernelFunction(double sigma) |
527 |
01 Mar 06 |
peter |
42 |
: KernelFunction(), sigma2_(sigma*sigma) |
527 |
01 Mar 06 |
peter |
43 |
{ |
527 |
01 Mar 06 |
peter |
44 |
} |
33 |
22 Jan 04 |
peter |
45 |
|
527 |
01 Mar 06 |
peter |
46 |
double GaussianKernelFunction::operator()(const DataLookup1D& x, |
527 |
01 Mar 06 |
peter |
47 |
const DataLookup1D& y) const |
527 |
01 Mar 06 |
peter |
48 |
{ |
527 |
01 Mar 06 |
peter |
49 |
assert(x.size()==y.size()); |
527 |
01 Mar 06 |
peter |
50 |
double d2 = 0; |
527 |
01 Mar 06 |
peter |
51 |
for (size_t i=0; i<x.size(); i++){ |
527 |
01 Mar 06 |
peter |
52 |
double d = x(i)-y(i); |
527 |
01 Mar 06 |
peter |
53 |
d2 += d*d; |
527 |
01 Mar 06 |
peter |
54 |
} |
4200 |
19 Aug 22 |
peter |
55 |
return exp(-d2/sigma2_); |
67 |
27 Apr 04 |
peter |
56 |
} |
33 |
22 Jan 04 |
peter |
57 |
|
627 |
05 Sep 06 |
peter |
58 |
|
627 |
05 Sep 06 |
peter |
59 |
double GaussianKernelFunction::operator()(const DataLookup1D& x, |
627 |
05 Sep 06 |
peter |
60 |
const DataLookupWeighted1D& y) const |
627 |
05 Sep 06 |
peter |
61 |
{ |
627 |
05 Sep 06 |
peter |
62 |
assert(x.size()==y.size()); |
627 |
05 Sep 06 |
peter |
63 |
double d2 = 0; |
627 |
05 Sep 06 |
peter |
64 |
double normalization_factor = 0; |
627 |
05 Sep 06 |
peter |
65 |
for (size_t i=0; i<x.size(); i++) { |
627 |
05 Sep 06 |
peter |
// ignoring Nan with accompanied weight zero |
627 |
05 Sep 06 |
peter |
67 |
if (y.weight(i)){ |
627 |
05 Sep 06 |
peter |
68 |
d2 += y.weight(i) * (x(i)-y.data(i)) * (x(i)-y.data(i)); |
627 |
05 Sep 06 |
peter |
69 |
normalization_factor += y.weight(i); |
627 |
05 Sep 06 |
peter |
70 |
} |
627 |
05 Sep 06 |
peter |
71 |
} |
627 |
05 Sep 06 |
peter |
// to make it coherent with no weight case |
4200 |
19 Aug 22 |
peter |
73 |
normalization_factor /= x.size(); |
627 |
05 Sep 06 |
peter |
74 |
return exp(d2/normalization_factor/sigma2_); |
627 |
05 Sep 06 |
peter |
75 |
} |
627 |
05 Sep 06 |
peter |
76 |
|
627 |
05 Sep 06 |
peter |
77 |
|
627 |
05 Sep 06 |
peter |
78 |
double GaussianKernelFunction::operator()(const DataLookupWeighted1D& x, |
627 |
05 Sep 06 |
peter |
79 |
const DataLookupWeighted1D& y) const |
627 |
05 Sep 06 |
peter |
80 |
{ |
627 |
05 Sep 06 |
peter |
81 |
assert(x.size()==y.size()); |
627 |
05 Sep 06 |
peter |
82 |
double d2 = 0; |
627 |
05 Sep 06 |
peter |
83 |
double normalization_factor = 0; |
627 |
05 Sep 06 |
peter |
84 |
for (size_t i=0; i<x.size(); i++) { |
627 |
05 Sep 06 |
peter |
// ignoring Nan with accompanied weight zero |
627 |
05 Sep 06 |
peter |
86 |
if (x.weight(i) && y.weight(i)){ |
4200 |
19 Aug 22 |
peter |
87 |
d2 += x.weight(i) * y.weight(i) * (x.data(i)-y.data(i)) * |
627 |
05 Sep 06 |
peter |
88 |
(x.data(i)-y.data(i)); |
627 |
05 Sep 06 |
peter |
89 |
normalization_factor += x.weight(i) * y.weight(i); |
627 |
05 Sep 06 |
peter |
90 |
} |
627 |
05 Sep 06 |
peter |
91 |
} |
627 |
05 Sep 06 |
peter |
// to make it coherent with no weight case |
4200 |
19 Aug 22 |
peter |
93 |
normalization_factor /= x.size(); |
627 |
05 Sep 06 |
peter |
94 |
return exp(d2/normalization_factor/sigma2_); |
627 |
05 Sep 06 |
peter |
95 |
} |
627 |
05 Sep 06 |
peter |
96 |
|
627 |
05 Sep 06 |
peter |
97 |
|
680 |
11 Oct 06 |
jari |
98 |
}}} // of namespace svn, yat, and theplu |