yat
0.8.3pre
|
00001 #ifndef _theplu_yat_statistics_roc_ 00002 #define _theplu_yat_statistics_roc_ 00003 00004 // $Id: ROC.h 2624 2011-11-07 22:02:28Z peter $ 00005 00006 /* 00007 Copyright (C) 2004 Peter Johansson 00008 Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson 00009 Copyright (C) 2011 Peter Johansson 00010 00011 This file is part of the yat library, http://dev.thep.lu.se/yat 00012 00013 The yat library is free software; you can redistribute it and/or 00014 modify it under the terms of the GNU General Public License as 00015 published by the Free Software Foundation; either version 3 of the 00016 License, or (at your option) any later version. 00017 00018 The yat library is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00021 General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 along with yat. If not, see <http://www.gnu.org/licenses/>. 00025 */ 00026 00027 #include <gsl/gsl_randist.h> 00028 00029 #include <map> 00030 #include <utility> 00031 00032 namespace theplu { 00033 namespace yat { 00034 namespace statistics { 00035 00045 class ROC 00046 { 00047 00048 public: 00052 ROC(void); 00053 00067 void add(double value, bool target, double weight=1.0); 00068 00076 double area(void); 00077 00085 unsigned int& minimum_size(void); 00086 00100 const unsigned int& minimum_size(void) const; 00101 00107 double n(void) const; 00108 00114 double n_neg(void) const; 00115 00121 double n_pos(void) const; 00122 00164 double p_value_one_sided(void) const; 00165 00183 double p_value(void) const; 00184 00188 void reset(void); 00189 00190 private: 00191 typedef std::multimap<double, std::pair<bool, double> > Map; 00192 00194 double get_p_approx(double) const; 00195 00196 /* 00197 */ 00198 template<typename ForwardIterator> 00199 double p_exact_with_ties(ForwardIterator first, ForwardIterator last, 00200 double block, double pos, double neg) const; 00201 00209 double p_exact(double area) const; 00210 00211 bool use_exact_method(void) const; 00212 00213 double area_; 00214 bool has_ties_; 00215 unsigned int minimum_size_; 00216 double w_neg_; 00217 double w_pos_; 00218 Map multimap_; 00219 }; 00220 00221 template<typename ForwardIterator> 00222 double 00223 ROC::p_exact_with_ties(ForwardIterator begin, ForwardIterator end, 00224 double block, double pos, double neg) const 00225 { 00226 if (block <= 0) 00227 return 1.0; 00228 if (block > pos*neg) 00229 return 0.0; 00230 00231 ForwardIterator iter(begin); 00232 size_t n=0; 00233 while (iter!=end && iter->first == begin->first) { 00234 ++iter; 00235 ++n; 00236 } 00237 double result = 0; 00238 for (size_t i=0; i<=n; ++i) { 00239 double pos1 = i; 00240 double neg1 = n-i; 00241 double pos2 = pos-pos1; 00242 double neg2 = neg-neg1; 00243 result += gsl_ran_hypergeometric_pdf(i, static_cast<unsigned int>(pos), 00244 static_cast<unsigned int>(neg), n) 00245 * p_exact_with_ties(iter, end, 00246 block - pos2*neg1 - 0.5*pos1*neg1, 00247 pos2, neg2); 00248 } 00249 return result; 00250 } 00251 00252 }}} // of namespace statistics, yat, and theplu 00253 00254 #endif