1 | #ifndef _theplu_yat_statistics_roc_ |
---|
2 | #define _theplu_yat_statistics_roc_ |
---|
3 | |
---|
4 | // $Id: ROC.h 1487 2008-09-10 08:41:36Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004 Peter Johansson |
---|
8 | Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 3 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "yat/classifier/Target.h" |
---|
27 | #include "yat/utility/iterator_traits.h" |
---|
28 | |
---|
29 | #include <algorithm> |
---|
30 | #include <map> |
---|
31 | #include <utility> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace statistics { |
---|
36 | |
---|
37 | /// |
---|
38 | /// @brief Class for Reciever Operating Characteristic. |
---|
39 | /// |
---|
40 | /// As the area under an ROC curve is equivalent to Mann-Whitney U |
---|
41 | /// statistica, this class can be used to perform a Mann-Whitney |
---|
42 | /// U-test (aka Wilcoxon). |
---|
43 | /// |
---|
44 | class ROC |
---|
45 | { |
---|
46 | |
---|
47 | public: |
---|
48 | /// |
---|
49 | /// @brief Default constructor |
---|
50 | /// |
---|
51 | ROC(void); |
---|
52 | |
---|
53 | /// |
---|
54 | /// @brief The destructor |
---|
55 | /// |
---|
56 | virtual ~ROC(void); |
---|
57 | |
---|
58 | /** |
---|
59 | Adding a data value to ROC. |
---|
60 | |
---|
61 | \see add(T &o, ForwardIterator first, ForwardIterator last, |
---|
62 | const classifier::Target &target) |
---|
63 | */ |
---|
64 | void add(double value, bool target, double weight=1.0); |
---|
65 | |
---|
66 | /** |
---|
67 | The area is defines as \f$ \frac{\sum w^+w^-} {\sum w^+w^-}\f$, |
---|
68 | where the sum in the numerator goes over all pairs where value+ |
---|
69 | is larger than value-. The denominator goes over all pairs. |
---|
70 | |
---|
71 | @return Area under curve. |
---|
72 | */ |
---|
73 | double area(void); |
---|
74 | |
---|
75 | /// |
---|
76 | /// minimum_size is the threshold for when a normal |
---|
77 | /// approximation is used for the p-value calculation. |
---|
78 | /// |
---|
79 | /// @return reference to minimum_size |
---|
80 | /// |
---|
81 | unsigned int& minimum_size(void); |
---|
82 | |
---|
83 | /** |
---|
84 | minimum_size is the threshold for when a normal |
---|
85 | approximation is used for the p-value calculation. |
---|
86 | |
---|
87 | @return const reference to minimum_size |
---|
88 | */ |
---|
89 | const unsigned int& minimum_size(void) const; |
---|
90 | |
---|
91 | /// |
---|
92 | /// @return sum of weights |
---|
93 | /// |
---|
94 | double n(void) const; |
---|
95 | |
---|
96 | /// |
---|
97 | /// @return sum of weights with negative target |
---|
98 | /// |
---|
99 | double n_neg(void) const; |
---|
100 | |
---|
101 | /// |
---|
102 | /// @return sum of weights with positive target |
---|
103 | /// |
---|
104 | double n_pos(void) const; |
---|
105 | |
---|
106 | /// |
---|
107 | ///Calculates the p-value, i.e. the probability of observing an |
---|
108 | ///area equally or larger if the null hypothesis is true. If P is |
---|
109 | ///near zero, this casts doubt on this hypothesis. The null |
---|
110 | ///hypothesis is that the values from the 2 classes are generated |
---|
111 | ///from 2 identical distributions. The alternative is that the |
---|
112 | ///median of the first distribution is shifted from the median of |
---|
113 | ///the second distribution by a non-zero amount. If the smallest |
---|
114 | ///group size is larger than minimum_size (default = 10), then P |
---|
115 | ///is calculated using a normal approximation. |
---|
116 | /// |
---|
117 | /// \note Weights should be either zero or unity, else present |
---|
118 | /// implementation is nonsense. |
---|
119 | /// |
---|
120 | /// @return One-sided p-value. |
---|
121 | /// |
---|
122 | double p_value_one_sided(void) const; |
---|
123 | |
---|
124 | /** |
---|
125 | @brief Two-sided p-value. |
---|
126 | |
---|
127 | @return min(2*p_value_one_sided, 2-2*p_value_one_sided) |
---|
128 | */ |
---|
129 | double p_value(void) const; |
---|
130 | |
---|
131 | /** |
---|
132 | @brief Set everything to zero |
---|
133 | */ |
---|
134 | void reset(void); |
---|
135 | |
---|
136 | private: |
---|
137 | |
---|
138 | /// Implemented as in MatLab 13.1 |
---|
139 | double get_p_approx(double) const; |
---|
140 | |
---|
141 | /// Implemented as in MatLab 13.1 |
---|
142 | double get_p_exact(const double, const double, const double) const; |
---|
143 | |
---|
144 | double area_; |
---|
145 | unsigned int minimum_size_; |
---|
146 | double w_neg_; |
---|
147 | double w_pos_; |
---|
148 | // <data pair<class, weight> > |
---|
149 | std::multimap<double, std::pair<bool, double> > multimap_; |
---|
150 | }; |
---|
151 | |
---|
152 | }}} // of namespace statistics, yat, and theplu |
---|
153 | |
---|
154 | #endif |
---|