1 | // $Id: tScore.cc 295 2005-04-29 09:15:58Z peter $ |
---|
2 | |
---|
3 | // System includes |
---|
4 | #include <cmath> |
---|
5 | |
---|
6 | // Thep C++ Tools |
---|
7 | #include "tScore.h" |
---|
8 | #include "Averager.h" |
---|
9 | #include "vector.h" |
---|
10 | #include "WeightedAverager.h" |
---|
11 | |
---|
12 | namespace theplu { |
---|
13 | namespace cpptools { |
---|
14 | |
---|
15 | tScore::tScore(bool b) |
---|
16 | : Score(b), t_(0), train_set_(), weight_() |
---|
17 | { |
---|
18 | } |
---|
19 | |
---|
20 | double tScore::score(const gslapi::vector& target, |
---|
21 | const gslapi::vector& data, |
---|
22 | const std::vector<size_t>& train_set) |
---|
23 | { |
---|
24 | weighted_=false; |
---|
25 | if (!train_set_.size()) |
---|
26 | for (size_t i=0; i<target_.size(); i++) |
---|
27 | train_set_.push_back(i); |
---|
28 | else |
---|
29 | train_set_=train_set; |
---|
30 | target_ = target; |
---|
31 | data_ = data; |
---|
32 | weight_ = gslapi::vector(target.size(),1); |
---|
33 | statistics::Averager positive; |
---|
34 | statistics::Averager negative; |
---|
35 | for(size_t i=0; i<train_set_.size(); i++){ |
---|
36 | if (target_[train_set_[i]]==1) |
---|
37 | positive.add(data_[train_set_[i]]); |
---|
38 | else |
---|
39 | negative.add(data_[train_set_[i]]); |
---|
40 | } |
---|
41 | double diff = positive.mean() - negative.mean(); |
---|
42 | double s=sqrt((positive.sum_xsqr()+negative.sum_xsqr()) |
---|
43 | /(positive.n()-1+negative.n()-1)); |
---|
44 | t_=diff/s; |
---|
45 | if (t_<0 && absolute_) |
---|
46 | t_=-t_; |
---|
47 | |
---|
48 | return t_; |
---|
49 | } |
---|
50 | |
---|
51 | double tScore::score(const gslapi::vector& target, |
---|
52 | const gslapi::vector& data, |
---|
53 | const gslapi::vector& weight, |
---|
54 | const std::vector<size_t>& train_set) |
---|
55 | { |
---|
56 | weighted_=true; |
---|
57 | if (!train_set_.size()) |
---|
58 | for (size_t i=0; i<target_.size(); i++) |
---|
59 | train_set_.push_back(i); |
---|
60 | else |
---|
61 | train_set_=train_set; |
---|
62 | target_ = target; |
---|
63 | weight_ = weight; |
---|
64 | statistics::WeightedAverager positive; |
---|
65 | statistics::WeightedAverager negative; |
---|
66 | for(size_t i=0; i<train_set_.size(); i++){ |
---|
67 | if (target_[train_set_[i]]==1) |
---|
68 | positive.add(data_(train_set_[i]),weight_(train_set_[i])); |
---|
69 | else |
---|
70 | negative.add(data_(train_set_[i]),weight_(train_set_[i])); |
---|
71 | } |
---|
72 | double diff = positive.mean() - negative.mean(); |
---|
73 | double s=sqrt((positive.squared_sum()+negative.squared_sum())/ |
---|
74 | (positive.sum_w()+negative.sum_w())); |
---|
75 | t_=diff/s; |
---|
76 | if (t_<0 && absolute_) |
---|
77 | t_=-t_; |
---|
78 | |
---|
79 | return t_; |
---|
80 | } |
---|
81 | |
---|
82 | double tScore::p_value(void) const |
---|
83 | { |
---|
84 | double dof = target_.size()-2; |
---|
85 | double p = gsl_cdf_tdist_Q(t_, dof); |
---|
86 | return (dof > 0 && !weighted_) ? p : 1; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | }} // of namespace cpptools and namespace theplu |
---|