1 | // $Id: ROC.cc 1271 2008-04-09 16:11:07Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004, 2005 Peter Johansson |
---|
5 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
6 | Copyright (C) 2007 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 "ROC.h" |
---|
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/VectorBase.h" |
---|
32 | |
---|
33 | #include <gsl/gsl_cdf.h> |
---|
34 | |
---|
35 | #include <cassert> |
---|
36 | #include <cmath> |
---|
37 | #include <utility> |
---|
38 | #include <vector> |
---|
39 | |
---|
40 | namespace theplu { |
---|
41 | namespace yat { |
---|
42 | namespace statistics { |
---|
43 | |
---|
44 | ROC::ROC(void) |
---|
45 | :minimum_size_(10) |
---|
46 | { |
---|
47 | reset(); |
---|
48 | } |
---|
49 | |
---|
50 | ROC::~ROC(void) |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | void ROC::add(double x, bool target, double w) |
---|
56 | { |
---|
57 | if (!w) |
---|
58 | return; |
---|
59 | multimap_.insert(std::make_pair(x, std::make_pair(target, w))); |
---|
60 | if (target) |
---|
61 | w_pos_+=w; |
---|
62 | else |
---|
63 | w_neg_+=w; |
---|
64 | area_=-1; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | double ROC::area(void) |
---|
69 | { |
---|
70 | if (area_==-1){ |
---|
71 | AUC auc(false); |
---|
72 | area_=auc.score(multimap_); |
---|
73 | } |
---|
74 | return area_; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | double ROC::get_p_approx(double x) const |
---|
79 | { |
---|
80 | // make x standard normal |
---|
81 | x -= 0.5; |
---|
82 | // Not integrating from the middle of the bin, but from the inner edge. |
---|
83 | if (x>0) |
---|
84 | x -= 0.5/(n_pos()*n_neg()); |
---|
85 | else if(x<0) |
---|
86 | x += 0.5/(n_pos()*n_neg()); |
---|
87 | else if (x==0) |
---|
88 | return 0.5; |
---|
89 | double sigma = std::sqrt( ( n()+1.0 )/(12*n_pos()*n_neg()) ); |
---|
90 | return gsl_cdf_gaussian_Q(x, sigma); |
---|
91 | } |
---|
92 | |
---|
93 | double ROC::get_p_exact(const double block, const double nof_pos, |
---|
94 | const double nof_neg) const |
---|
95 | { |
---|
96 | if (block <= 0.0) |
---|
97 | return 1.0; |
---|
98 | if (block > nof_neg*nof_pos) |
---|
99 | return 0.0; |
---|
100 | double p1 = get_p_exact(block-nof_neg, nof_pos-1, nof_neg); |
---|
101 | double p2 = get_p_exact(block, nof_pos, nof_neg-1); |
---|
102 | return nof_pos/(nof_pos+nof_neg)*p1 + nof_neg/(nof_pos+nof_neg)*p2; |
---|
103 | } |
---|
104 | |
---|
105 | unsigned int& ROC::minimum_size(void) |
---|
106 | { |
---|
107 | return minimum_size_; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | const unsigned int& ROC::minimum_size(void) const |
---|
112 | { |
---|
113 | return minimum_size_; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | double ROC::n(void) const |
---|
118 | { |
---|
119 | return n_pos()+n_neg(); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | double ROC::n_neg(void) const |
---|
124 | { |
---|
125 | return w_neg_; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | double ROC::n_pos(void) const |
---|
130 | { |
---|
131 | return w_pos_; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | double ROC::p_value() const |
---|
136 | { |
---|
137 | double p = 2*p_value_one_sided(); |
---|
138 | return std::min(p, 2-p); |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | double ROC::p_value_one_sided() const |
---|
143 | { |
---|
144 | double area(area_); |
---|
145 | if (area_==-1){ |
---|
146 | AUC auc(false); |
---|
147 | area = auc.score(multimap_); |
---|
148 | } |
---|
149 | if (n_pos() < minimum_size_ && n_neg() < minimum_size_) { |
---|
150 | // for small areas we calculate probabilitu to get larger area - |
---|
151 | // not larger or equal. |
---|
152 | if (area<0.5) |
---|
153 | return 1-get_p_exact((1-area)*n_pos()*n_neg(), n_pos(), n_neg()); |
---|
154 | return get_p_exact(area*n_pos()*n_neg(), n_pos(), n_neg()); |
---|
155 | } |
---|
156 | return get_p_approx(area); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | void ROC::reset(void) |
---|
161 | { |
---|
162 | area_=-1; |
---|
163 | w_pos_=0; |
---|
164 | w_neg_=0; |
---|
165 | multimap_.clear(); |
---|
166 | } |
---|
167 | |
---|
168 | }}} // of namespace statistics, yat, and theplu |
---|