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

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

Modified ROC class to use AUC class in calculation of ROC area. Refs #101

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