source: trunk/lib/statistics/SNR.cc @ 530

Last change on this file since 530 was 530, checked in by Markus Ringnér, 16 years ago

Fixed problems for empty target groups

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.5 KB
Line 
1// $Id: SNR.cc 530 2006-03-01 14:11:09Z markus $
2
3
4// Thep C++ Tools
5#include <c++_tools/statistics/SNR.h>
6#include <c++_tools/statistics/Averager.h>
7#include <c++_tools/statistics/AveragerWeighted.h>
8#include <c++_tools/classifier/Target.h>
9
10namespace theplu {
11namespace statistics { 
12
13  SNR::SNR(bool b) 
14    : Score(b),  score_(0)
15  {
16  }
17
18  double SNR::score(const classifier::Target& target, 
19                       const gslapi::vector& value)
20  {
21    weighted_=false;
22    statistics::Averager positive;
23    statistics::Averager negative;
24    for(size_t i=0; i<target.size(); i++){
25      if (target.binary(i))
26        positive.add(value(i));
27      else
28        negative.add(value(i));
29    }
30    double diff = positive.mean() - negative.mean();
31    double denom=positive.std()+negative.std();
32    score_=diff/denom;
33    if(positive.n()==0 || negative.n()==0) 
34      score_=0;
35    if (score_<0 && absolute_)
36      score_=-score_;   
37    return score_;
38  }
39
40  double SNR::score(const classifier::Target& target, 
41                       const gslapi::vector& value,
42                       const gslapi::vector& weight)
43  {
44    weighted_=true;
45    statistics::AveragerWeighted positive;
46    statistics::AveragerWeighted negative;
47    for(size_t i=0; i<target.size(); i++){
48      if (target.binary(i))
49        positive.add(value(i),weight(i));
50      else
51        negative.add(value(i),weight(i));
52    }
53    double diff = positive.mean() - negative.mean();
54    double denom=positive.std()+negative.std();
55    score_=diff/denom;
56    if(positive.sum_w()==0 || negative.sum_w()==0) 
57      score_=0;
58    if (score_<0 && absolute_)
59      score_=-score_;   
60    return score_;
61  }
62
63
64
65}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.