1 | // $Id: ROCscore.cc 779 2007-03-05 18:58:30Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) The authors contributing to this file. |
---|
5 | |
---|
6 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "ROCscore.h" |
---|
25 | #include "yat/classifier/DataLookupWeighted1D.h" |
---|
26 | #include "yat/classifier/Target.h" |
---|
27 | #include "yat/utility/stl_utility.h" |
---|
28 | #include "yat/utility/vector.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 | ROCscore::ROCscore(bool absolute) |
---|
40 | : Score(absolute) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | double ROCscore::score(const classifier::Target& target, |
---|
45 | const utility::vector& 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 ROCscore::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 ROCscore::score(const classifier::Target& target, |
---|
76 | const utility::vector& value, |
---|
77 | const utility::vector& 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 |
---|
93 | ROCscore::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 |
---|