source: trunk/test/fisher.cc @ 2881

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

Define PP variables in config.h rather than in CPPFLAGS. Include
config.h into all source files. Only ammend CXXFLAGS with '-Wall
-pedantic' when --enable-debug. In default mode we respect CXXFLAGS
value set by user, or set to default value '-O3'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1// $Id: fisher.cc 2881 2012-11-18 01:28:05Z 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_value_one_sided();
94}
95
96
97void test_p_value(test::Suite& suite)
98{
99  test_p_value_exact(suite);
100  test_p_value_approximative(suite);
101}
102
103
104void test_p_value_approximative(test::Suite& suite)
105{
106  suite.err() << "testing p_value_approximative\n";
107  statistics::Fisher f;
108  f.minimum_size() = 0;
109  f.oddsratio(10,20,20,50);
110  suite.add(suite.equal(f.p_value(), 2*f.p_value_one_sided()));
111  f.oddsratio(10,20,10,20);
112  suite.add(suite.equal(f.p_value(), 1.0));
113  suite.add(suite.equal(f.p_value_one_sided(), 0.5));
114}
115
116
117void test_p_value_exact(test::Suite& suite)
118{
119  suite.err() << "test p_value_exact\n";
120  statistics::Fisher f;
121  f.minimum_size() = 1000;
122  f.oddsratio(10,20,10,20);
123  suite.add(suite.equal(f.p_value(), 1.0));
124
125  f.oddsratio(10, 20, 20, 200);
126  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));
128
129  // testing symmetry
130  statistics::Fisher f2;
131  f2.minimum_size() = 1000;
132  f2.oddsratio(20, 200, 10, 20);
133  suite.add(suite.equal(f2.p_value(), f.p_value()));
134
135  f.oddsratio(1, 1, 1, 2);
136  suite.add(suite.equal(f.p_value(), 1.0, 2));
137  suite.add(suite.equal(f.p_value_one_sided(), 0.7));
138
139  f.oddsratio(1, 1, 2, 1);
140  suite.add(suite.equal(f.p_value(), 1.0, 2));
141  suite.add(suite.equal(f.p_value_one_sided(), 0.9));
142
143}
Note: See TracBrowser for help on using the repository browser.