source: trunk/test/fisher_test.cc @ 1746

Last change on this file since 1746 was 1746, checked in by Peter, 12 years ago

Merged patch release 0.4.3 to the trunk. Delta 0.4.3 - 0.4.2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1// $Id: fisher_test.cc 1746 2009-01-23 16:59:11Z peter $
2
3/*
4  Copyright (C) 2008 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 "Suite.h"
23
24#include "yat/statistics/Fisher.h"
25
26using namespace theplu::yat;
27void test_p_value(test::Suite&);
28void test_p_value_approximative(test::Suite&);
29void test_p_value_exact(test::Suite&);
30
31int main(int argc, char* argv[])
32{ 
33  test::Suite suite(argc, argv);
34
35  statistics::Fisher f;
36  if (!suite.equal(f.oddsratio(1,4,5,1), 0.05)) {
37    suite.add(false);
38    suite.err() << "oddsratio failed\n";
39  }
40  double a, b, c, d;
41  f.expected(a,b,c,d);
42  if (!suite.equal(a, 30.0/11.0)) {
43    suite.add(false);
44    suite.err() << "expected a failed\n";
45  }
46  if (!suite.equal(b, 25.0/11.0)) {
47    suite.add(false);
48    suite.err() << "expected b failed\n";
49  }
50  if (!suite.equal(c, 36.0/11.0)) {
51    suite.add(false);
52    suite.err() << "expected c failed\n";
53  }
54  if (!suite.equal(d, 30.0/11.0)) {
55    suite.add(false);
56    suite.err() << "expected d failed\n";
57  }
58  if (!suite.equal(f.Chi2(), 4.4122222222222222222222)) { 
59    suite.add(false);
60    suite.err() << "Chi2 failed\n";
61  }
62  if (!suite.equal(f.minimum_size(),10)) {
63    suite.add(false);
64    suite.err() << "minimum_size failed\n";
65  }
66  test_p_value(suite);
67  return suite.return_value();
68}
69
70
71void test_p_value(test::Suite& suite)
72{
73  test_p_value_exact(suite);
74  test_p_value_approximative(suite);
75}
76
77
78void test_p_value_approximative(test::Suite& suite)
79{
80  suite.err() << "testing p_value_approximative\n";
81  statistics::Fisher f;
82  f.minimum_size() = 0;
83  f.oddsratio(10,20,20,50);
84  suite.add(suite.equal(f.p_value(), 2*f.p_value_one_sided()));
85  f.oddsratio(10,20,10,20);
86  suite.add(suite.equal(f.p_value(), 1.0));
87  suite.add(suite.equal(f.p_value_one_sided(), 0.5));
88}
89
90
91void test_p_value_exact(test::Suite& suite)
92{
93  suite.err() << "test p_value_exact\n";
94  statistics::Fisher f;
95  f.minimum_size() = 1000;
96  f.oddsratio(10,20,10,20);
97  suite.add(suite.equal(f.p_value(), 1.0));
98
99  f.oddsratio(10, 20, 20, 200);
100  suite.add(suite.equal_fix(f.p_value(), 0.000811906062767622,2e-17));
101  suite.add(suite.equal_fix(f.p_value_one_sided(), 0.000811906062767622,2e-17));
102
103  // testing symmetry
104  statistics::Fisher f2;
105  f2.minimum_size() = 1000;
106  f2.oddsratio(20, 200, 10, 20);
107  suite.add(suite.equal(f2.p_value(), f.p_value()));
108
109  f.oddsratio(1, 1, 1, 2);
110  suite.add(suite.equal(f.p_value(), 1.0, 2));
111  suite.add(suite.equal(f.p_value_one_sided(), 0.7));
112
113  f.oddsratio(1, 1, 2, 1);
114  suite.add(suite.equal(f.p_value(), 1.0, 2));
115  suite.add(suite.equal(f.p_value_one_sided(), 0.9));
116
117}
Note: See TracBrowser for help on using the repository browser.