Changeset 3004 for trunk/yat/statistics/Fisher.cc
- Timestamp:
- Mar 24, 2013, 1:51:14 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/statistics/Fisher.cc
r2919 r3004 5 5 Copyright (C) 2005 Peter Johansson 6 6 Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson 7 Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson7 Copyright (C) 2009, 2010, 2011, 2012, 2013 Peter Johansson 8 8 9 9 This file is part of the yat library, http://dev.thep.lu.se/yat … … 40 40 namespace statistics { 41 41 42 42 43 43 Fisher::Fisher() 44 44 : a_(0), b_(0), c_(0), d_(0), minimum_size_(10), oddsratio_(1.0) … … 54 54 bool Fisher::calculate_p_exact(void) const 55 55 { 56 return ( a_<minimum_size_ || b_<minimum_size_ || 57 c_<minimum_size_ || d_<minimum_size_); 58 } 59 60 double Fisher::Chi2( ) const56 return ( a_<minimum_size_ || b_<minimum_size_ || 57 c_<minimum_size_ || d_<minimum_size_); 58 } 59 60 double Fisher::Chi2(void) const 61 61 { 62 62 double a,b,c,d; 63 63 expected(a,b,c,d); 64 return (a-a_)*(a-a_)/a + (b-b_)*(b-b_)/b + 64 return (a-a_)*(a-a_)/a + (b-b_)*(b-b_)/b + 65 65 (c-c_)*(c-c_)/c + (d-d_)*(d-d_)/d; 66 66 } … … 97 97 const unsigned int b, 98 98 const unsigned int c, 99 const unsigned int d) 99 const unsigned int d) 100 100 { 101 101 // If a column sum or a row sum is zero, the table is nonsense … … 108 108 d_ = d; 109 109 110 oddsratio_= (static_cast<double>(a) / static_cast<double>(b) * 110 oddsratio_= (static_cast<double>(a) / static_cast<double>(b) * 111 111 static_cast<double>(d) / static_cast<double>(c) ); 112 112 return oddsratio_; … … 120 120 121 121 122 double Fisher::p_value() const 122 double Fisher::p_left(void) const 123 { 124 if (!calculate_p_exact()) { 125 if (oddsratio_>1.0) 126 return 1.0-p_value_approximative()/2.0; 127 return p_value_approximative()/2.0; 128 } 129 // check for overflow 130 assert(c_ <= c_+d_ && d_ <= c_+d_); 131 assert(a_ <= a_+b_ && b_ <= a_+b_); 132 assert(a_ <= a_+c_ && c_ <= a_+c_); 133 134 return cdf_hypergeometric_P(a_, a_+b_, c_+d_, a_+c_); 135 } 136 137 138 double Fisher::p_value(void) const 123 139 { 124 140 if (calculate_p_exact()) … … 128 144 129 145 130 double Fisher::p_ value_one_sided() const146 double Fisher::p_right(void) const 131 147 { 132 148 if (!calculate_p_exact()) { … … 140 156 assert(a_ <= a_+c_ && c_ <= a_+c_); 141 157 142 return statistics::cdf_hypergeometric_P(c_, c_+d_, a_+b_, a_+c_); 143 } 144 145 146 double Fisher::p_value_approximative() const 158 return cdf_hypergeometric_P(c_, c_+d_, a_+b_, a_+c_); 159 } 160 161 162 double Fisher::p_value_one_sided(void) const 163 { 164 return p_right(); 165 } 166 167 168 double Fisher::p_value_approximative(void) const 147 169 { 148 170 return gsl_cdf_chisq_Q(Chi2(), 1.0); 149 171 } 150 172 151 double Fisher::p_value_exact() const 152 { 173 174 double Fisher::p_left_exact(void) const 175 { 176 return 0; 177 } 178 179 180 double Fisher::p_right_exact(void) const 181 { 182 return 0; 183 } 184 185 186 double Fisher::p_value_exact(void) const 187 { 153 188 double res=0; 154 189 double a, c, tmp; 155 190 expected(a, tmp, c, tmp); 156 // sum P(x) over x for which abs(x-E(a))>=abs(a-E(a)) 191 // sum P(x) over x for which abs(x-E(a))>=abs(a-E(a)) 157 192 158 193 // counting left tail
Note: See TracChangeset
for help on using the changeset viewer.