1 | // $Id: Pearson.cc 1487 2008-09-10 08:41:36Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004, 2005 Peter Johansson |
---|
5 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
6 | Copyright (C) 2008 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 "Pearson.h" |
---|
25 | #include "AveragerPair.h" |
---|
26 | #include "AveragerPairWeighted.h" |
---|
27 | #include "yat/utility/VectorBase.h" |
---|
28 | #include "yat/classifier/DataLookupWeighted1D.h" |
---|
29 | #include "yat/classifier/Target.h" |
---|
30 | |
---|
31 | #include <cmath> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace statistics { |
---|
36 | |
---|
37 | Pearson::Pearson(bool b) |
---|
38 | : Score(b) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | Pearson::~Pearson(void) |
---|
43 | { |
---|
44 | } |
---|
45 | |
---|
46 | double Pearson::score(const classifier::Target& target, |
---|
47 | const utility::VectorBase& value) const |
---|
48 | { |
---|
49 | AveragerPair ap; |
---|
50 | for (size_t i=0; i<target.size(); i++){ |
---|
51 | if (target.binary(i)) |
---|
52 | ap.add(1, value(i)); |
---|
53 | else |
---|
54 | ap.add(-1, value(i)); |
---|
55 | } |
---|
56 | double r = ap.correlation(); |
---|
57 | if (r<0 && absolute_) |
---|
58 | return -r; |
---|
59 | return r; |
---|
60 | } |
---|
61 | |
---|
62 | double Pearson::score(const classifier::Target& target, |
---|
63 | const classifier::DataLookupWeighted1D& value) const |
---|
64 | { |
---|
65 | AveragerPairWeighted ap; |
---|
66 | for (size_t i=0; i<target.size(); i++){ |
---|
67 | if (target.binary(i)) |
---|
68 | ap.add(1, value.data(i),1,value.weight(i)); |
---|
69 | else |
---|
70 | ap.add(-1, value.data(i),1,value.weight(i)); |
---|
71 | } |
---|
72 | double r = ap.correlation(); |
---|
73 | if (r<0 && absolute_) |
---|
74 | return -r; |
---|
75 | return r; |
---|
76 | } |
---|
77 | |
---|
78 | double Pearson::score(const classifier::Target& target, |
---|
79 | const utility::VectorBase& value, |
---|
80 | const utility::VectorBase& weight) const |
---|
81 | { |
---|
82 | AveragerPairWeighted ap; |
---|
83 | for (size_t i=0; i<target.size(); i++){ |
---|
84 | if (target.binary(i)) |
---|
85 | ap.add(1, value(i),1,weight(i)); |
---|
86 | else |
---|
87 | ap.add(-1, value(i),1,weight(i)); |
---|
88 | } |
---|
89 | double r = ap.correlation(); |
---|
90 | if (r<0 && absolute_) |
---|
91 | return -r; |
---|
92 | return r; |
---|
93 | } |
---|
94 | |
---|
95 | }}} // of namespace statistics, yat, and theplu |
---|