1 | // $Id: ROC.cc 1797 2009-02-12 18:07:10Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004, 2005 Peter Johansson |
---|
5 | Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 3 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "ROC.h" |
---|
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 <gsl/gsl_cdf.h> |
---|
31 | |
---|
32 | #include <cassert> |
---|
33 | #include <cmath> |
---|
34 | #include <utility> |
---|
35 | #include <vector> |
---|
36 | |
---|
37 | namespace theplu { |
---|
38 | namespace yat { |
---|
39 | namespace statistics { |
---|
40 | |
---|
41 | ROC::ROC(void) |
---|
42 | :minimum_size_(10) |
---|
43 | { |
---|
44 | reset(); |
---|
45 | } |
---|
46 | |
---|
47 | ROC::~ROC(void) |
---|
48 | { |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | void ROC::add(double x, bool target, double w) |
---|
53 | { |
---|
54 | if (!w) |
---|
55 | return; |
---|
56 | multimap_.insert(std::make_pair(x, std::make_pair(target, w))); |
---|
57 | if (target) |
---|
58 | w_pos_+=w; |
---|
59 | else |
---|
60 | w_neg_+=w; |
---|
61 | area_=-1; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | double ROC::area(void) |
---|
66 | { |
---|
67 | if (area_==-1){ |
---|
68 | AUC auc(false); |
---|
69 | area_=auc.score(multimap_); |
---|
70 | } |
---|
71 | return area_; |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | double ROC::get_p_approx(double x) const |
---|
76 | { |
---|
77 | // make x standard normal |
---|
78 | x -= 0.5; |
---|
79 | // Not integrating from the middle of the bin, but from the inner edge. |
---|
80 | if (x>0) |
---|
81 | x -= 0.5/(n_pos()*n_neg()); |
---|
82 | else if(x<0) |
---|
83 | x += 0.5/(n_pos()*n_neg()); |
---|
84 | else if (x==0) |
---|
85 | return 0.5; |
---|
86 | double sigma = std::sqrt( ( n()+1.0 )/(12*n_pos()*n_neg()) ); |
---|
87 | return gsl_cdf_gaussian_Q(x, sigma); |
---|
88 | } |
---|
89 | |
---|
90 | double ROC::get_p_exact(const double block, const double nof_pos, |
---|
91 | const double nof_neg) const |
---|
92 | { |
---|
93 | if (block <= 0.0) |
---|
94 | return 1.0; |
---|
95 | if (block > nof_neg*nof_pos) |
---|
96 | return 0.0; |
---|
97 | double p1 = get_p_exact(block-nof_neg, nof_pos-1, nof_neg); |
---|
98 | double p2 = get_p_exact(block, nof_pos, nof_neg-1); |
---|
99 | return nof_pos/(nof_pos+nof_neg)*p1 + nof_neg/(nof_pos+nof_neg)*p2; |
---|
100 | } |
---|
101 | |
---|
102 | unsigned int& ROC::minimum_size(void) |
---|
103 | { |
---|
104 | return minimum_size_; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | const unsigned int& ROC::minimum_size(void) const |
---|
109 | { |
---|
110 | return minimum_size_; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | double ROC::n(void) const |
---|
115 | { |
---|
116 | return n_pos()+n_neg(); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | double ROC::n_neg(void) const |
---|
121 | { |
---|
122 | return w_neg_; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | double ROC::n_pos(void) const |
---|
127 | { |
---|
128 | return w_pos_; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | double ROC::p_value() const |
---|
133 | { |
---|
134 | double p = 2*p_value_one_sided(); |
---|
135 | return std::min(p, 2-p); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | double ROC::p_value_one_sided() const |
---|
140 | { |
---|
141 | double area(area_); |
---|
142 | if (area_==-1){ |
---|
143 | AUC auc(false); |
---|
144 | area = auc.score(multimap_); |
---|
145 | } |
---|
146 | if (n_pos() < minimum_size_ && n_neg() < minimum_size_) { |
---|
147 | // for small areas we calculate probabilitu to get larger area - |
---|
148 | // not larger or equal. |
---|
149 | if (area<0.5) |
---|
150 | return 1-get_p_exact((1-area)*n_pos()*n_neg(), n_pos(), n_neg()); |
---|
151 | return get_p_exact(area*n_pos()*n_neg(), n_pos(), n_neg()); |
---|
152 | } |
---|
153 | return get_p_approx(area); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | void ROC::reset(void) |
---|
158 | { |
---|
159 | area_=-1; |
---|
160 | w_pos_=0; |
---|
161 | w_neg_=0; |
---|
162 | multimap_.clear(); |
---|
163 | } |
---|
164 | |
---|
165 | }}} // of namespace statistics, yat, and theplu |
---|