1 | // $Id: Pearson.cc 703 2006-12-18 00:47:44Z jari $ |
---|
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 "Pearson.h" |
---|
25 | #include "AveragerPair.h" |
---|
26 | #include "AveragerPairWeighted.h" |
---|
27 | #include "yat/utility/vector.h" |
---|
28 | #include "yat/classifier/DataLookupWeighted1D.h" |
---|
29 | #include "yat/classifier/Target.h" |
---|
30 | |
---|
31 | #include <cmath> |
---|
32 | #include <gsl/gsl_cdf.h> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace statistics { |
---|
37 | |
---|
38 | Pearson::Pearson(bool b) |
---|
39 | : Score(b), r_(0), nof_samples_(0) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | Pearson::~Pearson(void) |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | double Pearson::p_value() const |
---|
48 | { |
---|
49 | if(weighted_) |
---|
50 | return 1; |
---|
51 | if(nof_samples_<2){ |
---|
52 | std::cerr << "Warning: Only " << nof_samples_ << "samples. " |
---|
53 | << "Need at lest 3.\n"; |
---|
54 | return 1; |
---|
55 | } |
---|
56 | |
---|
57 | double t = sqrt(nof_samples_ - 2)*fabs(r_) /sqrt(1-r_*r_); |
---|
58 | double p = gsl_cdf_tdist_Q(t, nof_samples_ -2 ); |
---|
59 | if (absolute_) |
---|
60 | return 2*p; |
---|
61 | if (r_<0) |
---|
62 | return 1-p; |
---|
63 | return p; |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | double Pearson::score(const classifier::Target& target, |
---|
68 | const utility::vector& value) |
---|
69 | { |
---|
70 | weighted_=false; |
---|
71 | AveragerPair ap; |
---|
72 | for (size_t i=0; i<target.size(); i++){ |
---|
73 | if (target.binary(i)) |
---|
74 | ap.add(1, value(i)); |
---|
75 | else |
---|
76 | ap.add(-1, value(i)); |
---|
77 | nof_samples_ = target.size(); |
---|
78 | } |
---|
79 | r_ = ap.correlation(); |
---|
80 | if (r_<0 && absolute_) |
---|
81 | return -r_; |
---|
82 | |
---|
83 | return r_; |
---|
84 | } |
---|
85 | |
---|
86 | double Pearson::score(const classifier::Target& target, |
---|
87 | const classifier::DataLookupWeighted1D& value) |
---|
88 | { |
---|
89 | weighted_=true; |
---|
90 | AveragerPairWeighted ap; |
---|
91 | for (size_t i=0; i<target.size(); i++){ |
---|
92 | if (target.binary(i)) |
---|
93 | ap.add(1, value.data(i),1,value.weight(i)); |
---|
94 | else |
---|
95 | ap.add(-1, value.data(i),1,value.weight(i)); |
---|
96 | nof_samples_ = target.size(); |
---|
97 | } |
---|
98 | r_ = ap.correlation(); |
---|
99 | if (r_<0 && absolute_) |
---|
100 | return -r_; |
---|
101 | |
---|
102 | return r_; |
---|
103 | } |
---|
104 | |
---|
105 | double Pearson::score(const classifier::Target& target, |
---|
106 | const utility::vector& value, |
---|
107 | const utility::vector& weight) |
---|
108 | { |
---|
109 | weighted_=true; |
---|
110 | AveragerPairWeighted ap; |
---|
111 | for (size_t i=0; i<target.size(); i++){ |
---|
112 | if (target.binary(i)) |
---|
113 | ap.add(1, value(i),1,weight(i)); |
---|
114 | else |
---|
115 | ap.add(-1, value(i),1,weight(i)); |
---|
116 | nof_samples_ = target.size(); |
---|
117 | } |
---|
118 | r_ = ap.correlation(); |
---|
119 | if (r_<0 && absolute_) |
---|
120 | return -r_; |
---|
121 | |
---|
122 | return r_; |
---|
123 | } |
---|
124 | |
---|
125 | }}} // of namespace statistics, yat, and theplu |
---|