1 | // $Id: AUC.cc 1487 2008-09-10 08:41:36Z 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://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 "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/VectorBase.h" |
---|
29 | |
---|
30 | #include <cassert> |
---|
31 | #include <cmath> |
---|
32 | #include <utility> |
---|
33 | #include <map> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace statistics { |
---|
38 | |
---|
39 | AUC::AUC(bool absolute) |
---|
40 | : Score(absolute) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | double AUC::score(const classifier::Target& target, |
---|
45 | const utility::VectorBase& 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::VectorBase& value, |
---|
77 | const utility::VectorBase& 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 | double cache_cumsum_pos_w=0; |
---|
98 | double cache_cumsum_neg_w=0; |
---|
99 | typedef MultiMap::const_iterator iter; |
---|
100 | |
---|
101 | for (iter i=m.begin(); i!=m.end(); ++i) { |
---|
102 | if (i->second.first){ |
---|
103 | cache_cumsum_pos_w += i->second.second; |
---|
104 | } |
---|
105 | else |
---|
106 | cache_cumsum_neg_w += i->second.second; |
---|
107 | |
---|
108 | iter n(i); |
---|
109 | ++n; |
---|
110 | // last entry OR entry is smaller than next one |
---|
111 | if ( n==m.end() || i->first < n->first) { |
---|
112 | if (i->second.first) |
---|
113 | area += cache_cumsum_pos_w * ( cumsum_neg_w + cache_cumsum_neg_w/2 ); |
---|
114 | cumsum_pos_w += cache_cumsum_pos_w; |
---|
115 | cache_cumsum_pos_w = 0; |
---|
116 | cumsum_neg_w += cache_cumsum_neg_w; |
---|
117 | cache_cumsum_neg_w = 0; |
---|
118 | } |
---|
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 |
---|