1 | // $Id: Pearson.cc 1486 2008-09-09 21:17:19Z 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 this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "Pearson.h" |
---|
27 | #include "AveragerPair.h" |
---|
28 | #include "AveragerPairWeighted.h" |
---|
29 | #include "yat/utility/VectorBase.h" |
---|
30 | #include "yat/classifier/DataLookupWeighted1D.h" |
---|
31 | #include "yat/classifier/Target.h" |
---|
32 | |
---|
33 | #include <cmath> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace statistics { |
---|
38 | |
---|
39 | Pearson::Pearson(bool b) |
---|
40 | : Score(b) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | Pearson::~Pearson(void) |
---|
45 | { |
---|
46 | } |
---|
47 | |
---|
48 | double Pearson::score(const classifier::Target& target, |
---|
49 | const utility::VectorBase& value) const |
---|
50 | { |
---|
51 | AveragerPair ap; |
---|
52 | for (size_t i=0; i<target.size(); i++){ |
---|
53 | if (target.binary(i)) |
---|
54 | ap.add(1, value(i)); |
---|
55 | else |
---|
56 | ap.add(-1, value(i)); |
---|
57 | } |
---|
58 | double r = ap.correlation(); |
---|
59 | if (r<0 && absolute_) |
---|
60 | return -r; |
---|
61 | return r; |
---|
62 | } |
---|
63 | |
---|
64 | double Pearson::score(const classifier::Target& target, |
---|
65 | const classifier::DataLookupWeighted1D& value) const |
---|
66 | { |
---|
67 | AveragerPairWeighted ap; |
---|
68 | for (size_t i=0; i<target.size(); i++){ |
---|
69 | if (target.binary(i)) |
---|
70 | ap.add(1, value.data(i),1,value.weight(i)); |
---|
71 | else |
---|
72 | ap.add(-1, value.data(i),1,value.weight(i)); |
---|
73 | } |
---|
74 | double r = ap.correlation(); |
---|
75 | if (r<0 && absolute_) |
---|
76 | return -r; |
---|
77 | return r; |
---|
78 | } |
---|
79 | |
---|
80 | double Pearson::score(const classifier::Target& target, |
---|
81 | const utility::VectorBase& value, |
---|
82 | const utility::VectorBase& weight) const |
---|
83 | { |
---|
84 | AveragerPairWeighted ap; |
---|
85 | for (size_t i=0; i<target.size(); i++){ |
---|
86 | if (target.binary(i)) |
---|
87 | ap.add(1, value(i),1,weight(i)); |
---|
88 | else |
---|
89 | ap.add(-1, value(i),1,weight(i)); |
---|
90 | } |
---|
91 | double r = ap.correlation(); |
---|
92 | if (r<0 && absolute_) |
---|
93 | return -r; |
---|
94 | return r; |
---|
95 | } |
---|
96 | |
---|
97 | }}} // of namespace statistics, yat, and theplu |
---|