source: trunk/lib/statistics/tScore.cc @ 414

Last change on this file since 414 was 414, checked in by Peter, 16 years ago

adding FoldChange? class and fixing bugs in Score classes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1// $Id: tScore.cc 414 2005-12-01 15:17:49Z peter $
2
3// System includes
4#include <cmath>
5
6// Thep C++ Tools
7#include <c++_tools/statistics/tScore.h>
8#include <c++_tools/statistics/Averager.h>
9#include <c++_tools/gslapi/vector.h>
10#include <c++_tools/statistics/AveragerWeighted.h>
11
12namespace theplu {
13namespace statistics { 
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()+negative.n()-2));
44    t_=diff/s/(1.0/positive.n()+1.0/negative.n());
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::AveragerWeighted positive;
65    statistics::AveragerWeighted 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.sum_xsqr_centered()+negative.sum_xsqr_centered())/
74                  (positive.sum_w()+negative.sum_w()-2));
75    t_=diff/s/(1.0/positive.sum_w()+1.0/negative.sum_w());
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 statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.