1 | // $Id: ROC.h 509 2006-02-18 13:47:32Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_statistics_roc_ |
---|
4 | #define _theplu_statistics_roc_ |
---|
5 | |
---|
6 | #include <c++_tools/classifier/Target.h> |
---|
7 | #include <c++_tools/statistics/Score.h> |
---|
8 | |
---|
9 | #include <utility> |
---|
10 | #include <vector> |
---|
11 | |
---|
12 | namespace theplu { |
---|
13 | namespace statistics { |
---|
14 | |
---|
15 | /// |
---|
16 | /// Class for ROC (Reciever Operating Characteristic). |
---|
17 | /// |
---|
18 | class ROC : public Score |
---|
19 | { |
---|
20 | |
---|
21 | public: |
---|
22 | /// |
---|
23 | /// Default constructor |
---|
24 | /// |
---|
25 | ROC(bool absolute=true); |
---|
26 | |
---|
27 | /// |
---|
28 | /// Destructor |
---|
29 | /// |
---|
30 | virtual ~ROC(void) {}; |
---|
31 | |
---|
32 | /// Function taking \a value, \a target (+1 or -1) and vector |
---|
33 | /// defining what samples to use. The score is equivalent to |
---|
34 | /// Mann-Whitney statistics. |
---|
35 | /// @return the area under the ROC curve. If the area is less |
---|
36 | /// than 0.5 and absolute=true, 1-area is returned. Complexity is |
---|
37 | /// \f$ N\log N \f$ where \f$ N \f$ is number of samples. |
---|
38 | /// |
---|
39 | double score(const classifier::Target& target, |
---|
40 | const gslapi::vector& value); |
---|
41 | |
---|
42 | /// Function taking values, target, weight and a vector defining |
---|
43 | /// what samples to use. The area is defines as \f$ \frac{\sum |
---|
44 | /// w^+w^-}{\sum w^+w^-}\f$, where the sum in the numerator goes |
---|
45 | /// over all pairs where value+ is larger than value-. The |
---|
46 | /// denominator goes over all pairs. If target is equal to 1, |
---|
47 | /// sample belonges to class + otherwise sample belongs to class |
---|
48 | /// -. @return wheighted version of area under the ROC curve. If |
---|
49 | /// the area is less than 0.5 and absolute=true, 1-area is |
---|
50 | /// returned. Complexity is \f$ N^2 \f$ where \f$ N \f$ is number |
---|
51 | /// of samples. |
---|
52 | /// |
---|
53 | double score(const classifier::Target& target, |
---|
54 | const gslapi::vector& value, |
---|
55 | const gslapi::vector& weight); |
---|
56 | |
---|
57 | |
---|
58 | /// |
---|
59 | ///Calculates the p-value, i.e. the probability of observing an |
---|
60 | ///area equally or larger if the null hypothesis is true. If P is |
---|
61 | ///near zero, this casts doubt on this hypothesis. The null |
---|
62 | ///hypothesis is that the values from the 2 classes are generated |
---|
63 | ///from 2 identical distributions. The alternative is that the |
---|
64 | ///median of the first distribution is shifted from the median of |
---|
65 | ///the second distribution by a non-zero amount. If the smallest |
---|
66 | ///group size is larger than minimum_size (default = 10), then P |
---|
67 | ///is calculated using a normal approximation. @return the |
---|
68 | ///one-sided p-value( if absolute true is used this is equivalent |
---|
69 | ///to the two-sided p-value.) |
---|
70 | /// |
---|
71 | double p_value(void) const; |
---|
72 | |
---|
73 | /// |
---|
74 | /// minimum_size is the threshold for when a normal |
---|
75 | /// approximation is used for the p-value calculation. |
---|
76 | /// |
---|
77 | /// @return reference to minimum_size |
---|
78 | /// |
---|
79 | inline u_int& minimum_size(void){ return minimum_size_; } |
---|
80 | |
---|
81 | bool target(const size_t i) const; |
---|
82 | inline size_t n(void) const { return vec_pair_.size(); } |
---|
83 | inline size_t n_pos(void) const { return nof_pos_; } |
---|
84 | |
---|
85 | private: |
---|
86 | |
---|
87 | /// Implemented as in MatLab 13.1 |
---|
88 | double get_p_approx(const double) const; |
---|
89 | |
---|
90 | /// Implemented as in MatLab 13.1 |
---|
91 | double get_p_exact(const double, const double, const double) const; |
---|
92 | |
---|
93 | double area_; |
---|
94 | u_int minimum_size_; |
---|
95 | u_int nof_pos_; |
---|
96 | std::vector<std::pair<bool, double> > vec_pair_; // class-value-pair |
---|
97 | }; |
---|
98 | |
---|
99 | /// |
---|
100 | /// The output operator for the ROC class. The output is an Nx2 |
---|
101 | /// matrix, where the first column is the sensitivity and second |
---|
102 | /// is the specificity. |
---|
103 | /// |
---|
104 | std::ostream& operator<< (std::ostream& s, const ROC&); |
---|
105 | |
---|
106 | |
---|
107 | }} // of namespace statistics and namespace theplu |
---|
108 | |
---|
109 | #endif |
---|
110 | |
---|