source: trunk/test/fisher.cc @ 3004

Last change on this file since 3004 was 3004, checked in by Peter, 10 years ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1// $Id: fisher.cc 3004 2013-03-24 00:51:14Z peter $
2
3/*
4  Copyright (C) 2008, 2009, 2010, 2012 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&);
35
36int main(int argc, char* argv[])
37{
38  test::Suite suite(argc, argv);
39
40  statistics::Fisher f;
41  if (!suite.equal(f.oddsratio(1,4,5,1), 0.05)) {
42    suite.add(false);
43    suite.err() << "oddsratio failed\n";
44  }
45  double a, b, c, d;
46  f.expected(a,b,c,d);
47  if (!suite.equal(a, 30.0/11.0)) {
48    suite.add(false);
49    suite.err() << "expected a failed\n";
50  }
51  if (!suite.equal(b, 25.0/11.0)) {
52    suite.add(false);
53    suite.err() << "expected b failed\n";
54  }
55  if (!suite.equal(c, 36.0/11.0)) {
56    suite.add(false);
57    suite.err() << "expected c failed\n";
58  }
59  if (!suite.equal(d, 30.0/11.0)) {
60    suite.add(false);
61    suite.err() << "expected d failed\n";
62  }
63  if (!suite.equal(f.Chi2(), 4.4122222222222222222222)) {
64    suite.add(false);
65    suite.err() << "Chi2 failed\n";
66  }
67  if (!suite.equal(f.minimum_size(),10)) {
68    suite.add(false);
69    suite.err() << "minimum_size failed\n";
70  }
71  test_p_value(suite);
72  test_large_numbers(suite);
73  return suite.return_value();
74}
75
76
77void test_large_numbers(test::Suite& suite)
78{
79  // skip test if unsigned int is 16 bit
80  if ((UINT_MAX >> 16) == 0) {
81    suite.out() << "skipping test_large_numbers\n";
82    return;
83  }
84
85  statistics::Fisher f;
86  double oddsratio = f.oddsratio(1166,63326825-1166,1095,66074759-1095);
87  if (oddsratio<0.5 || oddsratio>2) {
88    suite.err() << "oddsratio: " << oddsratio << "\n";
89    suite.err() << "expected ~ 1\n";
90    suite.add(false);
91  }
92  suite.add(suite.equal_fix(f.p_value(), 0.0123, 0.0001));
93  f.p_left();
94  f.p_right();
95}
96
97
98void test_p_value(test::Suite& suite)
99{
100  test_p_value_exact(suite);
101  test_p_value_approximative(suite);
102}
103
104
105void test_p_value_approximative(test::Suite& suite)
106{
107  suite.err() << "testing p_value_approximative\n";
108  statistics::Fisher f;
109  f.minimum_size() = 0;
110  f.oddsratio(10,20,20,50);
111  // oddsratio 1.25 > 1 so p = 2*p_right
112  suite.add(suite.equal(f.p_value(), 2*f.p_right()));
113  f.oddsratio(10,20,10,20);
114  suite.add(suite.equal(f.p_value(), 1.0));
115  suite.add(suite.equal(f.p_right(), 0.5));
116  suite.add(suite.equal(f.p_left(), 0.5));
117}
118
119
120void test_p_value_exact(test::Suite& suite)
121{
122  suite.err() << "test p_value_exact\n";
123  statistics::Fisher f;
124  f.minimum_size() = 1000;
125  f.oddsratio(10,20,10,20);
126  suite.add(suite.equal(f.p_value(), 1.0));
127
128  f.oddsratio(10, 20, 20, 200);
129  suite.add(suite.equal_fix(f.p_value(), 0.000811906062767622,1e-16));
130  suite.add(suite.equal_fix(f.p_right(), 0.000811906062767622,1e-16));
131
132  // testing symmetry
133  statistics::Fisher f2;
134  f2.minimum_size() = 1000;
135  f2.oddsratio(20, 200, 10, 20);
136  suite.add(suite.equal(f2.p_value(), f.p_value()));
137  suite.add(suite.equal(f2.p_left(), f.p_right()));
138
139  f.oddsratio(1, 1, 1, 2);
140  suite.add(suite.equal(f.p_value(), 1.0, 2));
141  suite.add(suite.equal(f.p_right(), 0.7));
142  suite.add(suite.equal(f.p_left(), 0.9));
143
144  f.oddsratio(1, 1, 2, 1);
145  suite.add(suite.equal(f.p_value(), 1.0, 2));
146  suite.add(suite.equal(f.p_right(), 0.9));
147  suite.add(suite.equal(f.p_left(), 0.7));
148}
Note: See TracBrowser for help on using the repository browser.