source: trunk/yat/statistics/Fisher.cc @ 2119

Last change on this file since 2119 was 2119, checked in by Peter, 13 years ago

converted files to utf-8. fixes #577

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1// $Id: Fisher.cc 2119 2009-12-12 23:11:43Z peter $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
7  Copyright (C) 2009 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "Fisher.h"
26#include "utility.h"
27
28#include <gsl/gsl_cdf.h>
29#include <gsl/gsl_randist.h>
30
31#include <algorithm>
32
33namespace theplu {
34namespace yat {
35namespace statistics {
36
37 
38  Fisher::Fisher()
39    : a_(0), b_(0), c_(0), d_(0), minimum_size_(10), oddsratio_(1.0)
40  {
41  }
42
43
44  Fisher::~Fisher()
45  {
46  }
47
48
49  bool Fisher::calculate_p_exact(void) const
50  {
51    return ( a_<minimum_size_ || b_<minimum_size_ || 
52             c_<minimum_size_ || d_<minimum_size_); 
53  }
54
55  double Fisher::Chi2() const
56  {
57    double a,b,c,d;
58    expected(a,b,c,d);
59    return (a-a_)*(a-a_)/a + (b-b_)*(b-b_)/b + 
60      (c-c_)*(c-c_)/c + (d-d_)*(d-d_)/d;
61  }
62
63
64  void Fisher::expected(double& a, double& b, double& c, double& d) const
65  {
66    double N = a_+b_+c_+d_;
67    a =((a_+b_)*(a_+c_)) / N;
68    b =((a_+b_)*(b_+d_)) / N;
69    c =((c_+d_)*(a_+c_)) / N;
70    d =((c_+d_)*(b_+d_)) / N;
71  }
72
73
74  unsigned int& Fisher::minimum_size(void)
75  {
76    return minimum_size_;
77  }
78
79
80  const unsigned int& Fisher::minimum_size(void) const
81  {
82    return minimum_size_;
83  }
84
85
86  double Fisher::oddsratio(const unsigned int a,
87                           const unsigned int b,
88                           const unsigned int c,
89                           const unsigned int d) 
90  {
91    // If a column sum or a row sum is zero, the table is nonsense
92    if ((a==0 || d==0) && (c==0 || b==0)){
93      throw std::runtime_error("runtime_error: Table in Fisher is not valid\n");
94    }
95    a_ = a;
96    b_ = b;
97    c_ = c;
98    d_ = d;
99
100    oddsratio_=static_cast<double>(a*d)/static_cast<double>(b*c);
101    return oddsratio_;
102  }
103
104
105  double Fisher::p_value() const
106  {
107    if (calculate_p_exact())
108      return p_value_exact();
109    return p_value_approximative();
110  }
111
112
113  double Fisher::p_value_one_sided() const
114  {
115    if (!calculate_p_exact()) {
116      if (oddsratio_<1.0)
117        return 1.0-p_value_approximative()/2.0;
118      return p_value_approximative()/2.0;
119    }
120    return statistics::cdf_hypergeometric_P(c_, c_+d_, a_+b_, a_+c_);
121  }
122
123
124  double Fisher::p_value_approximative() const
125  {
126    return gsl_cdf_chisq_Q(Chi2(), 1.0);
127  }
128
129  double Fisher::p_value_exact() const
130  { 
131    double res=0;
132    double a, c, tmp;
133    expected(a, tmp, c, tmp);
134    // sum P(x) over x for which abs(x-E(a))>=abs(a-E(a)) 
135
136    // counting left tail
137    int k = static_cast<int>(a - std::abs(a_-a));
138    if (k>=0)
139      res = cdf_hypergeometric_P(k, a_+b_, c_+d_, a_+c_);
140    // counting right tail
141    k = static_cast<int>(c - std::abs(c_-c));
142    if (k>=0)
143      res += cdf_hypergeometric_P(k, c_+d_, a_+b_, a_+c_);
144
145    // avoid p>1 due to double counting middle one
146    return std::min(1.0, res);
147  }
148
149
150}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.