source: trunk/yat/statistics/AUC.cc

Last change on this file was 2992, checked in by Peter, 9 years ago

set svndigest:ignore proprty to reflect when files were initially copied from other file (and early history should be ignored). closes #750

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svndigest:ignore set to 776
File size: 3.5 KB
Line 
1// $Id: AUC.cc 2992 2013-03-03 05:03:44Z peter $
2
3/*
4  Copyright (C) 2004, 2005 Peter Johansson
5  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2011, 2012 Peter Johansson
7
8  This file is part of the yat library, http://dev.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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include <config.h>
25
26#include <iostream>
27
28#include "AUC.h"
29#include "yat/classifier/DataLookupWeighted1D.h"
30#include "yat/classifier/Target.h"
31#include "yat/utility/stl_utility.h"
32#include "yat/utility/VectorBase.h"
33
34#include <cassert>
35#include <cmath>
36#include <utility>
37#include <map>
38
39namespace theplu {
40namespace yat {
41namespace statistics { 
42
43  AUC::AUC(bool absolute)
44    : Score(absolute)
45  {
46  }
47
48  double AUC::score(const classifier::Target& target, 
49                    const utility::VectorBase& value) const
50  {
51    assert(target.size()==value.size());
52    // key data, pair<target, weight>
53    std::multimap<double, std::pair<bool, double> > m;
54    for (unsigned int i=0; i<target.size(); i++)
55      m.insert(std::make_pair(value(i), 
56                              std::make_pair(target.binary(i),1.0)));
57       
58    return score(m);
59  }
60
61
62
63  double AUC::score(const classifier::Target& target, 
64                    const classifier::DataLookupWeighted1D& value) const
65  {
66    assert(target.size()==value.size());
67    // key data, pair<target, weight>
68    std::multimap<double, std::pair<bool, double> > m;
69    for (unsigned int i=0; i<target.size(); i++)
70      if (value.weight(i))
71        m.insert(std::make_pair(value.data(i), 
72                                std::make_pair(target.binary(i), 
73                                               value.weight(i))));
74       
75    return score(m);
76  }
77
78
79  double AUC::score(const classifier::Target& target, 
80                    const utility::VectorBase& value, 
81                    const utility::VectorBase& weight) const
82  {
83    assert(target.size()==value.size());
84    assert(target.size()==weight.size());
85    // key data, pair<target, weight>
86    std::multimap<double, std::pair<bool, double> > m;
87    for (unsigned int i=0; i<target.size(); i++)
88      if (weight(i))
89        m.insert(std::make_pair(value(i), 
90                                std::make_pair(target.binary(i), weight(i))));
91       
92    return score(m);
93  }
94
95
96  double AUC::score(const MultiMap& m) const
97  {
98    double area=0;
99    double cumsum_pos_w=0;
100    double cumsum_neg_w=0;
101    typedef MultiMap::const_iterator iter;
102
103    iter first = m.begin();
104    while (first!=m.end()) {
105      double local_cumsum_pos_w=0;
106      double local_cumsum_neg_w=0;
107      iter last = first;
108      while (last!=m.end() && first->first==last->first) {
109        if (last->second.first)
110          local_cumsum_pos_w += last->second.second;
111        else
112          local_cumsum_neg_w += last->second.second;
113        ++last;
114      } 
115      area += local_cumsum_pos_w * ( cumsum_neg_w + 0.5*local_cumsum_neg_w );
116      cumsum_pos_w += local_cumsum_pos_w;
117      cumsum_neg_w += local_cumsum_neg_w;
118      first = last;
119    }
120    // max area is cumsum_neg_w * cumsum_pos_w
121    area/=(cumsum_neg_w*cumsum_pos_w);
122   
123    if (area<0.5 && absolute_)
124      return 1.0-area;
125    return area;
126}
127
128}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.