yat/statistics/SNRScore.cc

Code
Comments
Other
Rev Date Author Line
529 01 Mar 06 markus 1 // $Id$
529 01 Mar 06 markus 2
675 10 Oct 06 jari 3 /*
4359 23 Aug 23 peter 4   Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
4359 23 Aug 23 peter 5   Copyright (C) 2007 Peter Johansson, Markus Ringnér
2119 12 Dec 09 peter 6   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 7   Copyright (C) 2012 Peter Johansson
675 10 Oct 06 jari 8
1437 25 Aug 08 peter 9   This file is part of the yat library, http://dev.thep.lu.se/yat
675 10 Oct 06 jari 10
675 10 Oct 06 jari 11   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 12   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 13   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 14   License, or (at your option) any later version.
675 10 Oct 06 jari 15
675 10 Oct 06 jari 16   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 19   General Public License for more details.
675 10 Oct 06 jari 20
675 10 Oct 06 jari 21   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 22   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 23 */
675 10 Oct 06 jari 24
2881 18 Nov 12 peter 25 #include <config.h>
2881 18 Nov 12 peter 26
856 05 Sep 07 markus 27 #include "SNRScore.h"
680 11 Oct 06 jari 28 #include "Averager.h"
680 11 Oct 06 jari 29 #include "AveragerWeighted.h"
676 10 Oct 06 jari 30 #include "yat/classifier/DataLookupWeighted1D.h"
676 10 Oct 06 jari 31 #include "yat/classifier/Target.h"
1023 01 Feb 08 peter 32 #include "yat/utility/VectorBase.h"
529 01 Mar 06 markus 33
781 05 Mar 07 peter 34 #include <cassert>
781 05 Mar 07 peter 35
529 01 Mar 06 markus 36 namespace theplu {
680 11 Oct 06 jari 37 namespace yat {
4200 19 Aug 22 peter 38 namespace statistics {
529 01 Mar 06 markus 39
4200 19 Aug 22 peter 40   SNRScore::SNRScore(bool b)
856 05 Sep 07 markus 41     : Score(b)
529 01 Mar 06 markus 42   {
529 01 Mar 06 markus 43   }
529 01 Mar 06 markus 44
856 05 Sep 07 markus 45   SNRScore::~SNRScore(void)
529 01 Mar 06 markus 46   {
856 05 Sep 07 markus 47   }
856 05 Sep 07 markus 48
4200 19 Aug 22 peter 49   double SNRScore::score(const classifier::Target& target,
1023 01 Feb 08 peter 50                          const utility::VectorBase& value) const
856 05 Sep 07 markus 51   {
529 01 Mar 06 markus 52     statistics::Averager positive;
529 01 Mar 06 markus 53     statistics::Averager negative;
529 01 Mar 06 markus 54     for(size_t i=0; i<target.size(); i++){
529 01 Mar 06 markus 55       if (target.binary(i))
529 01 Mar 06 markus 56         positive.add(value(i));
529 01 Mar 06 markus 57       else
529 01 Mar 06 markus 58         negative.add(value(i));
529 01 Mar 06 markus 59     }
529 01 Mar 06 markus 60     double diff = positive.mean() - negative.mean();
529 01 Mar 06 markus 61     double denom=positive.std()+negative.std();
856 05 Sep 07 markus 62     double snr=diff/denom;
4200 19 Aug 22 peter 63     if(positive.n()==0 || negative.n()==0)
856 05 Sep 07 markus 64       snr=0;
856 05 Sep 07 markus 65     if (snr<0 && absolute_)
4200 19 Aug 22 peter 66       snr=-snr;
856 05 Sep 07 markus 67     return snr;
529 01 Mar 06 markus 68   }
529 01 Mar 06 markus 69
4200 19 Aug 22 peter 70   double SNRScore::score(const classifier::Target& target,
856 05 Sep 07 markus 71                          const classifier::DataLookupWeighted1D& value) const
623 05 Sep 06 peter 72   {
623 05 Sep 06 peter 73     statistics::AveragerWeighted positive;
623 05 Sep 06 peter 74     statistics::AveragerWeighted negative;
623 05 Sep 06 peter 75     for(size_t i=0; i<target.size(); i++){
623 05 Sep 06 peter 76       if (target.binary(i))
623 05 Sep 06 peter 77         positive.add(value.data(i),value.weight(i));
623 05 Sep 06 peter 78       else
623 05 Sep 06 peter 79         negative.add(value.data(i),value.weight(i));
623 05 Sep 06 peter 80     }
623 05 Sep 06 peter 81     double diff = positive.mean() - negative.mean();
623 05 Sep 06 peter 82     double denom=positive.std()+negative.std();
623 05 Sep 06 peter 83     assert(denom);
856 05 Sep 07 markus 84     double snr=diff/denom;
4200 19 Aug 22 peter 85     if(positive.sum_w()==0 || negative.sum_w()==0)
856 05 Sep 07 markus 86       snr=0;
856 05 Sep 07 markus 87     if (snr<0 && absolute_)
4200 19 Aug 22 peter 88       snr=-snr;
856 05 Sep 07 markus 89     return snr;
623 05 Sep 06 peter 90   }
623 05 Sep 06 peter 91
623 05 Sep 06 peter 92
623 05 Sep 06 peter 93
4200 19 Aug 22 peter 94   double SNRScore::score(const classifier::Target& target,
1023 01 Feb 08 peter 95                     const utility::VectorBase& value,
1023 01 Feb 08 peter 96                     const utility::VectorBase& weight) const
529 01 Mar 06 markus 97   {
529 01 Mar 06 markus 98     statistics::AveragerWeighted positive;
529 01 Mar 06 markus 99     statistics::AveragerWeighted negative;
529 01 Mar 06 markus 100     for(size_t i=0; i<target.size(); i++){
529 01 Mar 06 markus 101       if (target.binary(i))
529 01 Mar 06 markus 102         positive.add(value(i),weight(i));
529 01 Mar 06 markus 103       else
529 01 Mar 06 markus 104         negative.add(value(i),weight(i));
529 01 Mar 06 markus 105     }
529 01 Mar 06 markus 106     double diff = positive.mean() - negative.mean();
529 01 Mar 06 markus 107     double denom=positive.std()+negative.std();
560 13 Mar 06 markus 108     assert(denom);
856 05 Sep 07 markus 109     double snr=diff/denom;
4200 19 Aug 22 peter 110     if(positive.sum_w()==0 || negative.sum_w()==0)
856 05 Sep 07 markus 111       snr=0;
856 05 Sep 07 markus 112     if (snr<0 && absolute_)
4200 19 Aug 22 peter 113       snr=-snr;
856 05 Sep 07 markus 114     return snr;
529 01 Mar 06 markus 115   }
529 01 Mar 06 markus 116
683 11 Oct 06 jari 117 }}} // of namespace statistics, yat, and theplu