source: trunk/src/ROC.cc @ 75

Last change on this file since 75 was 75, checked in by Peter, 19 years ago

fixed bug in get_p_approx

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.9 KB
Line 
1// $Id: ROC.cc 75 2004-05-04 09:51:58Z peter $
2
3// System includes
4#include <algorithm>
5#include <utility>
6#include <vector>
7
8// Thep C++ Tools
9#include "ROC.h"
10#include "stl_utility.h"
11#include "vector.h"
12
13namespace theplu {
14namespace cpptools { 
15
16  ROC::ROC(const gslapi::vector& target, const gslapi::vector& value) 
17 
18    : value_(), nof_pos_(0), minimum_size_(10)
19
20       
21  {
22    for (unsigned int i=0; i<target.size(); i++){
23      int targ=static_cast<int>(target(i));
24      std::pair<int, double> tmp(targ, value(i));
25      value_.push_back(tmp);
26      if (targ==1)
27        nof_pos_++;
28    }
29    sort(value_.begin(),value_.end(),
30         pair_value_compare<int,double>());
31   
32  }
33
34
35  double ROC::area()
36  {
37    using namespace std;
38    double area=0;
39    for (unsigned int i=0; i<value_.size(); i++)
40      if (value_[i].first==1)
41        area+=i;
42    // Normalizing the area to 0-1
43    area = (area/nof_pos_ - (nof_pos_ - 1)/2 )/(value_.size() - nof_pos_);
44
45    return area;
46  }
47
48
49  double ROC::get_p(const double area)
50  {
51    double p;
52    if (nof_pos_ < minimum_size_ & value_.size()-nof_pos_ < minimum_size_)
53      p = get_p_exact(area*nof_pos_*(value_.size()-nof_pos_), 
54                          nof_pos_, value_.size()-nof_pos_);
55    else
56    p = get_p_approx(area);
57    return p;
58  }
59
60
61  double ROC::get_p_approx(const double area) 
62  {
63    double x = area - 0.5;
64    double sigma = (pow((value_.size()-nof_pos_)* nof_pos_ *
65                        (value_.size()+1)/12, 0.5) /
66                    (value_.size() - nof_pos_ ) / nof_pos_);
67    double p = gsl_cdf_gaussian_Q(x, sigma);
68       
69    return p;
70  }
71
72
73  double ROC::get_p_exact(const double block, const double nof_pos, 
74                            const double nof_neg)
75  {
76    double p;
77    if (block <= 0.0)
78      p = 1.0;
79    else if (block > nof_neg*nof_pos)
80      p = 0.0;
81    else {
82      double p1 = get_p_exact(block-nof_neg, nof_pos-1, nof_neg);
83      double p2 = get_p_exact(block, nof_pos, nof_neg-1);
84      p = nof_pos/(nof_pos+nof_neg)*p1 + nof_neg/(nof_pos+nof_neg)*p2;
85    }
86    return p;
87  }
88
89}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.