1 | // $Id: ROC.cc 69 2004-04-29 14:21:25Z peter $ |
---|
2 | |
---|
3 | // System includes |
---|
4 | #include <algorithm> |
---|
5 | #include <utility> |
---|
6 | #include <vector> |
---|
7 | |
---|
8 | // GSL includes |
---|
9 | //#include <gsl/gsl_cdf.h> |
---|
10 | |
---|
11 | // Thep C++ Tools |
---|
12 | #include "ROC.h" |
---|
13 | #include "stl_utility.h" |
---|
14 | #include "vector.h" |
---|
15 | |
---|
16 | namespace theplu { |
---|
17 | namespace cpptools { |
---|
18 | |
---|
19 | ROC::ROC(const gslapi::vector& target, const gslapi::vector& value) |
---|
20 | |
---|
21 | : value_(), nof_pos_(0), minimum_size_(10) |
---|
22 | |
---|
23 | |
---|
24 | { |
---|
25 | for (unsigned int i=0; i<target.size(); i++){ |
---|
26 | int targ=static_cast<int>(target(i)); |
---|
27 | std::pair<int, double> tmp(targ, value(i)); |
---|
28 | value_.push_back(tmp); |
---|
29 | if (targ==1) |
---|
30 | nof_pos_++; |
---|
31 | } |
---|
32 | sort(value_.begin(),value_.end(), |
---|
33 | pair_value_compare<int,double>()); |
---|
34 | |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | double ROC::area() |
---|
39 | { |
---|
40 | using namespace std; |
---|
41 | double area=0; |
---|
42 | for (unsigned int i=0; i<value_.size(); i++) |
---|
43 | if (value_[i].first==1) |
---|
44 | area+=i; |
---|
45 | // Normalizing the area to 0-1 |
---|
46 | area = (2*area/nof_pos_ - nof_pos_ + 1)/(2*value_.size() - 2*nof_pos_); |
---|
47 | return area; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | double ROC::get_p(const double area) |
---|
52 | { |
---|
53 | double p; |
---|
54 | if (nof_pos_ < minimum_size_ & value_.size()-nof_pos_ < minimum_size_) |
---|
55 | p = get_p_exact(area*nof_pos_*(value_.size()-nof_pos_), |
---|
56 | nof_pos_, value_.size()-nof_pos_); |
---|
57 | else |
---|
58 | p = get_p_approx(area); |
---|
59 | return p; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | double ROC::get_p_approx(const double area) |
---|
64 | { |
---|
65 | double x = area-0.5; |
---|
66 | double sigma = ((value_.size()-nof_pos_)*nof_pos_*value_.size()/12/ |
---|
67 | (2*value_.size() - 2*nof_pos_)); |
---|
68 | double p = gsl_cdf_gaussian_Q(x, sigma); |
---|
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 |
---|