1 | // $Id: tTest.cc 1797 2009-02-12 18:07:10Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2005 Peter Johansson |
---|
6 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
7 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "tTest.h" |
---|
26 | #include "AveragerWeighted.h" |
---|
27 | #include "tScore.h" |
---|
28 | |
---|
29 | #include <algorithm> |
---|
30 | #include <cassert> |
---|
31 | #include <cmath> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace statistics { |
---|
36 | |
---|
37 | tTest::tTest(void) |
---|
38 | : updated_(false) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | void tTest::add(double x, bool target, double w) |
---|
44 | { |
---|
45 | if (!w) |
---|
46 | return; |
---|
47 | if (target) |
---|
48 | pos_.add(x,w); |
---|
49 | else |
---|
50 | neg_.add(x,w); |
---|
51 | updated_=false; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | void tTest::reset(void) |
---|
56 | { |
---|
57 | pos_.reset(); |
---|
58 | neg_.reset(); |
---|
59 | dof_=0; |
---|
60 | t_=0; |
---|
61 | updated_=false; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | double tTest::score(void) |
---|
66 | { |
---|
67 | if (!updated_){ |
---|
68 | tScore score(false); |
---|
69 | t_ = score.score(pos_, neg_, &dof_); |
---|
70 | updated_=true; |
---|
71 | } |
---|
72 | return t_; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | double tTest::p_value(void) const |
---|
77 | { |
---|
78 | double p=2*p_value_one_sided(); |
---|
79 | return std::min(p,2-p); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | double tTest::p_value_one_sided(void) const |
---|
84 | { |
---|
85 | double t=t_; |
---|
86 | double dof=dof_; |
---|
87 | if (!updated_){ |
---|
88 | tScore score(false); |
---|
89 | t = score.score(pos_, neg_, &dof); |
---|
90 | } |
---|
91 | if (!dof) |
---|
92 | return 1.0; |
---|
93 | return gsl_cdf_tdist_Q(t, dof); |
---|
94 | } |
---|
95 | |
---|
96 | }}} // of namespace statistics, yat, and theplu |
---|