source: trunk/yat/statistics/SNR.cc @ 831

Last change on this file since 831 was 831, checked in by Peter, 14 years ago

Refs #185.

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