source: trunk/yat/statistics/AUC.cc @ 820

Last change on this file since 820 was 820, checked in by Peter, 15 years ago

Changed name again. ROCScore is now called AUC. Also fixed several bugs and added test for these.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1// $Id: AUC.cc 820 2007-03-17 21:54:52Z peter $
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 "AUC.h"
25#include "yat/classifier/DataLookupWeighted1D.h"
26#include "yat/classifier/Target.h"
27#include "yat/utility/stl_utility.h"
28#include "yat/utility/vector.h"
29
30#include <cassert>
31#include <cmath>
32#include <utility>
33#include <map>
34
35namespace theplu {
36namespace yat {
37namespace statistics { 
38
39  AUC::AUC(bool absolute)
40    : Score(absolute)
41  {
42  }
43
44  double AUC::score(const classifier::Target& target, 
45                    const utility::vector& value) const
46  {
47    assert(target.size()==value.size());
48    // key data, pair<target, weight>
49    std::multimap<double, std::pair<bool, double> > m;
50    for (unsigned int i=0; i<target.size(); i++)
51      m.insert(std::make_pair(value(i), 
52                              std::make_pair(target.binary(i),1.0)));
53       
54    return score(m);
55  }
56
57
58
59  double AUC::score(const classifier::Target& target, 
60                    const classifier::DataLookupWeighted1D& value) const
61  {
62    assert(target.size()==value.size());
63    // key data, pair<target, weight>
64    std::multimap<double, std::pair<bool, double> > m;
65    for (unsigned int i=0; i<target.size(); i++)
66      if (value.weight(i))
67        m.insert(std::make_pair(value.data(i), 
68                                std::make_pair(target.binary(i), 
69                                               value.weight(i))));
70       
71    return score(m);
72  }
73
74
75  double AUC::score(const classifier::Target& target, 
76                    const utility::vector& value, 
77                    const utility::vector& weight) const
78  {
79    assert(target.size()==value.size());
80    assert(target.size()==weight.size());
81    // key data, pair<target, weight>
82    std::multimap<double, std::pair<bool, double> > m;
83    for (unsigned int i=0; i<target.size(); i++)
84      if (weight(i))
85        m.insert(std::make_pair(value(i), 
86                                std::make_pair(target.binary(i), weight(i))));
87       
88    return score(m);
89  }
90
91
92  double AUC::score(const MultiMap& m) const
93  {
94    double area=0;
95    double cumsum_pos_w=0;
96    double cumsum_neg_w=0;
97    typedef MultiMap::const_iterator iter;
98
99    for (iter i=m.begin(); i!=m.end(); ++i)
100      if (i->second.first){
101        area += i->second.second * cumsum_neg_w;
102        cumsum_pos_w += i->second.second;
103      }
104      else
105        cumsum_neg_w += i->second.second;
106
107    // max area is cumsum_neg_w * cumsum_pos_w
108    area/=(cumsum_neg_w*cumsum_pos_w);
109   
110    if (area<0.5 && absolute_)
111      return 1.0-area;
112    return area;
113}
114
115}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.