source: trunk/yat/statistics/SNRScore.cc

Last change on this file was 2992, checked in by Peter, 9 years ago

set svndigest:ignore proprty to reflect when files were initially copied from other file (and early history should be ignored). closes #750

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svndigest:ignore set to 855
File size: 3.1 KB
Line 
1// $Id: SNRScore.cc 2992 2013-03-03 05:03:44Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
5  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2012 Peter Johansson
7
8  This file is part of the yat library, http://dev.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 3 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with yat. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include <config.h>
25
26#include "SNRScore.h"
27#include "Averager.h"
28#include "AveragerWeighted.h"
29#include "yat/classifier/DataLookupWeighted1D.h"
30#include "yat/classifier/Target.h"
31#include "yat/utility/VectorBase.h"
32
33#include <cassert>
34
35namespace theplu {
36namespace yat {
37namespace statistics { 
38
39  SNRScore::SNRScore(bool b) 
40    : Score(b)
41  {
42  }
43
44  SNRScore::~SNRScore(void)
45  {
46  }
47
48  double SNRScore::score(const classifier::Target& target, 
49                         const utility::VectorBase& value) const
50  {
51    statistics::Averager positive;
52    statistics::Averager negative;
53    for(size_t i=0; i<target.size(); i++){
54      if (target.binary(i))
55        positive.add(value(i));
56      else
57        negative.add(value(i));
58    }
59    double diff = positive.mean() - negative.mean();
60    double denom=positive.std()+negative.std();
61    double snr=diff/denom;
62    if(positive.n()==0 || negative.n()==0) 
63      snr=0;
64    if (snr<0 && absolute_)
65      snr=-snr;   
66    return snr;
67  }
68
69  double SNRScore::score(const classifier::Target& target, 
70                         const classifier::DataLookupWeighted1D& value) const
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    double denom=positive.std()+negative.std();
82    assert(denom);
83    double snr=diff/denom;
84    if(positive.sum_w()==0 || negative.sum_w()==0) 
85      snr=0;
86    if (snr<0 && absolute_)
87      snr=-snr;   
88    return snr;
89  }
90
91
92
93  double SNRScore::score(const classifier::Target& target, 
94                    const utility::VectorBase& value,
95                    const utility::VectorBase& weight) const
96  {
97    statistics::AveragerWeighted positive;
98    statistics::AveragerWeighted negative;
99    for(size_t i=0; i<target.size(); i++){
100      if (target.binary(i))
101        positive.add(value(i),weight(i));
102      else
103        negative.add(value(i),weight(i));
104    }
105    double diff = positive.mean() - negative.mean();
106    double denom=positive.std()+negative.std();
107    assert(denom);
108    double snr=diff/denom;
109    if(positive.sum_w()==0 || negative.sum_w()==0) 
110      snr=0;
111    if (snr<0 && absolute_)
112      snr=-snr;   
113    return snr;
114  }
115
116}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.