1 | // $Id: ROC.cc 98 2004-06-10 15:24:05Z 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 | |
---|
13 | namespace theplu { |
---|
14 | namespace cpptools { |
---|
15 | |
---|
16 | ROC::ROC(const gslapi::vector& target, const gslapi::vector& value) |
---|
17 | |
---|
18 | : Score(), value_(), nof_pos_(0), minimum_size_(10), area_(-1) |
---|
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::score() |
---|
36 | { |
---|
37 | using namespace std; |
---|
38 | if (area_==-1){ |
---|
39 | double area_=0; |
---|
40 | for (unsigned int i=0; i<value_.size(); i++) |
---|
41 | if (value_[i].first==1) |
---|
42 | area_+=i; |
---|
43 | // Normalizing the area to 0-1 |
---|
44 | area_ = (area_/nof_pos_ - (nof_pos_ - 1)/2 )/(value_.size() - nof_pos_); |
---|
45 | } |
---|
46 | return area_; |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | double ROC::p_value(void) |
---|
51 | { |
---|
52 | if (area_==-1) |
---|
53 | area_ = score(); |
---|
54 | double p; |
---|
55 | if (nof_pos_ < minimum_size_ & value_.size()-nof_pos_ < minimum_size_) |
---|
56 | p = get_p_exact(area_*nof_pos_*(value_.size()-nof_pos_), |
---|
57 | nof_pos_, value_.size()-nof_pos_); |
---|
58 | else |
---|
59 | p = get_p_approx(area_); |
---|
60 | return p; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | double ROC::get_p_approx(const double area) |
---|
65 | { |
---|
66 | double x = area - 0.5; |
---|
67 | // Not integrating from the middle of the bin, but from the inner edge. |
---|
68 | if (x>0) |
---|
69 | x -= 0.5/nof_pos_/(value_.size()-nof_pos_); |
---|
70 | else if(x<0) |
---|
71 | x += 0.5/nof_pos_/(value_.size()-nof_pos_); |
---|
72 | |
---|
73 | double sigma = (std::sqrt((value_.size()-nof_pos_)* nof_pos_ * |
---|
74 | (value_.size()+1)/12) / |
---|
75 | (value_.size() - nof_pos_ ) / nof_pos_); |
---|
76 | double p = gsl_cdf_gaussian_Q(x, sigma); |
---|
77 | |
---|
78 | return p; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | double ROC::get_p_exact(const double block, const double nof_pos, |
---|
83 | const double nof_neg) |
---|
84 | { |
---|
85 | double p; |
---|
86 | if (block <= 0.0) |
---|
87 | p = 1.0; |
---|
88 | else if (block > nof_neg*nof_pos) |
---|
89 | p = 0.0; |
---|
90 | else { |
---|
91 | double p1 = get_p_exact(block-nof_neg, nof_pos-1, nof_neg); |
---|
92 | double p2 = get_p_exact(block, nof_pos, nof_neg-1); |
---|
93 | p = nof_pos/(nof_pos+nof_neg)*p1 + nof_neg/(nof_pos+nof_neg)*p2; |
---|
94 | } |
---|
95 | return p; |
---|
96 | } |
---|
97 | |
---|
98 | }} // of namespace cpptools and namespace theplu |
---|