Changeset 3298


Ignore:
Timestamp:
Aug 8, 2014, 9:15:28 AM (9 years ago)
Author:
Peter
Message:

Yates' correction in Fisher's exact test. fixes #807.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/fisher.cc

    r3114 r3298  
    22
    33/*
    4   Copyright (C) 2008, 2009, 2010, 2012, 2013 Peter Johansson
     4  Copyright (C) 2008, 2009, 2010, 2012, 2013, 2014 Peter Johansson
    55
    66  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    3333void test_p_value_exact(test::Suite&);
    3434void test_large_numbers(test::Suite&);
     35void test_yates(test::Suite&);
    3536
    3637int main(int argc, char* argv[])
     
    7172  test_p_value(suite);
    7273  test_large_numbers(suite);
     74  test_yates(suite);
    7375  return suite.return_value();
    7476}
     
    147149  suite.add(suite.equal(f.p_left(), 0.7));
    148150}
     151
     152
     153void test_yates(test::Suite& suite)
     154{
     155  statistics::Fisher f;
     156  statistics::Fisher f2(true);
     157
     158  f.oddsratio(5,10,10,10);
     159  f2.oddsratio(5,10,10,10);
     160  f.minimum_size() = 0;
     161  f2.minimum_size() = 0;
     162  double p = f.p_value();
     163  double p_yates = f2.p_value();
     164  if (p==p_yates) {
     165    suite.add(false);
     166    suite.err() << "p-value: " << p << "\n";
     167    suite.err() << "p-value yates's corrected: " << p_yates << "\n";
     168  }
     169}
  • trunk/yat/statistics/Fisher.cc

    r3004 r3298  
    55  Copyright (C) 2005 Peter Johansson
    66  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
    7   Copyright (C) 2009, 2010, 2011, 2012, 2013 Peter Johansson
     7  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Peter Johansson
    88
    99  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    4141
    4242
    43   Fisher::Fisher()
    44     : a_(0), b_(0), c_(0), d_(0), minimum_size_(10), oddsratio_(1.0)
     43  Fisher::Fisher(bool yates)
     44    : a_(0), b_(0), c_(0), d_(0), minimum_size_(10), oddsratio_(1.0),
     45      yates_(yates)
    4546  {
    4647  }
     
    6263    double a,b,c,d;
    6364    expected(a,b,c,d);
     65    if (yates_)
     66      return yates(a_, a) + yates(b_, b) + yates(c_, c) + yates(d_, d);
     67
    6468    return (a-a_)*(a-a_)/a + (b-b_)*(b-b_)/b +
    6569      (c-c_)*(c-c_)/c + (d-d_)*(d-d_)/d;
     
    204208  }
    205209
     210
     211  double Fisher::yates(unsigned int o, double e) const
     212  {
     213    double x = std::abs(o - e) - 0.5;
     214    return x*x/e;
     215  }
     216
    206217}}} // of namespace statistics, yat, and theplu
  • trunk/yat/statistics/Fisher.h

    r3004 r3298  
    77  Copyright (C) 2004, 2005 Peter Johansson
    88  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
    9   Copyright (C) 2009, 2011, 2013 Peter Johansson
     9  Copyright (C) 2009, 2011, 2013, 2014 Peter Johansson
    1010
    1111  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    6969    ///
    7070    /// Default Constructor.
    71     ///
    72     Fisher(void);
     71    ///
     72    /// \param yates if true Yates's correction is used for
     73    /// chi-squared calculation
     74    ///
     75    Fisher(bool yates=false);
    7376
    7477    ///
     
    8285       \frac{(O_i-E_i)^2}{E_i}\f$ where \a E is expected value and \a
    8386       O is observed value.
     87
     88       If indicated in constructor, Yates's correction is used, i.e.,
     89       Chi2 is calculated as \f$ \frac{(|O_i-E_i|-0.5)^2}{E_i} \f$
     90
    8491
    8592       \see expected(double&, double&, double&, double&)
     
    193200    double p_value_exact(void) const;
    194201
     202    double yates(unsigned int o, double e) const;
     203
    195204    unsigned int a_;
    196205    unsigned int b_;
     
    199208    unsigned int minimum_size_;
    200209    double oddsratio_;
     210    bool yates_;
    201211  };
    202212
Note: See TracChangeset for help on using the changeset viewer.