source: trunk/c++_tools/statistics/SNR.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 Id
File size: 2.2 KB
Line 
1// $Id: SNR.cc 623 2006-09-05 02:13:12Z peter $
2
3#include "c++_tools/statistics/SNR.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
9namespace theplu {
10namespace statistics { 
11
12  SNR::SNR(bool b) 
13    : Score(b), score_(0)
14  {
15  }
16
17  double SNR::score(const classifier::Target& target, 
18                    const utility::vector& value)
19  {
20    weighted_=false;
21    statistics::Averager positive;
22    statistics::Averager negative;
23    for(size_t i=0; i<target.size(); i++){
24      if (target.binary(i))
25        positive.add(value(i));
26      else
27        negative.add(value(i));
28    }
29    double diff = positive.mean() - negative.mean();
30    double denom=positive.std()+negative.std();
31    score_=diff/denom;
32    if(positive.n()==0 || negative.n()==0) 
33      score_=0;
34    if (score_<0 && absolute_)
35      score_=-score_;   
36    return score_;
37  }
38
39  double SNR::score(const classifier::Target& target, 
40                    const classifier::DataLookupWeighted1D& value)
41  {
42    weighted_=true;
43    statistics::AveragerWeighted positive;
44    statistics::AveragerWeighted negative;
45    for(size_t i=0; i<target.size(); i++){
46      if (target.binary(i))
47        positive.add(value.data(i),value.weight(i));
48      else
49        negative.add(value.data(i),value.weight(i));
50    }
51    double diff = positive.mean() - negative.mean();
52    double denom=positive.std()+negative.std();
53    assert(denom);
54    score_=diff/denom;
55    if(positive.sum_w()==0 || negative.sum_w()==0) 
56      score_=0;
57    if (score_<0 && absolute_)
58      score_=-score_;   
59    return score_;
60  }
61
62
63
64  double SNR::score(const classifier::Target& target, 
65                    const utility::vector& value,
66                    const utility::vector& weight)
67  {
68    weighted_=true;
69    statistics::AveragerWeighted positive;
70    statistics::AveragerWeighted negative;
71    for(size_t i=0; i<target.size(); i++){
72      if (target.binary(i))
73        positive.add(value(i),weight(i));
74      else
75        negative.add(value(i),weight(i));
76    }
77    double diff = positive.mean() - negative.mean();
78    double denom=positive.std()+negative.std();
79    assert(denom);
80    score_=diff/denom;
81    if(positive.sum_w()==0 || negative.sum_w()==0) 
82      score_=0;
83    if (score_<0 && absolute_)
84      score_=-score_;   
85    return score_;
86  }
87
88
89
90}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.