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

Last change on this file since 1701 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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