source: trunk/yat/statistics/ROC.cc @ 2119

Last change on this file since 2119 was 2119, checked in by Peter, 13 years ago

converted files to utf-8. fixes #577

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