Changeset 3004


Ignore:
Timestamp:
Mar 24, 2013, 1:51:14 AM (9 years ago)
Author:
Peter
Message:

refs #689. Deprecate Fisher::one_sided_p; implement Fisher::left_p and right_p.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/NEWS

    r2960 r3004  
    44
    55version 0.11 (released NOT YET)
     6  - Fisher::one_sided_p(void) is now deprecated (ticket #689)
    67
    78  A complete list of closed tickets can be found here [[br]]
  • trunk/test/fisher.cc

    r2881 r3004  
    9191  }
    9292  suite.add(suite.equal_fix(f.p_value(), 0.0123, 0.0001));
    93   f.p_value_one_sided();
     93  f.p_left();
     94  f.p_right();
    9495}
    9596
     
    108109  f.minimum_size() = 0;
    109110  f.oddsratio(10,20,20,50);
    110   suite.add(suite.equal(f.p_value(), 2*f.p_value_one_sided()));
     111  // oddsratio 1.25 > 1 so p = 2*p_right
     112  suite.add(suite.equal(f.p_value(), 2*f.p_right()));
    111113  f.oddsratio(10,20,10,20);
    112114  suite.add(suite.equal(f.p_value(), 1.0));
    113   suite.add(suite.equal(f.p_value_one_sided(), 0.5));
     115  suite.add(suite.equal(f.p_right(), 0.5));
     116  suite.add(suite.equal(f.p_left(), 0.5));
    114117}
    115118
     
    125128  f.oddsratio(10, 20, 20, 200);
    126129  suite.add(suite.equal_fix(f.p_value(), 0.000811906062767622,1e-16));
    127   suite.add(suite.equal_fix(f.p_value_one_sided(), 0.000811906062767622,1e-16));
     130  suite.add(suite.equal_fix(f.p_right(), 0.000811906062767622,1e-16));
    128131
    129132  // testing symmetry
     
    132135  f2.oddsratio(20, 200, 10, 20);
    133136  suite.add(suite.equal(f2.p_value(), f.p_value()));
     137  suite.add(suite.equal(f2.p_left(), f.p_right()));
    134138
    135139  f.oddsratio(1, 1, 1, 2);
    136140  suite.add(suite.equal(f.p_value(), 1.0, 2));
    137   suite.add(suite.equal(f.p_value_one_sided(), 0.7));
     141  suite.add(suite.equal(f.p_right(), 0.7));
     142  suite.add(suite.equal(f.p_left(), 0.9));
    138143
    139144  f.oddsratio(1, 1, 2, 1);
    140145  suite.add(suite.equal(f.p_value(), 1.0, 2));
    141   suite.add(suite.equal(f.p_value_one_sided(), 0.9));
    142 
     146  suite.add(suite.equal(f.p_right(), 0.9));
     147  suite.add(suite.equal(f.p_left(), 0.7));
    143148}
  • trunk/yat/statistics/Fisher.cc

    r2919 r3004  
    55  Copyright (C) 2005 Peter Johansson
    66  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
    7   Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson
     7  Copyright (C) 2009, 2010, 2011, 2012, 2013 Peter Johansson
    88
    99  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    4040namespace statistics {
    4141
    42  
     42
    4343  Fisher::Fisher()
    4444    : a_(0), b_(0), c_(0), d_(0), minimum_size_(10), oddsratio_(1.0)
     
    5454  bool Fisher::calculate_p_exact(void) const
    5555  {
    56     return ( a_<minimum_size_ || b_<minimum_size_ || 
    57              c_<minimum_size_ || d_<minimum_size_); 
    58   }
    59 
    60   double Fisher::Chi2() const
     56    return ( a_<minimum_size_ || b_<minimum_size_ ||
     57             c_<minimum_size_ || d_<minimum_size_);
     58  }
     59
     60  double Fisher::Chi2(void) const
    6161  {
    6262    double a,b,c,d;
    6363    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 +
    6565      (c-c_)*(c-c_)/c + (d-d_)*(d-d_)/d;
    6666  }
     
    9797                           const unsigned int b,
    9898                           const unsigned int c,
    99                            const unsigned int d) 
     99                           const unsigned int d)
    100100  {
    101101    // If a column sum or a row sum is zero, the table is nonsense
     
    108108    d_ = d;
    109109
    110     oddsratio_= (static_cast<double>(a) / static_cast<double>(b) * 
     110    oddsratio_= (static_cast<double>(a) / static_cast<double>(b) *
    111111                 static_cast<double>(d) / static_cast<double>(c) );
    112112    return oddsratio_;
     
    120120
    121121
    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
    123139  {
    124140    if (calculate_p_exact())
     
    128144
    129145
    130   double Fisher::p_value_one_sided() const
     146  double Fisher::p_right(void) const
    131147  {
    132148    if (!calculate_p_exact()) {
     
    140156    assert(a_ <= a_+c_ && c_ <= a_+c_);
    141157
    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
    147169  {
    148170    return gsl_cdf_chisq_Q(Chi2(), 1.0);
    149171  }
    150172
    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  {
    153188    double res=0;
    154189    double a, c, tmp;
    155190    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))
    157192
    158193    // counting left tail
  • trunk/yat/statistics/Fisher.h

    r2555 r3004  
    11#ifndef _theplu_yat_statistics_fisher_
    2 #define _theplu_yat_statistics_fisher_ 
     2#define _theplu_yat_statistics_fisher_
    33
    44// $Id$
     
    77  Copyright (C) 2004, 2005 Peter Johansson
    88  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
    9   Copyright (C) 2009, 2011 Peter Johansson
     9  Copyright (C) 2009, 2011, 2013 Peter Johansson
    1010
    1111  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    2525*/
    2626
     27#include <yat/utility/deprecate.h>
     28
    2729namespace theplu {
    2830namespace yat {
    29 namespace statistics { 
     31namespace statistics {
     32
    3033  /**
    31      @brief Fisher's exact test.   
     34     @brief Fisher's exact test.
    3235
    3336     Fisher's Exact test is a procedure that you can use for data
     
    5255     because a large sample approximation (that the Chi-square test
    5356     relies on) is very reasonable. If all elements are larger than
    54      10 a Chi-square test is reasonable to use. 
    55      
     57     10 a Chi-square test is reasonable to use.
     58
    5659     @note The statistica assumes that each column and row sum,
    5760     respectively, are fixed. Just because you have a 2x2 table, this
     
    5962     e.g. Barnard's test for alternative.
    6063  */
    61  
     64
    6265  class Fisher
    6366  {
    64  
     67
    6568  public:
    6669    ///
     
    7073
    7174    ///
    72     /// Destructor 
     75    /// Destructor
    7376    ///
    7477    virtual ~Fisher(void);
    75          
    76    
     78
     79
    7780    /**
    7881       The Chi2 score is calculated as \f$ \sum
    7982       \frac{(O_i-E_i)^2}{E_i}\f$ where \a E is expected value and \a
    8083       O is observed value.
     84
     85       \see expected(double&, double&, double&, double&)
    8186
    8287       \return Chi2 score
     
    110115
    111116    /**
     117       Calculates probability to get oddsratio (or smaller).
     118
    112119       If all elements in table is at least minimum_size(), a Chi2
    113120       approximation is used.
    114        
     121
     122       \since New in yat 0.11
     123     */
     124    double p_left(void) const;
     125
     126    /**
     127       Calculates probability to get oddsratio (or greater).
     128
     129       If all elements in table is at least minimum_size(), a Chi2
     130       approximation is used.
     131
     132       \since New in yat 0.11
     133     */
     134    double p_right(void) const;
     135
     136    /**
     137       If all elements in table is at least minimum_size(), a Chi2
     138       approximation is used.
     139
    115140       Otherwise a two-sided p-value is calculated using the
    116141       hypergeometric distribution
     
    120145       \return two-sided p-value
    121146    */
    122     double p_value() const;
    123    
     147    double p_value(void) const;
     148
    124149    ///
    125150    /// One-sided p-value is probability to get larger (or equal) oddsratio.
     
    130155    /// @return One-sided p-value
    131156    ///
    132     double p_value_one_sided() const;
    133    
    134     /**
    135        Function calculating odds ratio from 2x2 table
     157    /// \deprecated Provided for backward compatibility with the 0.10
     158    /// API. Use p_right() instead.
     159    ///
     160    double p_value_one_sided() const YAT_DEPRECATE;
     161
     162    /**
     163       Function calculating odds ratio from 2x2 table
    136164       \f[ \begin{tabular}{|c|c|}
    137165       \hline a&b \tabularnewline \hline c&d \tabularnewline \hline
     
    140168       Object will remember the values of \a a, \a b, \a c, and \a d.
    141169
    142        @return odds ratio. 
     170       @return odds ratio.
    143171
    144172       @throw If table is invalid a runtime_error is thrown. A table
    145173       is invalid if a row or column sum is zero.
    146174    */
    147     double oddsratio(const unsigned int a, const unsigned int b, 
     175    double oddsratio(const unsigned int a, const unsigned int b,
    148176                     const unsigned int c, const unsigned int d);
    149177
     
    156184
    157185  private:
    158     bool calculate_p_exact() const;
     186    bool calculate_p_exact(void) const;
    159187
    160188    // two-sided
    161189    double p_value_approximative(void) const;
     190    double p_left_exact(void) const;
     191    double p_right_exact(void) const;
    162192    //two-sided
    163193    double p_value_exact(void) const;
Note: See TracChangeset for help on using the changeset viewer.