source: trunk/yat/statistics/tScore.cc @ 747

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

replaced includes in header files with forward declarations when possible. Added some includes in cc files.

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