source: branches/0.6-stable/yat/statistics/Fisher.cc @ 2319

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

fixed more problems with overflow in Fisher. refs #638

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1// $Id: Fisher.cc 2319 2010-08-20 19:11:30Z 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, 2010 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#include <cassert>
33
34namespace theplu {
35namespace yat {
36namespace statistics {
37
38 
39  Fisher::Fisher()
40    : a_(0), b_(0), c_(0), d_(0), minimum_size_(10), oddsratio_(1.0)
41  {
42  }
43
44
45  Fisher::~Fisher()
46  {
47  }
48
49
50  bool Fisher::calculate_p_exact(void) const
51  {
52    return ( a_<minimum_size_ || b_<minimum_size_ || 
53             c_<minimum_size_ || d_<minimum_size_); 
54  }
55
56  double Fisher::Chi2() const
57  {
58    double a,b,c,d;
59    expected(a,b,c,d);
60    return (a-a_)*(a-a_)/a + (b-b_)*(b-b_)/b + 
61      (c-c_)*(c-c_)/c + (d-d_)*(d-d_)/d;
62  }
63
64
65  void Fisher::expected(double& a, double& b, double& c, double& d) const
66  {
67    // use floting point arithmetic to avoid overflow
68    double a1 = a_;
69    double b1 = b_;
70    double c1 = c_;
71    double d1 = d_;
72    double N = a1 + b1 + c1 + d1;
73    a =((a1+b1)*(a1+c1)) / N;
74    b =((a1+b1)*(b1+d1)) / N;
75    c =((c1+d1)*(a1+c1)) / N;
76    d =((c1+d1)*(b1+d1)) / N;
77  }
78
79
80  unsigned int& Fisher::minimum_size(void)
81  {
82    return minimum_size_;
83  }
84
85
86  const unsigned int& Fisher::minimum_size(void) const
87  {
88    return minimum_size_;
89  }
90
91
92  double Fisher::oddsratio(const unsigned int a,
93                           const unsigned int b,
94                           const unsigned int c,
95                           const unsigned int d) 
96  {
97    // If a column sum or a row sum is zero, the table is nonsense
98    if ((a==0 || d==0) && (c==0 || b==0)){
99      throw std::runtime_error("runtime_error: Table in Fisher is not valid\n");
100    }
101    a_ = a;
102    b_ = b;
103    c_ = c;
104    d_ = d;
105
106    oddsratio_= (static_cast<double>(a) / static_cast<double>(b) * 
107                 static_cast<double>(d) / static_cast<double>(c) );
108    return oddsratio_;
109  }
110
111
112  double Fisher::p_value() const
113  {
114    if (calculate_p_exact())
115      return p_value_exact();
116    return p_value_approximative();
117  }
118
119
120  double Fisher::p_value_one_sided() const
121  {
122    if (!calculate_p_exact()) {
123      if (oddsratio_<1.0)
124        return 1.0-p_value_approximative()/2.0;
125      return p_value_approximative()/2.0;
126    }
127    // check for overflow
128    assert(c_ <= c_+d_ && d_ <= c_+d_);
129    assert(a_ <= a_+b_ && b_ <= a_+b_);
130    assert(a_ <= a_+c_ && c_ <= a_+c_);
131
132    return statistics::cdf_hypergeometric_P(c_, c_+d_, a_+b_, a_+c_);
133  }
134
135
136  double Fisher::p_value_approximative() const
137  {
138    return gsl_cdf_chisq_Q(Chi2(), 1.0);
139  }
140
141  double Fisher::p_value_exact() const
142  { 
143    double res=0;
144    double a, c, tmp;
145    expected(a, tmp, c, tmp);
146    // sum P(x) over x for which abs(x-E(a))>=abs(a-E(a)) 
147
148    // counting left tail
149    int k = static_cast<int>(a - std::abs(a_-a));
150    if (k>=0)
151      res = cdf_hypergeometric_P(k, a_+b_, c_+d_, a_+c_);
152    // counting right tail
153    k = static_cast<int>(c - std::abs(c_-c));
154    if (k>=0)
155      res += cdf_hypergeometric_P(k, c_+d_, a_+b_, a_+c_);
156
157    // avoid p>1 due to double counting middle one
158    return std::min(1.0, res);
159  }
160
161}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.