source: trunk/lib/statistics/Fisher.h @ 448

Last change on this file since 448 was 448, checked in by Peter, 16 years ago

oops, at least compiling now

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1// $Id: Fisher.h 448 2005-12-15 19:05:57Z peter $
2
3#ifndef _theplu_statistics_fisher_
4#define _theplu_statistics_fisher_
5
6#include <c++_tools/statistics/Score.h>
7#include <c++_tools/gslapi/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    /// Cutoff sets the limit whether a value should go into the left
61    /// or the right column. @see score
62    ///
63    /// @return reference to cutoff for column
64    ///
65    inline double& cutoff_column(void) { return cutoff_column_; }
66
67    ///
68    /// Cutoff sets the limit whether a value should go into the left
69    /// or the right row. @see score
70    ///
71    /// @return reference to cutoff for row
72    ///
73    inline double& cutoff_row(void) { return cutoff_row_; }
74
75    ///
76    /// Calculates the expected values under the null hypothesis.
77    /// a' = (a+c)(a+b)/(a+b+c+d)
78    ///
79    void expected(double& a, double& b, double& c, double& d);
80
81    ///
82    /// minimum_size is the threshold for when the p-value calculation
83    /// is performed using a Chi2 approximation.
84    ///
85    /// @return reference to minimum_size
86    ///
87    inline u_int& minimum_size(void){ return minimum_size_; } 
88
89    ///
90    /// If absolute, the p-value is the two-sided p-value. If all
91    /// elements in table is at least minimum_size, a Chi2
92    /// approximation is used.
93    ///
94    /// @return p-value
95    ///
96    double p_value() const;
97   
98    ///
99    /// Function calculating score from 2x2 table for which the
100    /// elements are calculated as follows \n
101    /// a: #data \f$ x=1 \f$ AND \f$ y=1 \f$ \n
102    /// b: #data \f$ x=-1 \f$ AND \f$ y=1 \f$ \n
103    /// c: #data \f$ x=1 \f$ AND \f$ y=-1 \f$ \n
104    /// d: #data \f$ x=-1 \f$ AND \f$ y=1 \f$ \n
105    ///
106    /// @return odds ratio. If absolute_ is true and odds ratio is
107    /// less than unity 1 divided by odds ratio is returned
108    ///
109    double score(const gslapi::vector& x, const gslapi::vector& y, 
110                 const std::vector<size_t>& = std::vector<size_t>());
111
112    ///
113    /// Weighted version of score. Each element in 2x2 table is
114    /// calculated as \f$ \sum w_i \f$, so when each weight is
115    /// unitary the same table is created as in the unweighted version
116    ///
117    /// @return odds ratio
118    ///
119    /// @note
120    ///
121    double score(const gslapi::vector& x, const gslapi::vector& y, 
122                 const gslapi::vector& w,
123                 const std::vector<size_t>& = std::vector<size_t>());
124
125    ///
126    /// \f$ \frac{ad}{bc} \f$
127    ///
128    /// @return odds ratio. If absolute_ is true and odds ratio is
129    /// less than unity, 1 divided by odds ratio is returned
130    ///
131    double score(const u_int a, const u_int b, 
132                 const u_int c, const u_int d); 
133   
134
135         
136  private:
137    double oddsratio(const double a, const double b, 
138                     const double c, const double d) const;
139
140    double p_value_approximative(void) const;
141    double p_value_exact(void) const;
142
143    std::vector<size_t> train_set_;
144    gslapi::vector weight_;
145    u_int a_;
146    u_int b_;
147    u_int c_;
148    u_int d_;
149    double cutoff_column_;
150    double cutoff_row_;
151    u_int minimum_size_;
152
153  };
154
155}} // of namespace statistics and namespace theplu
156
157#endif
158
Note: See TracBrowser for help on using the repository browser.