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

Last change on this file since 675 was 675, checked in by Jari Häkkinen, 17 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.3 KB
Line 
1// $Id: SAM.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 "yat/statistics/SAM.h"
25#include "yat/statistics/Averager.h"
26#include "yat/statistics/AveragerWeighted.h"
27#include "yat/classifier/DataLookupWeighted1D.h"
28#include "yat/classifier/Target.h"
29#include "yat/utility/vector.h"
30
31#include <cmath>
32
33namespace theplu {
34namespace statistics { 
35
36  SAM::SAM(const double s0, bool b) 
37    : Score(b), s0_(s0)
38  {
39  }
40
41  double SAM::score(const classifier::Target& target, 
42                    const utility::vector& value)
43  {
44    weighted_=false;
45    statistics::Averager positive;
46    statistics::Averager negative;
47    for(size_t i=0; i<target.size(); i++){
48      if (target.binary(i))
49        positive.add(value(i));
50      else
51        negative.add(value(i));
52    }
53    if(positive.n()+negative.n()<=2) 
54      return 0;
55    double diff = positive.mean() - negative.mean();
56    double s2 = ( (1.0/positive.n()+1.0/negative.n()) * 
57                 (positive.sum_xx_centered()+negative.sum_xx_centered()) /
58                 (positive.n()+negative.n()-2) );
59    if (diff<0 && absolute_)
60      return -diff/(sqrt(s2)+s0_);
61    return diff/(sqrt(s2)+s0_);
62  }
63
64  double SAM::score(const classifier::Target& target, 
65                    const classifier::DataLookupWeighted1D& value)
66  {
67    weighted_=true;
68    statistics::AveragerWeighted positive;
69    statistics::AveragerWeighted negative;
70    for(size_t i=0; i<target.size(); i++){
71      if (target.binary(i))
72        positive.add(value.data(i),value.weight(i));
73      else
74        negative.add(value.data(i),value.weight(i));
75    }
76    if(positive.n()+negative.n()<=2) 
77      return 0;
78    double diff = positive.mean() - negative.mean();
79    double s2 = ( (1.0/positive.n()+1.0/negative.n()) * 
80                 (positive.sum_xx_centered()+negative.sum_xx_centered()) /
81                 (positive.n()+negative.n()-2) );
82    if (diff<0 && absolute_)
83      return -diff/(sqrt(s2)+s0_);
84    return diff/(sqrt(s2)+s0_);
85  }
86
87
88
89  double SAM::score(const classifier::Target& target, 
90                    const utility::vector& value,
91                    const utility::vector& weight)
92  {
93    weighted_=true;
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    if(positive.n()+negative.n()<=2) 
103      return 0;
104    double diff = positive.mean() - negative.mean();
105    double s2 = ( (1.0/positive.n()+1.0/negative.n()) * 
106                 (positive.sum_xx_centered()+negative.sum_xx_centered()) /
107                 (positive.n()+negative.n()-2) );
108    if (diff<0 && absolute_)
109      return -diff/(sqrt(s2)+s0_);
110    return diff/(sqrt(s2)+s0_);
111  }
112
113
114
115}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.