1 | #ifndef _theplu_statistics_roc_ |
---|
2 | #define _theplu_statistics_roc_ |
---|
3 | |
---|
4 | // $Id: ROC.h 650 2006-09-15 15:12:44Z peter $ |
---|
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 utility { |
---|
14 | class vector; |
---|
15 | } |
---|
16 | namespace statistics { |
---|
17 | |
---|
18 | /// |
---|
19 | /// Class for ROC (Reciever Operating Characteristic). |
---|
20 | /// |
---|
21 | /// As the area under an ROC curve is equivalent to Mann-Whitney U |
---|
22 | /// statistica, this class can be used to perform a Mann-Whitney |
---|
23 | /// U-test (aka Wilcoxon). |
---|
24 | /// |
---|
25 | class ROC : public Score |
---|
26 | { |
---|
27 | |
---|
28 | public: |
---|
29 | /// |
---|
30 | /// Default constructor |
---|
31 | /// |
---|
32 | ROC(bool absolute=true); |
---|
33 | |
---|
34 | /// |
---|
35 | /// Destructor |
---|
36 | /// |
---|
37 | virtual ~ROC(void) {}; |
---|
38 | |
---|
39 | /// Function taking \a value, \a target (+1 or -1) and vector |
---|
40 | /// defining what samples to use. The score is equivalent to |
---|
41 | /// Mann-Whitney statistics. |
---|
42 | /// @return the area under the ROC curve. If the area is less |
---|
43 | /// than 0.5 and absolute=true, 1-area is returned. Complexity is |
---|
44 | /// \f$ N\log N \f$ where \f$ N \f$ is number of samples. |
---|
45 | /// |
---|
46 | double score(const classifier::Target& target, |
---|
47 | const utility::vector& value); |
---|
48 | |
---|
49 | /** |
---|
50 | Function taking values, target, weight and a vector defining |
---|
51 | what samples to use. The area is defines as \f$ \frac{\sum |
---|
52 | w^+w^-}{\sum w^+w^-}\f$, where the sum in the numerator goes |
---|
53 | over all pairs where value+ is larger than value-. The |
---|
54 | denominator goes over all pairs. If target is equal to 1, |
---|
55 | sample belonges to class + otherwise sample belongs to class |
---|
56 | -. @return wheighted version of area under the ROC curve. If |
---|
57 | the area is less than 0.5 and absolute=true, 1-area is |
---|
58 | returned. Complexity is \f$ N^2 \f$ where \f$ N \f$ is number |
---|
59 | of samples. |
---|
60 | */ |
---|
61 | double score(const classifier::Target& target, |
---|
62 | const classifier::DataLookupWeighted1D& value); |
---|
63 | |
---|
64 | |
---|
65 | /** |
---|
66 | Function taking values, target, weight and a vector defining |
---|
67 | what samples to use. The area is defines as \f$ \frac{\sum |
---|
68 | w^+w^-}{\sum w^+w^-}\f$, where the sum in the numerator goes |
---|
69 | over all pairs where value+ is larger than value-. The |
---|
70 | denominator goes over all pairs. If target is equal to 1, |
---|
71 | sample belonges to class + otherwise sample belongs to class |
---|
72 | -. @return wheighted version of area under the ROC curve. If |
---|
73 | the area is less than 0.5 and absolute=true, 1-area is |
---|
74 | returned. Complexity is \f$ N^2 \f$ where \f$ N \f$ is number |
---|
75 | of samples. |
---|
76 | */ |
---|
77 | double score(const classifier::Target& target, |
---|
78 | const utility::vector& value, |
---|
79 | const utility::vector& weight); |
---|
80 | |
---|
81 | |
---|
82 | /// |
---|
83 | ///Calculates the p-value, i.e. the probability of observing an |
---|
84 | ///area equally or larger if the null hypothesis is true. If P is |
---|
85 | ///near zero, this casts doubt on this hypothesis. The null |
---|
86 | ///hypothesis is that the values from the 2 classes are generated |
---|
87 | ///from 2 identical distributions. The alternative is that the |
---|
88 | ///median of the first distribution is shifted from the median of |
---|
89 | ///the second distribution by a non-zero amount. If the smallest |
---|
90 | ///group size is larger than minimum_size (default = 10), then P |
---|
91 | ///is calculated using a normal approximation. @return the |
---|
92 | ///one-sided p-value( if absolute true is used this is equivalent |
---|
93 | ///to the two-sided p-value.) |
---|
94 | /// |
---|
95 | double p_value(void) const; |
---|
96 | |
---|
97 | /// |
---|
98 | /// minimum_size is the threshold for when a normal |
---|
99 | /// approximation is used for the p-value calculation. |
---|
100 | /// |
---|
101 | /// @return reference to minimum_size |
---|
102 | /// |
---|
103 | inline u_int& minimum_size(void){ return minimum_size_; } |
---|
104 | |
---|
105 | /// |
---|
106 | /// Function returning true if target is positive (binary()) for |
---|
107 | /// the sample with ith lowest data value, so i=0 corresponds to |
---|
108 | /// the sample with the lowest data value and i=n()-1 the sample |
---|
109 | /// with highest data value. |
---|
110 | /// |
---|
111 | bool target(const size_t i) const; |
---|
112 | |
---|
113 | /// |
---|
114 | /// @return number of samples |
---|
115 | /// |
---|
116 | inline size_t n(void) const { return vec_pair_.size(); } |
---|
117 | |
---|
118 | /// |
---|
119 | /// @return number of positive samples (Target.binary()==true) |
---|
120 | /// |
---|
121 | inline size_t n_pos(void) const { return nof_pos_; } |
---|
122 | |
---|
123 | private: |
---|
124 | |
---|
125 | /// Implemented as in MatLab 13.1 |
---|
126 | double get_p_approx(const double) const; |
---|
127 | |
---|
128 | /// Implemented as in MatLab 13.1 |
---|
129 | double get_p_exact(const double, const double, const double) const; |
---|
130 | |
---|
131 | double area_; |
---|
132 | u_int minimum_size_; |
---|
133 | u_int nof_pos_; |
---|
134 | std::vector<std::pair<bool, double> > vec_pair_; // class-value-pair |
---|
135 | }; |
---|
136 | |
---|
137 | /// |
---|
138 | /// The output operator for the ROC class. The output is an Nx2 |
---|
139 | /// matrix, where the first column is the sensitivity and second |
---|
140 | /// is the specificity. |
---|
141 | /// |
---|
142 | std::ostream& operator<< (std::ostream& s, const ROC&); |
---|
143 | |
---|
144 | |
---|
145 | }} // of namespace statistics and namespace theplu |
---|
146 | |
---|
147 | #endif |
---|
148 | |
---|