// \$Id: Fisher.cc 1487 2008-09-10 08:41:36Z jari \$ /* Copyright (C) 2004 Jari Häkkinen, Peter Johansson Copyright (C) 2005 Peter Johansson Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson Copyright (C) 2008 Peter Johansson This file is part of the yat library, http://dev.thep.lu.se/yat The yat library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. The yat library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with yat. If not, see . */ #include "Fisher.h" #include "utility.h" #include #include namespace theplu { namespace yat { namespace statistics { Fisher::Fisher() : a_(0), b_(0), c_(0), d_(0), oddsratio_(1.0) { } Fisher::~Fisher() { } bool Fisher::calculate_p_exact(void) const { return ( a_1.0) return 2*p_value_one_sided(); if (oddsratio_<1.0){ // If oddsratio is less than unity // Two-sided p-value is // P(X <= oddsratio) + P(X >= 1/oddsratio) = // P(X <= oddsratio) + P(X <= oddsratio) // 2 * (P(X < oddsratio) + P(X = oddsratio)) // 2 * (1 - P(X >= oddsratio) + P(X = oddsratio)) if (calculate_p_exact()) return 2*(1-p_value_one_sided()+ gsl_ran_hypergeometric_pdf(a_,a_+b_,c_+d_, a_+c_) ); // if p is calculated approximatively correction is not needed return 2*(1-p_value_one_sided()); } return 1.0; } double Fisher::p_value_one_sided() const { if ( calculate_p_exact() ) return p_value_exact(); return p_value_approximative(); } double Fisher::p_value_approximative() const { return gsl_cdf_chisq_Q(Chi2(), 1.0); } double Fisher::p_value_exact() const { // Since the calculation is symmetric and cdf_hypergeometric_P // loops up to k we choose the smallest number to be k and mirror // the matrix. This choice makes the p-value two-sided. if (a_