source: trunk/c++_tools/statistics/Fisher.h @ 623

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

fixes #112 and refs #123 added overloaded function score taking Target and DataLookupWeighted1D, which is needed for InputRanker?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1#ifndef _theplu_statistics_fisher_
2#define _theplu_statistics_fisher_
3
4// $Id: Fisher.h 623 2006-09-05 02:13:12Z peter $
5
6#include <c++_tools/statistics/Score.h>
7#include <c++_tools/utility/vector.h>
8
9#include <cmath>
10
11namespace theplu {
12namespace statistics { 
13  ///
14  /// @brief Fisher's exact test.   
15  /// Fisher's Exact test is a procedure that you can use for data
16  /// in a two by two contingency table: \f[ \begin{tabular}{|c|c|}
17  /// \hline a&b \tabularnewline \hline c&d \tabularnewline \hline
18  /// \end{tabular} \f] Fisher's Exact Test is based on exact
19  /// probabilities from a specific distribution (the hypergeometric
20  /// distribution). There's really no lower bound on the amount of
21  /// data that is needed for Fisher's Exact Test. You do have to
22  /// have at least one data value in each row and one data value in
23  /// each column. If an entire row or column is zero, then you
24  /// don't really have a 2 by 2 table. But you can use Fisher's
25  /// Exact Test when one of the cells in your table has a zero in
26  /// it. Fisher's Exact Test is also very useful for highly
27  /// imbalanced tables. If one or two of the cells in a two by two
28  /// table have numbers in the thousands and one or two of the
29  /// other cells has numbers less than 5, you can still use
30  /// Fisher's Exact Test. For very large tables (where all four
31  /// entries in the two by two table are large), your computer may
32  /// take too much time to compute Fisher's Exact Test. In these
33  /// situations, though, you might as well use the Chi-square test
34  /// because a large sample approximation (that the Chi-square test
35  /// relies on) is very reasonable. If all elements are larger than
36  /// 10 a Chi-square test is reasonable to use.
37  ///
38  /// @note The statistica assumes that each column and row sum,
39  /// respectively, are fixed. Just because you have a 2x2 table, this
40  /// assumtion does not necessarily match you experimental upset. See
41  /// e.g. Barnard's test for alternative.
42  ///
43 
44  class Fisher : public Score
45  {
46 
47  public:
48    ///
49    /// Default Constructor.
50    ///
51    Fisher(bool absolute=true);
52
53    ///
54    /// Destructor
55    ///
56    virtual ~Fisher(void) {};
57         
58   
59    ///
60    /// @return Chi2 score
61    ///
62    double Chi2(void) const;
63
64    ///
65    /// Cutoff sets the limit whether a value should go into the left
66    /// or the right row. @see score
67    ///
68    /// @return reference to cutoff for row
69    ///
70    inline double& value_cutoff(void) { return value_cutoff_; }
71
72    ///
73    /// Calculates the expected values under the null hypothesis.
74    /// a' = (a+c)(a+b)/(a+b+c+d)
75    ///
76    void expected(double& a, double& b, double& c, double& d) const;
77
78    ///
79    /// minimum_size is the threshold for when the p-value calculation
80    /// is performed using a Chi2 approximation.
81    ///
82    /// @return reference to minimum_size
83    ///
84    inline u_int& minimum_size(void){ return minimum_size_; } 
85
86    ///
87    /// If absolute, the p-value is the two-sided p-value. If all
88    /// elements in table is at least minimum_size, a Chi2
89    /// approximation is used.
90    ///
91    /// @return p-value
92    ///
93    /// @note in weighted case, approximation Chi2 is always used.
94    ///
95    double p_value() const;
96   
97    ///
98    /// Function calculating score from 2x2 table for which the
99    /// elements are calculated as follows \n
100    /// target.binary(i) sample i in group a or c otherwise in b or d
101    /// \f$ value(i) > \f$ value_cutoff() sample i in group a or b
102    /// otherwise c or d\n
103    ///
104    /// @return odds ratio. If absolute_ is true and odds ratio is
105    /// less than unity 1 divided by odds ratio is returned
106    ///
107    double score(const classifier::Target& target, 
108                 const utility::vector& value);
109
110    ///
111    /// Weighted version of score. Each element in 2x2 table is
112    /// calculated as \f$ \sum w_i \f$, so when each weight is
113    /// unitary the same table is created as in the unweighted version
114    ///
115    /// @return odds ratio
116    ///
117    /// @see score
118    ///
119    double score(const classifier::Target& target, 
120                 const classifier::DataLookupWeighted1D& value);
121
122
123    ///
124    /// Weighted version of score. Each element in 2x2 table is
125    /// calculated as \f$ \sum w_i \f$, so when each weight is
126    /// unitary the same table is created as in the unweighted version
127    ///
128    /// @return odds ratio
129    ///
130    /// @see score
131    ///
132    double score(const classifier::Target& target, 
133                 const utility::vector& value,
134                 const utility::vector& weight); 
135
136    ///
137    /// \f$ \frac{ad}{bc} \f$
138    ///
139    /// @return odds ratio. If absolute_ is true and odds ratio is
140    /// less than unity, 1 divided by odds ratio is returned
141    ///
142    double score(const u_int a, const u_int b, 
143                 const u_int c, const u_int d); 
144   
145
146         
147  private:
148    double oddsratio(const double a, const double b, 
149                     const double c, const double d);
150
151    // two-sided
152    double p_value_approximative(void) const;
153    //two-sided
154    double p_value_exact(void) const;
155
156    double a_;
157    double b_;
158    double c_;
159    double d_;
160    u_int minimum_size_;
161    double oddsratio_;
162    double value_cutoff_;
163  };
164
165}} // of namespace statistics and namespace theplu
166
167#endif
168
Note: See TracBrowser for help on using the repository browser.