source: trunk/c++_tools/statistics/tScore.cc @ 623

Last change on this file since 623 was 623, checked in by Peter, 15 years ago

fixes #112 and refs #123 added overloaded function score taking Target and DataLookupWeighted1D, which is needed for InputRanker?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1// $Id: tScore.cc 623 2006-09-05 02:13:12Z peter $
2
3#include "c++_tools/statistics/tScore.h"
4#include "c++_tools/statistics/Averager.h"
5#include "c++_tools/statistics/AveragerWeighted.h"
6#include "c++_tools/classifier/DataLookupWeighted1D.h"
7#include "c++_tools/classifier/Target.h"
8
9#include <cassert>
10#include <cmath>
11
12
13namespace theplu {
14namespace statistics { 
15
16  tScore::tScore(bool b) 
17    : Score(b),  t_(0)
18  {
19  }
20
21  double tScore::score(const classifier::Target& target, 
22                       const utility::vector& value)
23  {
24    weighted_=false;
25    statistics::Averager positive;
26    statistics::Averager negative;
27    for(size_t i=0; i<target.size(); i++){
28      if (target.binary(i))
29        positive.add(value(i));
30      else
31        negative.add(value(i));
32    }
33    double diff = positive.mean() - negative.mean();
34    dof_=positive.n()+negative.n()-2;
35    double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_;
36
37    t_=diff/sqrt(s2/positive.n()+s2/negative.n());
38    if (t_<0 && absolute_)
39      t_=-t_;
40     
41    return t_;
42  }
43
44
45  double tScore::score(const classifier::Target& target, 
46                       const classifier::DataLookupWeighted1D& value)
47  {
48    weighted_=true;
49
50    statistics::AveragerWeighted positive;
51    statistics::AveragerWeighted negative;
52    for(size_t i=0; i<target.size(); i++){
53      if (target.binary(i))
54        positive.add(value.data(i),value.weight(i));
55      else
56        negative.add(value.data(i),value.weight(i));
57    }
58    double diff = positive.mean() - negative.mean();
59    dof_=positive.n()+negative.n()-2;
60    double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_;
61    t_=diff/sqrt(s2/positive.n()+s2/(negative.n()));
62    if (t_<0 && absolute_)
63      t_=-t_;
64
65    if(positive.sum_w()==0 || negative.sum_w()==0)
66      t_=0;
67    return t_;
68  }
69
70
71  double tScore::score(const classifier::Target& target, 
72                       const utility::vector& value,
73                       const utility::vector& weight)
74  {
75    weighted_=true;
76
77    statistics::AveragerWeighted positive;
78    statistics::AveragerWeighted negative;
79    for(size_t i=0; i<target.size(); i++){
80      if (target.binary(i))
81        positive.add(value(i),weight(i));
82      else
83        negative.add(value(i),weight(i));
84    }
85    double diff = positive.mean() - negative.mean();
86    dof_=positive.n()+negative.n()-2;
87    double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_;
88    t_=diff/sqrt(s2/positive.n()+s2/(negative.n()));
89    if (t_<0 && absolute_)
90      t_=-t_;
91
92    if(positive.sum_w()==0 || negative.sum_w()==0)
93      t_=0;
94    return t_;
95  }
96
97  double tScore::p_value(void) const
98  {
99    double p = gsl_cdf_tdist_Q(t_, dof_);
100    return (dof_ > 0 && !weighted_) ? p : 1;
101  }
102
103
104
105}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.