1 | // $Id: tScore.cc 675 2006-10-10 12:08:45Z 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 "c++_tools/statistics/tScore.h" |
---|
25 | #include "c++_tools/statistics/Averager.h" |
---|
26 | #include "c++_tools/statistics/AveragerWeighted.h" |
---|
27 | #include "c++_tools/classifier/DataLookupWeighted1D.h" |
---|
28 | #include "c++_tools/classifier/Target.h" |
---|
29 | |
---|
30 | #include <cassert> |
---|
31 | #include <cmath> |
---|
32 | |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace statistics { |
---|
36 | |
---|
37 | tScore::tScore(bool b) |
---|
38 | : Score(b), t_(0) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | double tScore::score(const classifier::Target& target, |
---|
43 | const utility::vector& value) |
---|
44 | { |
---|
45 | weighted_=false; |
---|
46 | statistics::Averager positive; |
---|
47 | statistics::Averager negative; |
---|
48 | for(size_t i=0; i<target.size(); i++){ |
---|
49 | if (target.binary(i)) |
---|
50 | positive.add(value(i)); |
---|
51 | else |
---|
52 | negative.add(value(i)); |
---|
53 | } |
---|
54 | double diff = positive.mean() - negative.mean(); |
---|
55 | dof_=positive.n()+negative.n()-2; |
---|
56 | double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_; |
---|
57 | |
---|
58 | t_=diff/sqrt(s2/positive.n()+s2/negative.n()); |
---|
59 | if (t_<0 && absolute_) |
---|
60 | t_=-t_; |
---|
61 | |
---|
62 | return t_; |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | double tScore::score(const classifier::Target& target, |
---|
67 | const classifier::DataLookupWeighted1D& value) |
---|
68 | { |
---|
69 | weighted_=true; |
---|
70 | |
---|
71 | statistics::AveragerWeighted positive; |
---|
72 | statistics::AveragerWeighted negative; |
---|
73 | for(size_t i=0; i<target.size(); i++){ |
---|
74 | if (target.binary(i)) |
---|
75 | positive.add(value.data(i),value.weight(i)); |
---|
76 | else |
---|
77 | negative.add(value.data(i),value.weight(i)); |
---|
78 | } |
---|
79 | double diff = positive.mean() - negative.mean(); |
---|
80 | dof_=positive.n()+negative.n()-2; |
---|
81 | double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_; |
---|
82 | t_=diff/sqrt(s2/positive.n()+s2/(negative.n())); |
---|
83 | if (t_<0 && absolute_) |
---|
84 | t_=-t_; |
---|
85 | |
---|
86 | if(positive.sum_w()==0 || negative.sum_w()==0) |
---|
87 | t_=0; |
---|
88 | return t_; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | double tScore::score(const classifier::Target& target, |
---|
93 | const utility::vector& value, |
---|
94 | const utility::vector& weight) |
---|
95 | { |
---|
96 | weighted_=true; |
---|
97 | |
---|
98 | statistics::AveragerWeighted positive; |
---|
99 | statistics::AveragerWeighted negative; |
---|
100 | for(size_t i=0; i<target.size(); i++){ |
---|
101 | if (target.binary(i)) |
---|
102 | positive.add(value(i),weight(i)); |
---|
103 | else |
---|
104 | negative.add(value(i),weight(i)); |
---|
105 | } |
---|
106 | double diff = positive.mean() - negative.mean(); |
---|
107 | dof_=positive.n()+negative.n()-2; |
---|
108 | double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_; |
---|
109 | t_=diff/sqrt(s2/positive.n()+s2/(negative.n())); |
---|
110 | if (t_<0 && absolute_) |
---|
111 | t_=-t_; |
---|
112 | |
---|
113 | if(positive.sum_w()==0 || negative.sum_w()==0) |
---|
114 | t_=0; |
---|
115 | return t_; |
---|
116 | } |
---|
117 | |
---|
118 | double tScore::p_value(void) const |
---|
119 | { |
---|
120 | double p = gsl_cdf_tdist_Q(t_, dof_); |
---|
121 | return (dof_ > 0 && !weighted_) ? p : 1; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | }} // of namespace statistics and namespace theplu |
---|