source: trunk/test/fisher.cc @ 3455

Last change on this file since 3455 was 3455, checked in by Peter, 7 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1// $Id: fisher.cc 3455 2015-12-10 06:57:14Z peter $
2
3/*
4  Copyright (C) 2008, 2009, 2010, 2012, 2013, 2014, 2015 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include <config.h>
23
24#include "Suite.h"
25
26#include "yat/statistics/Fisher.h"
27
28#include <climits>
29
30using namespace theplu::yat;
31void test_p_value(test::Suite&);
32void test_p_value_approximative(test::Suite&);
33void test_p_value_exact(test::Suite&);
34void test_large_numbers(test::Suite&);
35void test_yates(test::Suite&);
36
37int main(int argc, char* argv[])
38{
39  test::Suite suite(argc, argv);
40
41  statistics::Fisher f;
42  if (!suite.equal(f.oddsratio(1,4,5,1), 0.05)) {
43    suite.add(false);
44    suite.err() << "oddsratio failed\n";
45  }
46  double a, b, c, d;
47  f.expected(a,b,c,d);
48  if (!suite.equal(a, 30.0/11.0)) {
49    suite.add(false);
50    suite.err() << "expected a failed\n";
51  }
52  if (!suite.equal(b, 25.0/11.0)) {
53    suite.add(false);
54    suite.err() << "expected b failed\n";
55  }
56  if (!suite.equal(c, 36.0/11.0)) {
57    suite.add(false);
58    suite.err() << "expected c failed\n";
59  }
60  if (!suite.equal(d, 30.0/11.0)) {
61    suite.add(false);
62    suite.err() << "expected d failed\n";
63  }
64  if (!suite.equal(f.Chi2(), 4.4122222222222222222222)) {
65    suite.add(false);
66    suite.err() << "Chi2 failed\n";
67  }
68  if (!suite.equal(f.minimum_size(),10)) {
69    suite.add(false);
70    suite.err() << "minimum_size failed\n";
71  }
72  test_p_value(suite);
73  test_large_numbers(suite);
74  test_yates(suite);
75  return suite.return_value();
76}
77
78
79void test_large_numbers(test::Suite& suite)
80{
81  // skip test if unsigned int is 16 bit
82  if ((UINT_MAX >> 16) == 0) {
83    suite.out() << "skipping test_large_numbers\n";
84    return;
85  }
86
87  statistics::Fisher f;
88  double oddsratio = f.oddsratio(1166,63326825-1166,1095,66074759-1095);
89  if (oddsratio<0.5 || oddsratio>2) {
90    suite.err() << "oddsratio: " << oddsratio << "\n";
91    suite.err() << "expected ~ 1\n";
92    suite.add(false);
93  }
94  suite.add(suite.equal_fix(f.p_value(), 0.0123, 0.0001));
95  f.p_left();
96  f.p_right();
97}
98
99
100void test_p_value(test::Suite& suite)
101{
102  test_p_value_exact(suite);
103  test_p_value_approximative(suite);
104}
105
106
107void test_p_value_approximative(test::Suite& suite)
108{
109  suite.err() << "testing p_value_approximative\n";
110  statistics::Fisher f;
111  f.minimum_size() = 0;
112  f.oddsratio(10,20,20,50);
113  // oddsratio 1.25 > 1 so p = 2*p_right
114  suite.add(suite.equal(f.p_value(), 2*f.p_right()));
115  f.oddsratio(10,20,10,20);
116  suite.add(suite.equal(f.p_value(), 1.0));
117  suite.add(suite.equal(f.p_right(), 0.5));
118  suite.add(suite.equal(f.p_left(), 0.5));
119}
120
121
122void test_p_value_exact(test::Suite& suite)
123{
124  suite.err() << "test p_value_exact\n";
125  statistics::Fisher f;
126  f.minimum_size() = 1000;
127  f.oddsratio(10,20,10,20);
128  suite.add(suite.equal(f.p_value(), 1.0, 20));
129
130  f.oddsratio(10, 20, 20, 200);
131  suite.add(suite.equal_fix(f.p_value(), 0.000811906062767622,1e-16));
132  suite.add(suite.equal_fix(f.p_right(), 0.000811906062767622,1e-16));
133
134  // testing symmetry
135  statistics::Fisher f2;
136  f2.minimum_size() = 1000;
137  f2.oddsratio(20, 200, 10, 20);
138  suite.add(suite.equal(f2.p_value(), f.p_value()));
139  suite.add(suite.equal(f2.p_left(), f.p_right()));
140
141  f.oddsratio(1, 1, 1, 2);
142  suite.add(suite.equal(f.p_value(), 1.0, 2));
143  suite.add(suite.equal(f.p_right(), 0.7));
144  suite.add(suite.equal(f.p_left(), 0.9));
145
146  f.oddsratio(1, 1, 2, 1);
147  suite.add(suite.equal(f.p_value(), 1.0, 2));
148  suite.add(suite.equal(f.p_right(), 0.9));
149  suite.add(suite.equal(f.p_left(), 0.7));
150}
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}
Note: See TracBrowser for help on using the repository browser.