source: branches/0.4-stable/yat/statistics/ROC.h @ 1392

Last change on this file since 1392 was 1392, checked in by Peter, 15 years ago

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1#ifndef _theplu_yat_statistics_roc_
2#define _theplu_yat_statistics_roc_
3
4// $Id: ROC.h 1392 2008-07-28 19:35:30Z peter $
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 2 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 this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include "yat/classifier/Target.h"
29#include "yat/utility/iterator_traits.h"
30
31#include <algorithm>
32#include <map>
33#include <utility>
34
35namespace theplu {
36namespace yat {
37namespace statistics { 
38
39  ///
40  /// @brief Class for Reciever Operating Characteristic.
41  ///   
42  /// As the area under an ROC curve is equivalent to Mann-Whitney U
43  /// statistica, this class can be used to perform a Mann-Whitney
44  /// U-test (aka Wilcoxon).
45  ///
46  class ROC
47  {
48 
49  public:
50    ///
51    /// @brief Default constructor
52    ///
53    ROC(void);
54         
55    ///
56    /// @brief The destructor
57    ///
58    virtual ~ROC(void);
59         
60    /**
61       Adding a data value to ROC.
62    */
63    void add(double value, bool target, double weight=1.0);
64
65    /**
66       The area is defines as \f$ \frac{\sum w^+w^-} {\sum w^+w^-}\f$,
67       where the sum in the numerator goes over all pairs where value+
68       is larger than value-. The denominator goes over all pairs.
69
70       @return Area under curve.
71    */
72    double area(void);
73
74    ///
75    /// minimum_size is the threshold for when a normal
76    /// approximation is used for the p-value calculation.
77    ///
78    /// @return reference to minimum_size
79    ///
80    unsigned int& minimum_size(void);
81
82    /**
83       minimum_size is the threshold for when a normal
84       approximation is used for the p-value calculation.
85       
86       @return const reference to minimum_size
87    */
88    const unsigned int& minimum_size(void) const;
89
90    ///
91    /// @return sum of weights
92    ///
93    double n(void) const;
94
95    ///
96    /// @return sum of weights with negative target
97    ///
98    double n_neg(void) const;
99
100    ///
101    /// @return sum of weights with positive target
102    ///
103    double n_pos(void) const;
104
105    ///
106    ///Calculates the p-value, i.e. the probability of observing an
107    ///area equally or larger if the null hypothesis is true. If P is
108    ///near zero, this casts doubt on this hypothesis. The null
109    ///hypothesis is that the values from the 2 classes are generated
110    ///from 2 identical distributions. The alternative is that the
111    ///median of the first distribution is shifted from the median of
112    ///the second distribution by a non-zero amount. If the smallest
113    ///group size is larger than minimum_size (default = 10), then P
114    ///is calculated using a normal approximation. 
115    ///
116    /// \note Weights should be either zero or unity, else present
117    /// implementation is nonsense.
118    ///
119    /// @return One-sided p-value.
120    ///
121    double p_value_one_sided(void) const;
122   
123    /**
124       @brief Two-sided p-value.
125
126       @return min(2*p_value_one_sided, 2-2*p_value_one_sided)
127    */
128    double p_value(void) const;
129
130    /**
131       @brief Set everything to zero
132    */
133    void reset(void);
134
135  private:
136   
137    /// Implemented as in MatLab 13.1
138    double get_p_approx(double) const;
139
140    /// Implemented as in MatLab 13.1
141    double get_p_exact(const double, const double, const double) const;
142
143    double area_;
144    unsigned int minimum_size_;
145    double w_neg_;
146    double w_pos_;
147    // <data pair<class, weight> >
148    std::multimap<double, std::pair<bool, double> > multimap_;
149  };
150
151}}} // of namespace statistics, yat, and theplu
152
153#endif
Note: See TracBrowser for help on using the repository browser.