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

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

Refs #185.

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