1 | // $Id: SNRScore.cc 1487 2008-09-10 08:41:36Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/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 3 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "SNRScore.h" |
---|
24 | #include "Averager.h" |
---|
25 | #include "AveragerWeighted.h" |
---|
26 | #include "yat/classifier/DataLookupWeighted1D.h" |
---|
27 | #include "yat/classifier/Target.h" |
---|
28 | #include "yat/utility/VectorBase.h" |
---|
29 | |
---|
30 | #include <cassert> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace statistics { |
---|
35 | |
---|
36 | SNRScore::SNRScore(bool b) |
---|
37 | : Score(b) |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | SNRScore::~SNRScore(void) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | double SNRScore::score(const classifier::Target& target, |
---|
46 | const utility::VectorBase& value) const |
---|
47 | { |
---|
48 | statistics::Averager positive; |
---|
49 | statistics::Averager negative; |
---|
50 | for(size_t i=0; i<target.size(); i++){ |
---|
51 | if (target.binary(i)) |
---|
52 | positive.add(value(i)); |
---|
53 | else |
---|
54 | negative.add(value(i)); |
---|
55 | } |
---|
56 | double diff = positive.mean() - negative.mean(); |
---|
57 | double denom=positive.std()+negative.std(); |
---|
58 | double snr=diff/denom; |
---|
59 | if(positive.n()==0 || negative.n()==0) |
---|
60 | snr=0; |
---|
61 | if (snr<0 && absolute_) |
---|
62 | snr=-snr; |
---|
63 | return snr; |
---|
64 | } |
---|
65 | |
---|
66 | double SNRScore::score(const classifier::Target& target, |
---|
67 | const classifier::DataLookupWeighted1D& value) const |
---|
68 | { |
---|
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.data(i),value.weight(i)); |
---|
74 | else |
---|
75 | negative.add(value.data(i),value.weight(i)); |
---|
76 | } |
---|
77 | double diff = positive.mean() - negative.mean(); |
---|
78 | double denom=positive.std()+negative.std(); |
---|
79 | assert(denom); |
---|
80 | double snr=diff/denom; |
---|
81 | if(positive.sum_w()==0 || negative.sum_w()==0) |
---|
82 | snr=0; |
---|
83 | if (snr<0 && absolute_) |
---|
84 | snr=-snr; |
---|
85 | return snr; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | double SNRScore::score(const classifier::Target& target, |
---|
91 | const utility::VectorBase& value, |
---|
92 | const utility::VectorBase& weight) const |
---|
93 | { |
---|
94 | statistics::AveragerWeighted positive; |
---|
95 | statistics::AveragerWeighted negative; |
---|
96 | for(size_t i=0; i<target.size(); i++){ |
---|
97 | if (target.binary(i)) |
---|
98 | positive.add(value(i),weight(i)); |
---|
99 | else |
---|
100 | negative.add(value(i),weight(i)); |
---|
101 | } |
---|
102 | double diff = positive.mean() - negative.mean(); |
---|
103 | double denom=positive.std()+negative.std(); |
---|
104 | assert(denom); |
---|
105 | double snr=diff/denom; |
---|
106 | if(positive.sum_w()==0 || negative.sum_w()==0) |
---|
107 | snr=0; |
---|
108 | if (snr<0 && absolute_) |
---|
109 | snr=-snr; |
---|
110 | return snr; |
---|
111 | } |
---|
112 | |
---|
113 | }}} // of namespace statistics, yat, and theplu |
---|