source: trunk/yat/statistics/SAM.cc @ 683

Last change on this file since 683 was 683, checked in by Jari Häkkinen, 15 years ago

Addresses #153. Clean up of code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: SAM.cc 683 2006-10-11 22:20:36Z 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 "SAM.h"
25#include "Averager.h"
26#include "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 yat {
35namespace statistics { 
36
37  SAM::SAM(const double s0, bool b) 
38    : Score(b), s0_(s0)
39  {
40  }
41
42  double SAM::score(const classifier::Target& target, 
43                    const utility::vector& value)
44  {
45    weighted_=false;
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    if(positive.n()+negative.n()<=2) 
55      return 0;
56    double diff = positive.mean() - negative.mean();
57    double s2 = ( (1.0/positive.n()+1.0/negative.n()) * 
58                 (positive.sum_xx_centered()+negative.sum_xx_centered()) /
59                 (positive.n()+negative.n()-2) );
60    if (diff<0 && absolute_)
61      return -diff/(sqrt(s2)+s0_);
62    return diff/(sqrt(s2)+s0_);
63  }
64
65  double SAM::score(const classifier::Target& target, 
66                    const classifier::DataLookupWeighted1D& value)
67  {
68    weighted_=true;
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    if(positive.n()+negative.n()<=2) 
78      return 0;
79    double diff = positive.mean() - negative.mean();
80    double s2 = ( (1.0/positive.n()+1.0/negative.n()) * 
81                 (positive.sum_xx_centered()+negative.sum_xx_centered()) /
82                 (positive.n()+negative.n()-2) );
83    if (diff<0 && absolute_)
84      return -diff/(sqrt(s2)+s0_);
85    return diff/(sqrt(s2)+s0_);
86  }
87
88
89
90  double SAM::score(const classifier::Target& target, 
91                    const utility::vector& value,
92                    const utility::vector& weight)
93  {
94    weighted_=true;
95    statistics::AveragerWeighted positive;
96    statistics::AveragerWeighted negative;
97    for(size_t i=0; i<target.size(); i++){
98      if (target.binary(i))
99        positive.add(value(i),weight(i));
100      else
101        negative.add(value(i),weight(i));
102    }
103    if(positive.n()+negative.n()<=2) 
104      return 0;
105    double diff = positive.mean() - negative.mean();
106    double s2 = ( (1.0/positive.n()+1.0/negative.n()) * 
107                 (positive.sum_xx_centered()+negative.sum_xx_centered()) /
108                 (positive.n()+negative.n()-2) );
109    if (diff<0 && absolute_)
110      return -diff/(sqrt(s2)+s0_);
111    return diff/(sqrt(s2)+s0_);
112  }
113
114}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.