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

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

Updating copyright statements.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1// $Id: AUC.cc 1275 2008-04-11 06:10:12Z jari $
2
3/*
4  Copyright (C) 2004, 2005 Peter Johansson
5  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2008 Peter Johansson
7
8  This file is part of the yat library, http://trac.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "AUC.h"
27#include "yat/classifier/DataLookupWeighted1D.h"
28#include "yat/classifier/Target.h"
29#include "yat/utility/stl_utility.h"
30#include "yat/utility/VectorBase.h"
31
32#include <cassert>
33#include <cmath>
34#include <utility>
35#include <map>
36
37namespace theplu {
38namespace yat {
39namespace statistics { 
40
41  AUC::AUC(bool absolute)
42    : Score(absolute)
43  {
44  }
45
46  double AUC::score(const classifier::Target& target, 
47                    const utility::VectorBase& value) const
48  {
49    assert(target.size()==value.size());
50    // key data, pair<target, weight>
51    std::multimap<double, std::pair<bool, double> > m;
52    for (unsigned int i=0; i<target.size(); i++)
53      m.insert(std::make_pair(value(i), 
54                              std::make_pair(target.binary(i),1.0)));
55       
56    return score(m);
57  }
58
59
60
61  double AUC::score(const classifier::Target& target, 
62                    const classifier::DataLookupWeighted1D& value) const
63  {
64    assert(target.size()==value.size());
65    // key data, pair<target, weight>
66    std::multimap<double, std::pair<bool, double> > m;
67    for (unsigned int i=0; i<target.size(); i++)
68      if (value.weight(i))
69        m.insert(std::make_pair(value.data(i), 
70                                std::make_pair(target.binary(i), 
71                                               value.weight(i))));
72       
73    return score(m);
74  }
75
76
77  double AUC::score(const classifier::Target& target, 
78                    const utility::VectorBase& value, 
79                    const utility::VectorBase& weight) const
80  {
81    assert(target.size()==value.size());
82    assert(target.size()==weight.size());
83    // key data, pair<target, weight>
84    std::multimap<double, std::pair<bool, double> > m;
85    for (unsigned int i=0; i<target.size(); i++)
86      if (weight(i))
87        m.insert(std::make_pair(value(i), 
88                                std::make_pair(target.binary(i), weight(i))));
89       
90    return score(m);
91  }
92
93
94  double AUC::score(const MultiMap& m) const
95  {
96    double area=0;
97    double cumsum_pos_w=0;
98    double cumsum_neg_w=0;
99    double cache_cumsum_pos_w=0;
100    double cache_cumsum_neg_w=0;
101    typedef MultiMap::const_iterator iter;
102
103    for (iter i=m.begin(); i!=m.end(); ++i) {
104      if (i->second.first){
105        cache_cumsum_pos_w += i->second.second;
106      }
107      else
108        cache_cumsum_neg_w += i->second.second;
109
110      iter n(i);
111      ++n;
112      // last entry OR entry is smaller than next one
113      if ( n==m.end() || i->first < n->first) {
114        if (i->second.first)
115          area += cache_cumsum_pos_w * ( cumsum_neg_w + cache_cumsum_neg_w/2 );
116        cumsum_pos_w += cache_cumsum_pos_w;
117        cache_cumsum_pos_w = 0;
118        cumsum_neg_w += cache_cumsum_neg_w;
119        cache_cumsum_neg_w = 0;
120      }
121    }
122    // max area is cumsum_neg_w * cumsum_pos_w
123    area/=(cumsum_neg_w*cumsum_pos_w);
124   
125    if (area<0.5 && absolute_)
126      return 1.0-area;
127    return area;
128}
129
130}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.