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

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

modified implementation of Parson weighted - now in line with other weighted statistics.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1// $Id: tScore.cc 532 2006-03-02 17:44:48Z peter $
2
3// System includes
4#include <cassert>
5#include <cmath>
6
7// Thep C++ Tools
8#include <c++_tools/statistics/tScore.h>
9#include <c++_tools/statistics/Averager.h>
10#include <c++_tools/statistics/AveragerWeighted.h>
11#include <c++_tools/classifier/Target.h>
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 gslapi::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*(1.0/positive.n()+1.0/negative.n()));
38    if (t_<0 && absolute_)
39      t_=-t_;
40     
41    return t_;
42  }
43
44  double tScore::score(const classifier::Target& target, 
45                       const gslapi::vector& value,
46                       const gslapi::vector& weight)
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(i),weight(i));
55      else
56        negative.add(value(i),weight(i));
57    }
58    double diff = positive.mean() - negative.mean();
59    dof_=positive.sum_w()*positive.sum_w()/positive.sum_ww() + 
60      negative.sum_w()*negative.sum_w()/negative.sum_ww();
61    double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_;
62    t_=diff/sqrt(s2*(1.0/(positive.sum_w()*positive.sum_w()/positive.sum_ww())+
63                     1.0/(negative.sum_w()*negative.sum_w()/negative.sum_ww())));
64    if (t_<0 && absolute_)
65      t_=-t_;
66
67    if(positive.sum_w()==0 || negative.sum_w()==0)
68      t_=0;
69    return t_;
70  }
71
72  double tScore::p_value(void) const
73  {
74    double p = gsl_cdf_tdist_Q(t_, dof_);
75    return (dof_ > 0 && !weighted_) ? p : 1;
76  }
77
78
79
80}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.