source: trunk/c++_tools/statistics/SNR.cc @ 675

Last change on this file since 675 was 675, checked in by Jari Häkkinen, 15 years ago

References #83. Changing project name to yat. Compilation will fail in this revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// $Id: SNR.cc 675 2006-10-10 12:08:45Z jari $
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 "c++_tools/statistics/SNR.h"
25#include "c++_tools/statistics/Averager.h"
26#include "c++_tools/statistics/AveragerWeighted.h"
27#include "c++_tools/classifier/DataLookupWeighted1D.h"
28#include "c++_tools/classifier/Target.h"
29
30namespace theplu {
31namespace statistics { 
32
33  SNR::SNR(bool b) 
34    : Score(b), score_(0)
35  {
36  }
37
38  double SNR::score(const classifier::Target& target, 
39                    const utility::vector& value)
40  {
41    weighted_=false;
42    statistics::Averager positive;
43    statistics::Averager negative;
44    for(size_t i=0; i<target.size(); i++){
45      if (target.binary(i))
46        positive.add(value(i));
47      else
48        negative.add(value(i));
49    }
50    double diff = positive.mean() - negative.mean();
51    double denom=positive.std()+negative.std();
52    score_=diff/denom;
53    if(positive.n()==0 || negative.n()==0) 
54      score_=0;
55    if (score_<0 && absolute_)
56      score_=-score_;   
57    return score_;
58  }
59
60  double SNR::score(const classifier::Target& target, 
61                    const classifier::DataLookupWeighted1D& value)
62  {
63    weighted_=true;
64    statistics::AveragerWeighted positive;
65    statistics::AveragerWeighted negative;
66    for(size_t i=0; i<target.size(); i++){
67      if (target.binary(i))
68        positive.add(value.data(i),value.weight(i));
69      else
70        negative.add(value.data(i),value.weight(i));
71    }
72    double diff = positive.mean() - negative.mean();
73    double denom=positive.std()+negative.std();
74    assert(denom);
75    score_=diff/denom;
76    if(positive.sum_w()==0 || negative.sum_w()==0) 
77      score_=0;
78    if (score_<0 && absolute_)
79      score_=-score_;   
80    return score_;
81  }
82
83
84
85  double SNR::score(const classifier::Target& target, 
86                    const utility::vector& value,
87                    const utility::vector& weight)
88  {
89    weighted_=true;
90    statistics::AveragerWeighted positive;
91    statistics::AveragerWeighted negative;
92    for(size_t i=0; i<target.size(); i++){
93      if (target.binary(i))
94        positive.add(value(i),weight(i));
95      else
96        negative.add(value(i),weight(i));
97    }
98    double diff = positive.mean() - negative.mean();
99    double denom=positive.std()+negative.std();
100    assert(denom);
101    score_=diff/denom;
102    if(positive.sum_w()==0 || negative.sum_w()==0) 
103      score_=0;
104    if (score_<0 && absolute_)
105      score_=-score_;   
106    return score_;
107  }
108
109
110
111}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.